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
tempdir()
## [1] "C:\\Users\\rjc2003\\AppData\\Local\\Temp\\RtmpGee7V8"
R.home()
## [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.
library(pathological)
temp_dir()
## [1] "C:/Users/rjc2003/AppData/Local/Temp/RtmpGee7V8"
r_home()
## [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.
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")
withr::with_dir(
temp_dir(),
decompose_path(x)
)
## 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
.
get_extension
returns only the file extension.strip_extension
returns the file name without the extension.replace_extension
replaces the file extension with a new one.recompose_path
undoes the effects of decompose_path
.split_path
splits file paths into directory components, returning a list of character vectors, or a matrix.
split_path(r_home())
## $`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.
split_dir(r_home())
A screenshot of the results from the split_dir function, viewed with the RStudio data viewer.
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.
r_profile_site()
## [1] "C:/Program Files/R/R-devel/etc/Rprofile.site"
r_profile()
## [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.
There are a few functions for use with Windows file paths.
is_windows_drive
returns wherever the input paths are a letter followed by a colon, and optionally a forward or back slash.get_windows_drive
returns the drive of the input paths.cygwinify_path
converts a p[ath to a form that cygwin is happy with.rstudio_project_dir
returns the directory of the current project.parent_dir
returns the parent directory of the input.os_path
returns the operating system PATH
environment varaible as a character vector.