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 FRANCE
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 > FRANCEIt 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, Ted. -------------------------------------------------------------------- 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 ------------------------------
On Wed, 4 Jan 2006, yvonnick noel wrote:> Hello, > > I've seen this question asked in the archives but no clear reply or solution > provided.I've seen the answer many times: what were you searching on?> So, just to be sure it is not possible in R: Can I replace > backslashes with slashes in a string ?Yes. The Windows R code does it in several places.> 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.Actually, read.table accepts any valid file path, so I don't see why.> I would have expected something like: > > gsub("\\","\/","c:\My Documents\data.dat") > to work but it does not (incorrect regular expression).You need one of gsub("\\\\","/","c:\\My Documents\\data.dat") gsub("\\","/","c:\\My Documents\\data.dat", fixed = TRUE) chartr("\\", "/", "c:\\My Documents\\data.dat") Note that you need to be careful in certain multi-byte locales to change characters not bytes, but nowadays (post 2.1.0) R is careful.> Note that I have no control on the string which is returned from the > system (no such problem under Linux BTW).Really? What happens with file names containing backslashes on Linux? The string will have embedded backslashes, represented by \\ in quoted strings in R ...> Any idea ? > > Yvonnick NOEL > U. of Rennes 2 > FRANCE-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Try > name<-"C:\\myfile.txt" > name [1] "C:\\myfile.txt" > gsub("([\\])","\\/",name) [1] "C:/myfile.txt" yvonnick noel <yvonnick.noel@uhb.fr> 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 FRANCE ______________________________________________ R-help@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 --------------------------------- Ring in the New Year with Photo Calendars. Add photos, events, holidays, whatever. [[alternative HTML version deleted]]
> It would work with > > gsub("\\\\","/","c:\\My Documents\\data.dat") > [1] "c:/My Documents/data.dat" > > which of course is not your case :(Absolutely. What I get from the interface is the string "c:\My Documents\data.dat" and NOT the string "c:\\My Documents\\data.dat". That is why the solutions previously proposed do not solve my problem (but thank you for your replies !). Yvonnick NOEL U. of Rennes 2 FRANCE
Hi Yvonnick, On 4 Jan 2006, yvonnick.noel at uhb.fr wrote:> 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.Can you provide some further detail on how the string is created? I'm not familiar with Rpad, but I wonder if the issue isn't at that level. Note that if you create a string with single '\', R eats them up and at that point there is nothing you can do: s <- "x\y\z" s [1] "xyz" Do you have a way of printing your path string? + seth
It is often convenient to quickly set the working directory to a path copied onto the windows clipboard. A simple trick I have been using for a while is along the lines given in the previous posts. setwd.clip<-function() { options(warn=-1) setwd(gsub("\\\\","/",readLines("clipboard"))) options(warn=0) getwd() } I load this at the start of every session and then write setwd.clip() whenever I have a path I want to change to on the clipboard. You can of course write setwd(gsub("\\\\","/",readLines("clipboard"))) everytime you need it. Obviously it takes longer and there is the minor detail that the path read from the clipboard is incomplete (no EOL marker) which leads to an unnecessary warning. Dr Duncan Golicher Ecologia y Sistematica Terrestre Conservaci??n de la Biodiversidad El Colegio de la Frontera Sur San Cristobal de Las Casas, Chiapas, Mexico Email: dgoliche at sclc.ecosur.mx Tel: 967 674 9000 ext 1310 Fax: 967 678 2322 Celular: 044 9671041021 United Kingdom Skypein; 020 7870 6251 Skype name: duncangolicher Download Skype from http://www.skype.com
Berton Gunter
2006-Jan-05 19:11 UTC
[R] Setting the working directory on Windows (was: Replacing backslashes with slashes)
In a similar vein, a GUI version is: setwd(dirname(choose.files())) This gives you a standard Windows file browser -- you just click on any file in the directory you want to set. Obviously, dirname(choose.files()) is an easy interactive way to get directories as strings if you need them. See also ?basename . -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Duncan Golicher > Sent: Thursday, January 05, 2006 10:11 AM > To: r-help at stat.math.ethz.ch > Subject: Re: [R] Replacing backslashes with slashes > > It is often convenient to quickly set the working directory > to a path > copied onto the windows clipboard. A simple trick I have been > using for > a while is along the lines given in the previous posts. > > setwd.clip<-function() > { > options(warn=-1) > setwd(gsub("\\\\","/",readLines("clipboard"))) > options(warn=0) > getwd() > } > > > I load this at the start of every session and then write setwd.clip() > whenever I have a path I want to change to on the clipboard. > You can of > course write > > setwd(gsub("\\\\","/",readLines("clipboard"))) > > everytime you need it. Obviously it takes longer and there is > the minor > detail that the path read from the clipboard is incomplete (no EOL > marker) which leads to an unnecessary warning. > > > Dr Duncan Golicher > Ecologia y Sistematica Terrestre > Conservaci??n de la Biodiversidad > El Colegio de la Frontera Sur > San Cristobal de Las Casas, > Chiapas, Mexico > > Email: dgoliche at sclc.ecosur.mx > > Tel: 967 674 9000 ext 1310 > Fax: 967 678 2322 > Celular: 044 9671041021 > > United Kingdom Skypein; 020 7870 6251 > Skype name: duncangolicher > Download Skype from http://www.skype.com > > ______________________________________________ > 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 >