Learn R Programming

fs (version 1.2.2)

path_math: Path computations

Description

All functions apart from path_expand() and path_real() are purely path computations, so the files in question do not need to exist on the filesystem.

Usage

path_real(path)

path_split(path)

path_join(parts)

path_abs(path, start = ".")

path_norm(path)

path_rel(path, start = ".")

path_common(path)

Arguments

path

A character vector of one or more paths.

parts

A list of character vectors, corresponding to split paths.

start

A starting directory to compute the path to.

Value

The new path as a character vector. For path_split(), a list of character vectors of path components is returned instead.

Functions

  • path_real: returns the canonical path, eliminating any symbolic links.

  • path_split: splits paths into parts.

  • path_join: joins parts together. The inverse of path_split(). See path() to concatenate vectorized strings into a path.

  • path_abs: returns a normalized, absolute version of a path.

  • path_norm: collapses redundant separators and up-level references, so A//B, A/B, A/.B and A/foo/../B all become A/B. If one of the paths is a symbolic link, this may change the meaning of the path, in this case one should use path_real() prior to calling path_norm().

  • path_rel: computes the path relative to the start path, which can be either a absolute or relative path.

  • path_common: finds the common parts of two (or more) paths.

Examples

Run this code
# NOT RUN {
# Expand a path
path_expand("~/bin")

dir_create("a")
file_create("a/b")
link_create(path_abs("a"), "c")

# Realize the path
path_real("c/b")

# Split a path
parts <- path_split("a/b")
parts

# Join it together
path_join(parts)

# Find the absolute path
path_abs("..")

# Normalize a path
path_norm("a/../b\\c/.")

# Compute a relative path
path_rel("/foo/abc", "/foo/bar/baz")

# Find the common path between multiple paths
path_common(c("/foo/bar/baz", "/foo/bar/abc", "/foo/xyz/123"))

# Cleanup
dir_delete("a")
link_delete("c")
# }

Run the code above in your browser using DataLab