[R] How to convert backslash to slash?

Shengqiao Li shli at stat.wvu.edu
Wed Sep 24 04:47:12 CEST 2008

On Tue, 23 Sep 2008, Duncan Murdoch wrote:

> On 23/09/2008 4:00 PM, Shengqiao Li wrote:
>> How to use sub, gsub, etc. to replace "\" in a string to "/"?
>> For example,convert "C:\foo\bar" to "C:/foo/bar".
> If those are R strings, there are no backslashes in the first one.  It has a 
> formfeed and a backspace in it.

I did notice that this string  was special. It's a legimate R string. If 
"f" and "b" are replaced by "d", it will not. My purpose is to convert a 
Windows file path (eg. copied from Explorer location bar) to a R file path 
through some R function inside R terminal. The "File->Change dir..." takes 
a file path like "C:\Acer", but setwd function will fail.  I guess there 
must be some ways in R to replace a backslash by slash, eg. C:\Acer -> 
C:/Acer. The first problem may be how to pass and save this kind of 
strings. encodeString does not work for this, it will just ignore "\".

Shengqiao Li

> Duncan Murdoch

More information about the R-help mailing list