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

Seth Falcon sfalcon at fhcrc.org
Sat Aug 20 16:07:54 CEST 2005


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?

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.  

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