7

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?

3
  • 4
    If you are using RStudio, then you need "soft wrap", see stackoverflow.com/q/39901028/680068 Commented Mar 12, 2024 at 9:29
  • 1
    Maybe this helps: How to wrap lines of code in Rstudio IDE? Commented Mar 12, 2024 at 9:35
  • 1
    @zx8754 Thanks for the RStudio pointers, but I am looking for real R character string constructs. Commented Mar 13, 2024 at 23:50

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No code or instructions needed. OP has all the code they need. Just including a citation to where I got my information from. :)
4

Here's a convenient infix operation that allows you to concatenate strings:

`%+%` <- stringr::str_c

This allows you to do string concatenation in a similar way to Python and other languages:
"hello " %+% "world"

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."

I just put this in e.g. my utils.R and source that file when I want to use this operator.

You can use paste0 instead of stringr::str_c if you so prefer.

Alternatively: you can also override the + operator directly (a la ggplot2), but this might make things confusing to your collaborators.

`+` = function(x, y) {
  if (is.character(x) || is.character(y)) {
    return(stringr::str_c(x , y, sep = ""))
  } else {
    .Primitive("+")(x, y)
  }
}

Comments

0

I don't believe there's a native way to achieve what you want in R. I couldn't find anything about multiline strings in the R Manuals.

This isn't exactly what you're looking for, but it might help:

long_string <- function(x) {
  x |>
    strwrap() |>
    paste0(collapse = " ") |>
    gsub(x = _, pattern = "\\s+", replacement = " ")
}
long_string(
  "
  Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor
  incidunt ut labore et dolore magna aliqua. Donec sed odio operae, eu
  vulputate felis rhoncus. Salutantibus vitae elit libero, a pharetra augue.
  Nihil hic munitissimus habendi senatus locus, nihil horum? A communi
  observantia non est recedendum.
  
  Plura mihi bona sunt, inclinet, amari petere vellent. Ab illo tempore, ab est 
  sed immemorabili. Ullamco laboris nisi ut aliquid ex ea commodi consequat. 
  Quae vero auctorem tractata ab fiducia dicuntur. At nos hinc posthac, 
  sitientis piros Afros.
  "
)

[1] "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Donec sed odio operae, eu vulputate felis rhoncus. Salutantibus vitae elit libero, a pharetra augue. Nihil hic munitissimus habendi senatus locus, nihil horum? A communi observantia non est recedendum. Plura mihi bona sunt, inclinet, amari petere vellent. Ab illo tempore, ab est sed immemorabili. Ullamco laboris nisi ut aliquid ex ea commodi consequat. Quae vero auctorem tractata ab fiducia dicuntur. At nos hinc posthac, sitientis piros Afros."

6 Comments

"I would like to learn if there is a native way to do this in R without any functions".
I think you did not read the part: "This isn't exactly what you're looking for, but it might help". With this attitude, you don't deserve any help.
I apologize for offending you. I tried to undo the downvote, but the system has locked it for some reason. I'll try again later.
Since I can't undo my downvote, I've gone around and upvoted your answers on other questions that I thought were good. Sorry again.
That's ok. You don't need to do that. Thank you for your answer and attitude to remediation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.