Search code examples
rstring

Compose long character string in R across multiple lines but without functions or line breaks


In R, I want to write a very long character string that is wider than my window view (let's say longer than 80 characters). To make the code easier to read, I'd like to break it across multiple lines. I typically do this with the paste0() function like so:

long_string <- paste0(
  "This is a very long string that I would like to split across ",
  "multiple lines without introducing any new line characters in ",
  "the final output. This makes the code more readable without ",
  "affecting the string's integrity."
)

long_string
#> [1] "This is a very long string that I would like to split across multiple lines without introducing any new line characters in the final output. This makes the code more readable without affecting the string's integrity."

Created on 2024-03-12 with reprex v2.1.0

This is the exact result that I want, but I would like to learn if there is a native way to do this in R without any functions. My reason is that I create long strings extensively and I would like to learn a simpler way than paste0() or paste() if there is one. Here are some things I've tried that do not work as I want:

Simply break the string where I want for convenient typing (the problem here is that R inserts newline characters into my resulting string, which I do not want):

long_string <- "This is a very long string that I would like to split across 
multiple lines without introducing any new line characters in 
the final output. This makes the code more readable without 
affecting the string's integrity."

long_string
#> [1] "This is a very long string that I would like to split across \nmultiple lines without introducing any new line characters in \nthe final output. This makes the code more readable without \naffecting the string's integrity."

Created on 2024-03-12 with reprex v2.1.0

Concatenate with & (that doesn't work in R)

long_string <- "This is a very long string that I would like to split across " &
  "multiple lines without introducing any new line characters in " &
  "the final output. This makes the code more readable without " &
  "affecting the string's integrity."
#> Error in "This is a very long string that I would like to split across " & : operations are possible only for numeric, logical or complex types

long_string
#> Error in eval(expr, envir, enclos): object 'long_string' not found

Created on 2024-03-12 with reprex v2.1.0

Leave the strings quoted each on its own line (but then they are just independent strings; only the first substring is saved in the variable):

long_string <- "This is a very long string that I would like to split across "
"multiple lines without introducing any new line characters in "
#> [1] "multiple lines without introducing any new line characters in "
"the final output. This makes the code more readable without "
#> [1] "the final output. This makes the code more readable without "
"affecting the string's integrity."
#> [1] "affecting the string's integrity."

print(long_string)
#> [1] "This is a very long string that I would like to split across "

Created on 2024-03-12 with reprex v2.1.0

Concatenate with \ (same result as line-breaking within the string as above--it preserves the newline breaks, which I don't want):

long_string <- "This is a very long string that I would like to split across \
multiple lines without introducing any new line characters in \
the final output. This makes the code more readable without \
affecting the string's integrity."

long_string
#> [1] "This is a very long string that I would like to split across \nmultiple lines without introducing any new line characters in \nthe final output. This makes the code more readable without \naffecting the string's integrity."

Created on 2024-03-12 with reprex v2.1.0

Is there a native way in R to concatenate strings across lines without any functions and without inserting newline characters in the resulting long string?


Solution

  • While I would also like to see something like \ accomplish this, I did find that the Tidyverse Style Guide recommended paste0() (which you're already using).
    https://style.tidyverse.org/syntax.html#long-lines

    Maybe not the answer you were looking for, but "stay the course" seems to be the advice here.