Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

relative_path

Get the relative path of a path relative to a directory


Description

Given a directory, return the relative path that is relative to this directory. For example, the path ‘foo/bar.txt’ relative to the directory ‘foo/’ is ‘bar.txt’, and the path ‘/a/b/c.txt’ relative to ‘/d/e/’ is ‘../../a/b/c.txt’.

Usage

relative_path(x, dir = ".", use.. = TRUE, error = TRUE)

Arguments

x

A vector of paths to be converted to relative paths.

dir

Path to a directory.

use..

Whether to use double-dots (‘..’) in the relative path. A double-dot indicates the parent directory (starting from the directory provided by the dir argument).

error

Whether to signal an error if a path cannot be converted to a relative path.

Value

A vector of relative paths if the conversion succeeded; otherwise the original paths when error = FALSE, and an error when error = TRUE.

Examples

xfun::relative_path("foo/bar.txt", "foo/")
xfun::relative_path("foo/bar/a.txt", "foo/haha/")
xfun::relative_path(getwd())

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.