Very Very long strings on a single line.

Hi All

I waste two hours of my life trying to paste a website url together with a vector in between
eg paste0("first_half_of_url",x,"second_half_of_url").
The second half of the url string was very long and I placed it on a single line on Rstudio, this caused some error message about an unexpected symbol.
Once the string was spread over 20 lines and all in view it was fine, can anybody shed some light on why for myself and other less experienced muppets?

Also just a shout out to all those who answer these questions, as hacks like myself would have given up long ago without the support that I find accross all of these programming communities.

1 Like

Breaking down and reassembling long-strings often results in the unexpected symbol nuisance, because it is so easy to create transcription errors when editing out your x. Here's what I do:

suppressPackageStartupMessages({
  library(glue)
  library(stringr)
})

# actual url is https://marlow.richard-careaga.com/posts/2020-08-16-duets.html
the_url <- "https://marlow.richard-careaga.com/posts/2020-08-16.html"
the_var <- "2020-08-16"
str_split(the_url,the_var) -> a
new_var <- "2021-03-14"
the_query <- glue(a[[1]][1],new_var,a[[1]][2])
the_query
#> https://marlow.richard-careaga.com/posts/2021-03-14.html

This is ultimately a limitation in the R parser. From ?parse:

          When input is taken from the console, 'n = NULL' is
          equivalent to 'n = 1', and 'n < 0' will read until an EOF
          character is read.  (The EOF character is Ctrl-Z for the
          Windows front-ends.)  The line-length limit is 4095 bytes
          when reading from the console (which may impose a lower
          limit: see 'An Introduction to R').
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.