Split a path into directory components

Share:

Description

split_path splits a character vector of paths into directory components. The opposite of file.path. split_dir is a convenience wrapper equivalent to dir + split_path, making it easy to View directory contents.

Usage

1
2
3
4
split_path(x = dir(), simplify = FALSE)

split_dir(x = ".", pattern = NULL, all.files = TRUE, recursive = TRUE,
  simplify = TRUE)

Arguments

x

A character vector. For split_path, this should contain file paths, and it defaults to files in the current directory. For split_dir, this should contain directory paths, and it defaults to the current directory.

simplify

A logical value. If TRUE, the return value is simplified from a list to a matrix.

pattern

A string containing a regular expression, to filter the files that are returned. Passed to dir.

all.files

Logical. If TRUE, files whose name starts with a dot are included. Passed to dir.

recursive

Logical. If TRUE, files in subdirectories are included. Passed to dir.

Value

Either a named list of character vectors containing the split paths, or a matrix. See simplify argument in Usage section.

Note

Paths are split on forward and back slashes, except for double forward or back slashes at the start of (UNC) paths. These are included in the first element of that split path.

See Also

file.path, dir

Examples

1
2
3
4
5
6
7
8
(splits <- split_path(c(getwd(), "~", r_home())))
# Reverse the operation
sapply(splits, paste, collapse = "/")

base_r_files <- split_dir(R.home(), pattern = "\\.R$")

  # Viewing not needed for testing purposes
  utils::View(base_r_files)