[R] How to replace slashes with back slashes

Jan T. Kim jtk at cmp.uea.ac.uk
Wed Jan 19 16:15:12 CET 2005


On Wed, Jan 19, 2005 at 02:50:41PM +0100, Joerg Klausen wrote:

> I am running R2.0.0 under Windows 2000. I am compiling a number of file paths into a simple text file that will be read by some other software we use. Unfortunately, it  can only handle file paths with back slashes (MS Windows convention), and from R, I get file paths with forward slashes. The following didn't work. 
> 
> > gsub('/', '\\', 'c:/dir1/dir2/file.ext')
> [1] "c:dir1dir2file.ext"
> > gsub('/', '\\\\', 'c:/dir1/dir2/file.ext')
> [1] "c:\\dir1\\dir2\\file.ext"
> 
> I have tried to find an answer on R-help, but didn't ;-(

Sometimes, you don't find an(other) answer because you already have
one...  ;-)

In this case,

    gsub('/', '\\\\', 'c:/dir1/dir2/file.ext')

actually does what you want. Notice that print doesn't just dump the
string, it renders it in a representation suitable for the righthand
side in an R assignment. You could convince yourself with

    cat(gsub('/', '\\\\', 'c:/dir1/dir2/file.ext'))

which actually does dump the string.

Greetinx, Jan
-- 
 +- Jan T. Kim -------------------------------------------------------+
 |    *NEW*    email: jtk at cmp.uea.ac.uk                               |
 |    *NEW*    WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*




More information about the R-help mailing list