[R] Replacing backslashes with slashes

yvonnick noel yvonnick.noel at uhb.fr
Wed Jan 4 20:11:04 CET 2006


> For example, if I make a file names.txt with contents
>  c:\My Documents\data.dat
> (even without quotes) then
>  A<-readLines("names.txt",n=1)
>  > A
>  [1] "c:\\My Documents\\data.dat"
> so the result is now in the format such that gsub will work.
> E.g.
>  gsub("\\\\","/",readLines("names.txt",n=1))
>  [1] "c:/My Documents/data.dat"

Yes. I agree that this work. But I am sure you'll agree that it is not 
a simple
way to get double backslashes ! But it looks like it is the only way to get it
in R. Jim Holtman also suggested a similar solution with scan().

Probably the simplest way to go is to double the backslashes in javascript,
before the string is passed to R.

> So now all that would remain is for you to work out how best
> to get the filename from the web page into the file which is
> read by 'readLines'. One would need more information to work
> out how to do that in the context of your usage.

This would not be a problem. I could use R functions to do this, since Rpad
allows you to mix R code with standard web scripting.

Thank you.


More information about the R-help mailing list