[R] url prep function (backslash issue)

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 30 15:00:22 CEST 2011


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



More information about the R-help mailing list