Convenient Wrappers for Base-R Functions

There are lots of functions in base-R for working with files and directories. Unfortunately, some of them are a little bit annoying to work with. For example, the function names, and the form in which paths are returned, are inconsistent. For example

## [1] "C:\\Users\\rjc2003\\AppData\\Local\\Temp\\RtmpGee7V8"
## [1] "C:/PROGRA~1/R/R-devel"

pathological contains standardized wrappers to these functions. These return absolute, unabbreviated paths. You can choose whether you want back or forward slashes to separate directory levels.

## [1] "C:/Users/rjc2003/AppData/Local/Temp/RtmpGee7V8"
## [1] "C:/Program Files/R/R-devel"

These wrappers are also vectorized.

r_home(c("home", "bin", "share"), c("", "i386", "zoneinfo"))
## [1] "C:/Program Files/R/R-devel"               
## [2] "C:/PROGRA~1/R/R-devel/bin/x64/i386"       
## [3] "C:/Program Files/R/R-devel/share/zoneinfo"

There are several wrapper functions available in pathological.

base.utils pathological
choose.files, file.choose choose_files
choose.dir choose_dir
dir.create copy_dir
file.create create_dirs
file.copy create_files
.libPaths get_libraries
R.home r_home
Sys.which sys_which
system.file system_file
tempdir temp_dir
tempfile temp_file

The underlying function for creating the standardized paths is standardize_path, with standardise_path also available for fans of British English.

Getting Parts of Files

There are several functions for getting a parts of a file name , or parts of a path. decompose_path splits a path into the directory name, file name without extension, and file extension. The results are returned as a data frame, and multiple dots in the file extension are correctly dealt with. In the following example, the with_dir function from the withr package is used to temporarily change the working directory, to better demonstrate the dirname column returned by decompose_path.

x <- c("foo.tgz", "bar.tar.gz", "baz")
##                                                   dirname filename
## foo.tgz    C:/Users/rjc2003/AppData/Local/Temp/RtmpGee7V8      foo
## bar.tar.gz C:/Users/rjc2003/AppData/Local/Temp/RtmpGee7V8      bar
## baz        C:/Users/rjc2003/AppData/Local/Temp/RtmpGee7V8      baz
##            extension
## foo.tgz          tgz
## bar.tar.gz    tar.gz
## baz

There are several convenience functions that retrieve build on decompose_path.

split_path splits file paths into directory components, returning a list of character vectors, or a matrix.

## $`C:/Program Files/R/R-devel`
## [1] "C:"            "Program Files" "R"             "R-devel"
split_path(r_home(), simplify = TRUE)
##                            [,1] [,2]            [,3] [,4]     
## C:/Program Files/R/R-devel "C:" "Program Files" "R"  "R-devel"

split_dir is a shortcut for dir + split_path, with defaults to make it easy to View the contents of a directory structure.

A screenshot of the results from the split_dir function, viewed with the RStudio data viewer.

A screenshot of the results from the split_dir function, viewed with the RStudio data viewer.

Startup files

On startup, you can customize R’s behaviour by running an R Profile script, or setting environment variables in an R Environ script. the ?Startup help page details the convoluted process of how it determines which files to run. To save worrying about the details, pathological has some functions that return the paths to these files. r_profile_site returns the path to the site-level R Profile file. r_profile returns the user-level file. If a suitable file cannot be found, NA is returned.

## [1] "C:/Program Files/R/R-devel/etc/"
## [1] "C:/Users/rjc2003/AppData/Local/Temp/RtmpQpfDaz/Rprofile-devtools"

r_environ_site and r_environ are the equivalent functions for site and user-level R Environ files, respectively.

Windows-Specific Functionality

There are a few functions for use with Windows file paths.

Other Miscellanea