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

Examples

# the reference path (ref_path <- system.file(package="sen2r"))
#> [1] "/home/lranghetti/share/git/github/ranghetti/sen2r/inst"
# a path with a common parent with ref_path (in_path_1 <- system.file(package="gdalUtils"))
#> [1] "/home/lranghetti/R/x86_64-pc-linux-gnu-library/3.6/gdalUtils"
# a path included in ref_path (in_path_2 <- system.file("R/abs2rel.R", package="sen2r"))
#> [1] "/home/lranghetti/share/git/github/ranghetti/sen2r/R/abs2rel.R"
# 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("sen2r","r2sen",ref_path))
#> [1] "/home/lranghetti/share/git/github/ranghetti/r2sen/inst"
abs2rel(in_path_1, ref_path)
#> [1] "../../../../../../R/x86_64-pc-linux-gnu-library/3.6/gdalUtils"
abs2rel(in_path_2, ref_path)
#> [1] "../R/abs2rel.R"
suppressWarnings(abs2rel(in_path_3, ref_path))
#> [1] "/usr/lib/R/library/base"
suppressWarnings(abs2rel(in_path_4, ref_path, mustWork=FALSE))
#> [1] "../../r2sen/inst"
suppressWarnings(abs2rel(ref_path, ref_path))
#> [1] "."