[R] \ escape sequence and windows path

Duncan Murdoch murdoch.duncan at gmail.com
Tue May 20 19:03:09 CEST 2014


On 20/05/2014 12:04 PM, Knut Krueger wrote:
> Am 20.05.2014 17:32, schrieb Bert Gunter:
>
> >> paste("a","b",sep="\\") ## "\\" is the escaped single backslash
> > [1] "a\\b"
> >> cat(paste("a","b",sep="\\"))
> > a\b
> >
> > Does this help clarify? Or have i misunderstood you?
>
> @David and @Bert
>
> unfortunately yes. My question is more a system level question as about
> any converting functions.
>
> The question is how to paste a windows path directly into an R script
> (without readline() or readClipboard() and how could I convert this
> string that it is usable in R. Means any function which is not using
> escape characters
>
> it seems to be possible for readline() an  readClipboard().
>
> I do not want to use y=readline() I do want to use
> y=foo("c:\foo1\foo2\") but this seems to be impossible because R is
> interpreting one backslash as escape sequence. So how does readline()
> and readClipboard() are working? Is there any other callback from utils
> which is able to deal with this?

The difference between the two cases is that you are trying to write 
something in the R language when you write

y=foo("c:\foo1\foo2\")

but that is not a legal R statement, since you never close the opening 
quote.  When using readLines() etc., you're just reading data, you're 
not trying to parse it as R code.

I have no idea what you mean by a "callback from utils".

If you want to solve the problem "I have a pathname in the clipboard, 
and want to put it in a string", it's not hard to write
a function that essentially does readLines("clipboard").  It's a little 
harder to make that platform-neutral, but it sounds as if you're only 
working on Windows.

If you want to solve the problem, "I want to write some R source code 
that contains a pathname, but I don't want to escape the backslashes", 
then I think you're out of luck.  There are ways to read data inline in 
your source, e.g. you can paste

f <- scan(what="character")
c:\foo1\foo2\


into your console to read x, but that doesn't work with source(), so I'd 
avoid it.

Duncan Murdoch

>
> I am afraid there is no way.
>
> The reason is to prevent errors for beginning windows R user. If the
> path is long they either forget to convert one backslash or all and then
> they are frustrated that it is not working. And telling them to use
> search and replace ... Whatever they would replace it might be worse
> than before ;-)
>
> regards Knut
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list