Function to concatenate paths?

Adam Ryczkowski picture Adam Ryczkowski · Oct 28, 2012 · Viewed 38.1k times · Source

Is there an existing function to concatenate paths?

I know it is not that difficult to implement, but still... besides taking care of trailing / (or \) I would need to take care of proper OS path format detection (i.e. whether we write C:\dir\file or /dir/file).

As I said, I believe I know how to implement it; the question is: should I do it? Does the functionality already exist in existing R package?

Answer

Dirk Eddelbuettel picture Dirk Eddelbuettel · Oct 28, 2012

Yes, file.path()

R> file.path("usr", "local", "lib")
[1] "usr/local/lib"
R> 

There is also the equally useful system.path() for files in a package:

R> system.file("extdata", "date_time_zonespec.csv", package="RcppBDT")
[1] "/usr/local/lib/R/site-library/RcppBDT/extdata/date_time_zonespec.csv"
R> 

which will get the file extdata/date_time_zonespec.csv irrespective of

  1. where the package is installed, and
  2. the OS

which is very handy. Lastly, there is also

R> .Platform$file.sep
[1] "/"
R> 

if you insist on doing it manually.