[R] pasting "\" into character strings

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Sun Dec 22 12:43:03 CET 2002


Oh dear, oh dear, oh dear!

My answer is correct, and it does gives you

c:\work\part1.txt

However, print() (which you have called by auto-printing) escapes the
escape characters.  cat() does not, so you get

> x <- paste("c:",  "work", "part1.txt", sep="\\")
> x
[1] "c:\\work\\part1.txt"
> cat(x, "\n")
c:\work\part1.txt


On Sun, 22 Dec 2002, John Miyamoto wrote:

> On Sun, 22 Dec 2002 ripley at stats.ox.ac.uk wrote:
>
> > 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="\\")
>
> Reply: The problem with this command is that it produces
>
> paste("c:",  "work", "part1.txt", sep="\\")
> [1] "c:\\work\\part1.txt"
>
> whereas I want "c:\work\part1.txt".  Some people have suggested to try
> 'file.path', but it either doesn't work, or I don't know how to make it
> work.
>
> > file.path("c:","work","part1.txt", fsep = "\")
> Error: syntax error
> > file.path("c:","work","part1.txt", fsep = "\\")
> [1] "c:\\work\\part1.txt"
>
> John Miyamoto
>
>

-- 
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