[Rd] Questions on "\" vs "/" on Windows

Gabor Grothendieck ggrothendieck at gmail.com
Sat Aug 20 16:13:35 CEST 2005


On 8/20/05, Seth Falcon <sfalcon at fhcrc.org> wrote:
> A recent thread on R-help reminded me of some questions I have
> regarding the path separator on Windows.
> 
> The thread: [R] using paste and "\" to create a valid filename
> 
> The question:
> 
> What are the use-cases where "\" is required for paths passed as
> character vectors from within R?

I think Windows itself allows / but some programs require \.

> 
> My experience has been that "/" always works and "\" often fails due
> to escaping issues (the user's fault).  A pathalogical example
> that I _have_ encountered due to temp file naming on Windows:
> 
>  > badpath <- "foo\\2\\bar"
>  > root <- "c:\\HERE\\file.txt"
>  > gsub("HERE", badpath, root)
>  Error in gsub("HERE", badpath, root) : invalid backreference 2 in regular expression
> 
> Using file.path is recommended as a best practice, but AFAICT, it
> forces "\" on Windows.

Actually it uses /. On my Windows XP system:

> file.path("a", "b")
[1] "a/b"
> R.version.string
[1] "R version 2.1.1, 2005-06-23"
 

> 
> Why not have file.path dynamically read .Platform$file.sep
> so that in code that uses file.path one could modify
> .Platform$file.sep and change the behavior of all subsequent file.path
> calls?
> 
> Python's os.path.join function behaves similarly to R's: changing
> os.sep doesn't change the behavior of os.path.join.
> .Platform$file.sep = "/" and change the behavior of all subsequent
> calls.
> 
> With both languages, I've found that for glue-code type tasks,
> sticking to "/" on Windows is much easier and I've been frustrated by
> the built-in path handling function.
> 
> + seth



More information about the R-devel mailing list