[R] pasting "\" into character strings

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Sun Dec 22 09:27:05 CET 2002


Why guess?  It's in the R Language definition and in all good books on
S/R.

`String constants are delimited by a pair of single (') or double (")
quotes and can contain all other printable characters. Quotes and other
special characters within strings are specified using escape sequences:'

paste("c:",  "work", "part1.txt", sep="\\")



On Sun, 22 Dec 2002, John Miyamoto wrote:

> Dear R-Help,
>    I'm using R version 1.6.0 on a Windows computer.  I am trying to create
> a function that, among other things, constructs strings that refer to
> Windows files, e.g., I might want to construct a string like
> 'c:\work\part1.txt'.  I have found that the following does not work.
>
> > paste("c:", "\", "work", "\", "part1.txt", sep="")
> Error: syntax error
>
> I'm guessing that R interprets "\" as some kind of special control
> character, and that there is some way to show that one wants a literal
> interpretation of "\" and not a control interpretation, but I haven't been
> able to find an explanation of this issue.  I understand that I must use
> 'c:\\work\\part1.txt' or 'c:/work/part1.txt' to refer to the file that
> Windows knows as 'c:\work\part.txt', but what I'm trying to do is to
> write an R function that writes references to Windows files into a text
> file, where a different Windows programs will later read these references
> in the standard Windows syntax.

c:/work/part1.txt *is* standard Windows syntax, too!

>    Can someone tell me how to create the character string
> 'c:\work\part1.txt' from the parts, "c:", "work", an "part1.txt"?


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list