Search code examples
rpathconcatenation

Function to concatenate paths?


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?


Solution

  • Yes, file.path()

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

    There is also the equally useful system.file() 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.