Skip to content
Back to formatted view

Raw Message

Message-ID: <967f1f49-c268-4208-803b-e7af29d3e95c@gmail.com>
Date: 2025-06-02T10:19:27Z
From: Tomas Kalibera
Subject: Specifying a long string literal across several lines
In-Reply-To: <0d740a410d3ad7629105ad508f6969f2c9785e79.camel@unsw.edu.au>

On 5/28/25 04:15, Pavel Krivitsky via R-devel wrote:
> Dear All,
>
> Perhaps this should go in r-package-devel, but I suspect that this is
> going to turn into a feature request, and I want to run it by the list
> before filing it in the Bugzilla.
>
> I would like to specify a long string literal without making the line
> of code too long. In R,
>
> "abc
> def"
>
> yields the string "abc\def", and, as far as I can tell, there is no
> mechanism for preventing it from inserting a newline into the string.
>
> Putting a backslash before the newline, i.e.,
>
> "abc\
> def"
>
> eliminates the newline in (that I know of) C/C++, Python, and Julia,
> but it makes no difference in R.
>
> The implicit concatenation of Python and C/C++, e.g., "abc" "def", is a
> syntax error as well in R.
>
> It is, of course, possible to use paste0(), but is there a more concise
> built-in mechanism in R of which I am not aware?
>
> If not, I think it would make sense to bring R in line with the others.
> Currently, backslash and no backslash before a newline behave
> identically (at least as far as I can tell), so I doubt that a
> nontrivial amount of code relies on the current behaviour. [1]

What would be real example of a long string literal you would want to 
enter this way?

For entering a long text with newlines, one can use raw strings in R 
(see ?Quotes) - but there you would see the newlines and indentation. 
I've seen code where? "paste0" has been aliased to a local function 
named with a single letter to make concatenation more concise.

Best
Tomas

>
> 				Any thoughts?
> 				Pavel
>
> [1] On the off chance that it does, it should easy to check by
> searching for "\\\n" in package sources, because a backslash before a
> newline is a syntax error outside a string.
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel