[R] Replacing backslashes with slashes

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 4 18:08:12 CET 2006


On Wed, 4 Jan 2006, yvonnick noel wrote:

>> You need one of
>> 
>> gsub("\\\\","/","c:\\My Documents\\data.dat")
>> gsub("\\","/","c:\\My Documents\\data.dat", fixed = TRUE)
>> chartr("\\", "/", "c:\\My Documents\\data.dat")
>
> The string I get is an ASCII string in a web page, through the use of an 
> <INPUT
> type="file" ... > tag (with a "browse" button). This string is caught as is 
> by
> R through the Rpad interface (using tcltk as a mini local webserver).
>
> So it is not manually input by the user. As it appears in a textfield on a 
> web
> page, I could of course ask the user to change it manually and double the
> antislashes. But this is not user-friendly.

You really don't understand what I wrote (and it is in many places in the 
R documentation).  How are you getting that string into R? It does not 
contain doubled backslashes: that is the way C represents backslashes. 
For example

> foo <- readLines(n=1)
c:\My Documents\data.dat
> foo
[1] "c:\\My Documents\\data.dat"

So if you are reading the file path into R, you do not need to double the 
backslashes, only if you are entering it as a character string within 
quotes at the R prompt.

>>> Note that I have no control on the string which is returned from the 
>>> system (no such problem under Linux BTW).
>> 
>> Really?  What happens with file names containing backslashes on Linux?
>
> I just meant no conversion was needed under Linux since pathnames use 
> slashes.

As I did say, they can also use backslashes: it is a perfectly valid 
character in a Linux file name.

> That's why I didn't see this problem until I had my student use the graphical
> interface under Windows.
>
> Thank you,
>
> Yvonnick.
>
>
>

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