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

From: Seth Falcon <sfalcon_at_fhcrc.org>
Date: Sat 20 Aug 2005 - 14:07:54 GMT


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



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun Aug 21 00:11:03 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:16 GMT