Dear R-helpers 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 ;-( Thanks for helping me, Kind regards, J?rg Dr. J?rg Klausen phone : +41 (0)44 823 41 27 EMPA (134)/GAW/QA-SAC fax : +41 (0)44 821 62 44 ?berlandstrasse 129 mailto: joerg.klausen at empa.ch CH-8600 D?bendorf http://www.empa.ch/gaw Switzerland http://www.empa.ch/gaw/gawsis
try sink("filename") cat(paste(gsub('/', '\\\\', 'c:/dir1/dir2/file.ext'),"\n",gsub('/', '\\\\', 'c:/dir1/dir2/file.ext'),"\n" )) sink() the idea is to use cat, instead of paste you can do a for loop where you cat the string into the sinked file On Wed, 19 Jan 2005, Joerg Klausen wrote:> Dear R-helpers > > 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 ;-( > > Thanks for helping me, > Kind regards, > Jörg > > Dr. Jörg Klausen phone : +41 (0)44 823 41 27 > EMPA (134)/GAW/QA-SAC fax : +41 (0)44 821 62 44 > Überlandstrasse 129 mailto: joerg.klausen at empa.ch > CH-8600 Dübendorf http://www.empa.ch/gaw > Switzerland http://www.empa.ch/gaw/gawsis > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Joerg Klausen wrote:> Dear R-helpers > > 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"The latter worked very well, it's just a matter of print()ing: cat(gsub('/', '\\\\', 'c:/dir1/dir2/file.ext')) Uwe Ligges> I have tried to find an answer on R-help, but didn't ;-( > > Thanks for helping me, > Kind regards, > J?rg > > Dr. J?rg Klausen phone : +41 (0)44 823 41 27 > EMPA (134)/GAW/QA-SAC fax : +41 (0)44 821 62 44 > ?berlandstrasse 129 mailto: joerg.klausen at empa.ch > CH-8600 D?bendorf http://www.empa.ch/gaw > Switzerland http://www.empa.ch/gaw/gawsis > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
The second syntax is correct: > gsub('/', '\\\\', 'c:/dir1/dir2/file.ext') [1] "c:\\dir1\\dir2\\file.ext" Indeed, R escapes backslashes. That is why they are doubled in R strings. However, they appear as single backslashes in oyur text file: > res <- gsub('/', '\\\\', 'c:/dir1/dir2/file.ext') > cat(res, file = "test.txt") > file.show("test.txt", delete.file = TRUE) You got this in your file: c:\dir1\dir2\file.ext thus pretty readable by a Windows app. Best, Philippe Grosjean ..............................................<?}))><........ ) ) ) ) ) ( ( ( ( ( Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( ( Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Pentagone (3D08) ( ( ( ( ( Academie Universitaire Wallonie-Bruxelles ) ) ) ) ) 8, av du Champ de Mars, 7000 Mons, Belgium ( ( ( ( ( ) ) ) ) ) phone: + 32.65.37.34.97, fax: + 32.65.37.30.54 ( ( ( ( ( email: Philippe.Grosjean at umh.ac.be ) ) ) ) ) ( ( ( ( ( web: http://www.umh.ac.be/~econum ) ) ) ) ) http://www.sciviews.org ( ( ( ( ( .............................................................. Joerg Klausen wrote:> Dear R-helpers > > 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 ;-( > > Thanks for helping me, > Kind regards, > J?rg > > Dr. J?rg Klausen phone : +41 (0)44 823 41 27 > EMPA (134)/GAW/QA-SAC fax : +41 (0)44 821 62 44 > ?berlandstrasse 129 mailto: joerg.klausen at empa.ch > CH-8600 D?bendorf http://www.empa.ch/gaw > Switzerland http://www.empa.ch/gaw/gawsis > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
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 >=-----*
Seemingly Similar Threads
- Combine univariate time series
- [Bug 13587] New: Add a --dry-run way to show destination for each item
- directories not correctly recognized rsync-3.0.4
- rsync-ing from two locations with same filenames (at different versions)
- How to calculate the spatial correlation of several files?