Greeting R Community, I am a windows user so this problem may be specific to windows. I often want to source files from within R such as: C:\Users\Rinker\Desktop\Research & Law\Data\School Data 09-10. To source this file I need to go through the path and replace all the backslashes (\) with forward slashes (/). I usually do this in MS Word using the replace option, however, I'd like to do this in R. Attempting to write a function to do this runs into problems: When I enter the following: readyPath <- function(path){ z <- gsub("\", "/", path) return(z) } I get:>readyPath <- function(path){ + z <- gsub("\", "/", path) + return(z) + } + ...meaning R can't close the sequence (presumably because of the backslash which has special meaning). So I tried (\\): readyPath <- function(path){ z <- gsub("\\", "/", path) return(z) }This allows the function to be stored as an object but I'm not sure if this is correct. When I try the function the backslash gets me again:>readyPath("C:\Users\Rinker\Desktop\Research & Law\Data\School Data 09-10") Error: '\U' used without hex digits in character string starting "C:\U" This is what I'd like the function to return: [1] "C:/Users/Rinker/Desktop/Research & Law/Data/School Data 09-10" I want a function in which I enter a path and it returns the path with backslashes replaced with forward slashes. Is there a way to make a function to do this? Windows 7 user R version 2.14 beta Thank you, Tyler Rinker [[alternative HTML version deleted]]
You seem to be looking for chartr("\\", "/", path) (and FAQ Q7.8) What does any of this have to do with 'url prep': URLs are never written with backslashes? On Tue, 30 Aug 2011, Tyler Rinker wrote:> > > > Greeting R > Community, > > I am a > windows user so this problem may be specific to windows. I often want to source > files from within R > > such as: > C:\Users\Rinker\Desktop\Research & Law\Data\School Data 09-10. To source > this file I need to go > > through the > path and replace all the backslashes (\) with forward slashes (/). I usually do > this in MS Word > > using the > replace option, however, I'd like to do this in R. Attempting to write a > function to do this runs into > > problems: > > When I > enter the following: > > readyPath > <- function(path){ > > z <- gsub("\", "/", path) > > return(z) > > } > > I get: > >> > readyPath <- function(path){ > > + z <- gsub("\", "/", path) > > + return(z) > > + } > > + > > ...meaning > R can't close the sequence (presumably because of the backslash which has > special meaning). > > So I tried > (\\): > > > > readyPath <- function(path){ > > z <- gsub("\\", "/", path) > > return(z) > > }This allows > the function to be stored as an object but I'm not sure if this is correct.It isn't: please do read the help for gsub (\ is a metacharacter).> When I try > the function the backslash gets me again: > >> > readyPath("C:\Users\Rinker\Desktop\Research & Law\Data\School Data > 09-10") > > Error: '\U' used without hex digits in character string starting > "C:\U"You cannot do that: you have to scan a file or escape \> This is > what I'd like the function to return: > > [1] > "C:/Users/Rinker/Desktop/Research & Law/Data/School Data 09-10" > > I want a > function in which I enter a path and it returns the path with backslashes > > replaced > with forward slashes. Is there a way to make a function to do this??normalizePath chartr("\\", "/", path)> Windows 7 > user > > R version > 2.14 beta > > Thank you, > > Tyler > Rinker > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- 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
Brian Ripley told you how to do the translation, but there's another problem: On 30/08/2011 8:14 AM, Tyler Rinker wrote: [ much deleted ]> When I try > the function the backslash gets me again: > > > > readyPath("C:\Users\Rinker\Desktop\Research& Law\Data\School Data > 09-10")The problem is that you haven't entered a string containing backslashes, you've tried to enter a string containing escapes. The parser sees a single backslash and attaches it to the next letter, so \U is taken to be the start of a Unicode character, and you get the error> Error: '\U' used without hex digits in character string starting > "C:\U" >The way around this is to avoid the parser, by something like this: oldstring <- readline() C:\Users\Rinker\Desktop\Research& Law\Data\School Data 09-10 and then applying chartr to oldstring. Duncan Murdoch> This is > what I'd like the function to return: > > [1] > "C:/Users/Rinker/Desktop/Research& Law/Data/School Data 09-10" > > I want a > function in which I enter a path and it returns the path with backslashes > > replaced > with forward slashes. Is there a way to make a function to do this? > > Windows 7 > user > > R version > 2.14 beta > > Thank you, > > Tyler > Rinker > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.