Skip to content
Prev 63068 / 63424 Next

Specifying a long string literal across several lines

My two cents (soon to be discontinued) is that it is not very relevant to compare one computer language ALREADY DESIGNED to another and insist they become the same. Next we will be asked to allow "+" or just adjacency to connect two or more quoted strings because, well, Python does that. Then, we can ask Python to give up using indentation and start using braces because R does that.

Paste and paste0 were planned as THE way R implements what is wanted. Sure, you could create functions with names like %plus% or %concat% or even overload that allow you to implement paste inline but is that much of a gain? Note suggestions here of things that could easily break if certain changes are blindly made.

Here is a trivial solution using %>>>% as an in-line operator you can replace with anything you want:

[1] "HelloThereCruelworld"
[1] "HelloThereCruelWorld"

But you want a multi-line approach this works the same:

"Hello" %>>>% 
  "There" %>>>% 
  "Cruel" %>>>% 
  "World"

No backslashes, just the function name at the end of each line. I question how it would be faster than paste or even more readble as the above called paste0 multiple times plus calls a function multiple times.

What exactly is wrong with this:

paste0(
  "Hello",
  "There",
  "Cruel",
  "World"
)

If properly written, you can even place the text left-justified or wherever and it should be quite readable. 

Having said that, yes, it can be very nice to use languages designed differently. R can be used in ways you might not imagine given delayed evaluations. The price of a paste0 call is compensated for by the simplification of some other things but note other packages handling strings are available now like stringi and stringr that may give you some other things you want. You can even overload something like + this way but do NOT do that as it messes lots of things up:

`+`<- function(a,b) paste0(a,b)
"first" +
  "Second" +
  "Third"

A safer approach like the above could be creating your own "objects" that hold text and arranging for that object to treat plus )or other symbols) specially.

I would say on the list of changes and updates to R, this feature may not look like it is worthwhile and may break things. If this feature is really that important to you, and you use it many times in your program, there are fairly oddball solutions such as running a program in an environment (as in POSIT RSTUDIO) that allows python and R to work together on a single set of data so python can be used to create variables like the ones you want and R can then use them. I suspect you might find such a solution far worse than just using R the way it was designed.





-----Original Message-----
From: R-devel <r-devel-bounces at r-project.org> On Behalf Of Josiah Parry
Sent: Monday, June 2, 2025 5:18 PM
To: Kasper Daniel Hansen <kasperdanielhansen at gmail.com>
Cc: r-devel at r-project.org
Subject: Re: [Rd] Specifying a long string literal across several lines

I suppose taste is learned as well. It does feel quite odd that the best
way to define a long string without a note or text wrapping is by being
creative with functions.

This is valid in Python, Julia, and Rust (if you add `let` and a
terminating semi-colon):

my_str = "part1\
part2\
part2"

I don't think it is abnormal to expect or desire this type of functionality
in our favorite language.

On Mon, Jun 2, 2025 at 13:59 Kasper Daniel Hansen <
kasperdanielhansen at gmail.com> wrote:

            
______________________________________________
R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel