The function convert an absolute path to a relative path in respect to a reference. The longest common parent directory is taken as reference. Symbolic links are converted to original paths before performing the operation.

abs2rel(path, ref_path, mustWork = NA)

Arguments

path

The path to be converted (if it is not absolute, the current working directory is considered as its parent, and a warning is shown).

ref_path

The reference path to be compared to path to obtain the relative directory. Important: the path is considered as a directory also if it is the path of a file!

mustWork

(optional) logical: if TRUE an error is given if path or ref_path do not exists; if NA (default) then a warning; if FALSE nothing is shown.

Value

The relative path

Note

License: GPL 3.0

References

L. Ranghetti, M. Boschetti, F. Nutini, L. Busetto (2020). "sen2r": An R toolbox for automatically downloading and preprocessing Sentinel-2 satellite data. Computers & Geosciences, 139, 104473. doi:10.1016/j.cageo.2020.104473 , URL: https://sen2r.ranghetti.info/.

Author

Luigi Ranghetti, phD (2017) luigi.ranghetti@gmail.com

Examples

# the reference path
(ref_path <- system.file(package="sf"))
#> [1] "/home/lranghetti/R/x86_64-pc-linux-gnu-library/4.3/sf"
# a path with a common parent with ref_path
(in_path_1 <- system.file(package="stars"))
#> [1] "/home/lranghetti/R/x86_64-pc-linux-gnu-library/4.3/stars"
# a path included in ref_path
(in_path_2 <- system.file("DESCRIPTION", package="sf"))
#> [1] "/home/lranghetti/R/x86_64-pc-linux-gnu-library/4.3/sf/DESCRIPTION"
# a path external to ref_path (in Linux)
(in_path_3 <- system.file(package="base"))
#> [1] "/usr/lib/R/library/base"
# an unexisting path
(in_path_4 <- gsub("sf$","unexistingpackage",ref_path))
#> [1] "/home/lranghetti/R/x86_64-pc-linux-gnu-library/4.3/unexistingpackage"

abs2rel(in_path_1, ref_path)
#> [1] "../stars"

abs2rel(in_path_2, ref_path)
#> [1] "./DESCRIPTION"

suppressWarnings(abs2rel(in_path_3, ref_path))
#> [1] "/usr/lib/R/library/base"

suppressWarnings(abs2rel(in_path_4, ref_path, mustWork=FALSE))
#> [1] "../unexistingpackage"

suppressWarnings(abs2rel(ref_path, ref_path))
#> [1] "."