[R] Replacing backslashes with slashes

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Wed Jan 4 15:01:10 CET 2006

On 04-Jan-06 yvonnick noel wrote:
> Hello,
> I've seen this question asked in the archives but no clear reply or
> solution
> provided. So, just to be sure it is not possible in R: Can I replace
> backslashes with slashes in a string ?
> I am writing a GUI for R with the Rpad library. I have a "browse"
> button for
> data loading and Windows return a path string with backslashes. I need
> to
> convert them into slashes to use the string with read.table.
> I would have expected something like:
> gsub("\\","\/","c:\My Documents\data.dat")
> to work but it does not (incorrect regular expression).
> Note that I have no control on the string which is returned from the
> system (no
> such problem under Linux BTW).
> Any idea ?
> Yvonnick NOEL
> U. of Rennes 2

It would work with

gsub("\\\\","/","c:\\My Documents\\data.dat")
[1] "c:/My Documents/data.dat"

which of course is not your case :(

To see what the real problem is, just enter the string itself:

"c:\My Documents\data.dat"
[1] "c:My Documentsdata.dat"

so 'gsub' is not seeing the "\" in the first place.

I don't know what the solution is (though others might),
but that's the problem!

Good luck,

E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 04-Jan-06                                       Time: 14:01:07
------------------------------ XFMail ------------------------------

More information about the R-help mailing list