[R] Replacing backslashes with slashes

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Jan 4 19:19:12 CET 2006


yvonnick noel <yvonnick.noel at uhb.fr> writes:

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

But what does R see? I.e., what is in the string that gets passed to
R, and how does it get passed?

You sound confused (many are) about character escapes: Notice that
"\\" is a string with *one* character in it, namely the backslash.
"\"" has no backslash inside; it contains one double-quote character,
etc. The first backslash is just for representation - it is used to
print or specify a string with a quote, a newline, a bell character,
or the backslash escape character itself.

In general, if you read a string from a Tcl/Tk form or text widget or
whatever using the tcltk package, everything should be OK. However, if
the string is somehow being parsed by R, the *interface* has a problem
and needs to do some escaping first.
 

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list