[R] backslash quirk in paste

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Dec 6 20:33:38 CET 2014

fname = "John"; lname = "Smith"
ans <- paste( fname, " \\ ", lname )
cat( ans)
print( ans )

Note that ans only has one backslash in it, but print gives you a source-suitable string with the escape character.

Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
Sent from my phone. Please excuse my brevity.

On December 6, 2014 11:00:46 AM PST, "Prof J C Nash (U30A)" <nashjc at uottawa.ca> wrote:
>This is NOT critical. It arose due to a fumble fingers when developing
>an R example, but slightly intriguing.
>How could one build a string from substrings with a single backslash
>as separator. Here's the reproducible example:
>fname = "John"; lname = "Smith"
>paste(fname, lname)
>paste(fname, lname, sep=" / ")
># BUT there's a glitch with backslash
>paste(fname, lname, sep=" \ ") # because of escaping character
>paste(fname, lname, sep=' \ ')
>paste(fname, lname, sep=' \\ ') # because of escaping character
>bslash <- "\\"
>paste(fname, lname, sep=bslash)
>Possibly the answer is that R never allows a single backslash in its
>strings, but I can imagine possible cases where I might want to output
>such lines, for example, in documenting this.
>Best, JN
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list