Introduction

set.seed(19790801)
knitr::opts_chunk$set(error = FALSE)

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

tempdir()
R.home()

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()
r_home()

These wrappers are also vectorized.

r_home(c("home", "bin", "share"), c("", "i386", "zoneinfo"))

There are several wrapper functions available in pathological.

wrapper_data <- data.frame(
  "base/utils" = c("choose.files, file.choose", "choose.dir", "dir.create", "file.create", "file.copy", ".libPaths", "R.home", "Sys.which", "system.file", "tempdir", "tempfile"),
  pathological = c("choose_files", "choose_dir", "copy_dir", "create_dirs", "create_files", "get_libraries", "r_home", "sys_which", "system_file", "temp_dir", "temp_file"),
  stringsAsFactors = FALSE
)
wrapper_data$pathological <- paste0(
  "[", wrapper_data$pathological,
  "](https://www.rdocumentation.org/packages/pathlogical/topics/", 
  wrapper_data$pathological, ")"
)
knitr::kable(wrapper_data)

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")
withr::with_dir(
  temp_dir(),
  decompose_path(x)
)

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.

split_path(r_home())
split_path(r_home(), simplify = TRUE)

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.

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.

r_profile_site()
r_profile()

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



Try the pathological package in your browser

Any scripts or data that you put into this service are public.

pathological documentation built on May 29, 2017, 1:41 p.m.