[Rd] [WISH / PATCH] possibility to split string literals across multiple lines

Andreas Kersting r-devel at akersting.de
Wed Jun 14 13:45:37 CEST 2017


On Wed, 14 Jun 2017 06:12:09 -0500, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:

> On 14/06/2017 5:58 AM, Andreas Kersting wrote:
> > Hi,
> >
> > I would really like to have a way to split long string literals across
> > multiple lines in R.
> 
> I don't understand why you require the string to be a literal.  Why not 
> construct the long string in an expression like
> 
>   paste0("aaa",
>          "bbb")
> 
> ?  Surely the execution time of the paste0 call is negligible.
> 
> Duncan Murdoch

Actually "execution time" is precisely one of the reasons why I would like to see this feature as - depending on the context (e.g. in a tight loop) - the execution time of paste0 (or probably also glue, thanks Gabor) is not necessarily insignificant. 

The other reason is style: I think it is cleaner if we can construct such a long string literal without the need for a function call.

Andreas

> >
> > Currently, if a string literal spans multiple lines, there is no way to
> > inhibit the introduction of newline characters:
> >
> >  > "aaa
> > + bbb"
> > [1] "aaa\nbbb"
> >
> >
> > If a line ends with a backslash, it is just ignored:
> >
> >  > "aaa\
> > + bbb"
> > [1] "aaa\nbbb"
> >
> >
> > We could use this fact to implement string splitting in a fairly
> > backward-compatible way, since currently such trailing backslashes
> > should hardly be used as they do not have any effect. The attached patch
> > makes the parser ignore a newline character directly following a backslash:
> >
> >  > "aaa\
> > + bbb"
> > [1] "aaabbb"
> >
> >
> > I personally would also prefer if leading blanks (spaces and tabs) in
> > the second line are ignored to allow for proper indentation:
> >
> >  >   "aaa \
> > +    bbb"
> > [1] "aaa bbb"
> >
> >  >   "aaa\
> > +    \ bbb"
> > [1] "aaa bbb"
> >
> > This is also implemented by this patch.
> >
> >
> > An alternative approach could be to have something like
> >
> > ("aaa "
> > "bbb")
> >
> > or
> >
> > ("aaa ",
> > "bbb")
> >
> > be interpreted as "aaa bbb".
> >
> > I don't know the ins and outs of the parser of R (hence: please very
> > carefully review the attached patch), but I guess this would be more
> > work to implement!?
> >
> >
> > What do you think? Is there anybody else who is missing this feature in
> > the first place?
> >
> > Regards,
> > Andreas
> >
> >
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >



More information about the R-devel mailing list