path.contract: Contract File Paths

path.contractR Documentation

Contract File Paths

Description

Contract a path name, for example by replacing the user's home directory (if defined on that platform) with a leading tilde.

Usage

path.contract(path, ignore.case = os.windows, home = "~")

Arguments

path

character vector containing one or more path names.

ignore.case

TRUE or FALSE, should case be ignored when replacing the user's home directory?

home

character string, the path to contract against.

Details

On Windows, paths are case insensitive, so something like ‘C:\Users\effective_user\Documents’ is regarded as equivalent to ‘c:\users\effective_user\documents’.

Under Unix-alikes, paths are case sensitive, so something like ‘/home/effective_user’ is not equivalent to ‘/HOME/effective_user’.

By default, path.contract respects each of these behaviours, but can be changed if desired.

Additionally, on Windows, the file separator may be either "/" or "\\", for example ‘C:\Users\effective_user\Documents’ and ‘C:/Users/effective_user/Documents’ would be treated as equivalent (or any mixture of the two, for example ‘C:/Users\effective_user/Documents’).

The 'path names' need not exist nor be valid path names.

Value

A character vector of possibly contracted path names: where the home directory is unknown of none is specified the path is returned unchanged.

Note

Using ./, ../, symbolic links, hard links, or multiple mounts will NOT be resolved before attempting to contract path names. If you wish to resolve these before contracting path names, see normalizePath.

See Also

path.expand, basename, normalizePath, file.path.

Examples

stopifnot(path.contract(path.expand(x <- c("~", "~/foo"))) == x)
# Note that this is not necessarily true the other way around (in Windows)
# simply because the path separator may have changed


tilde <- path.expand("~")
if (tilde == "~") {
    cat("the home directory is unknown or none is specified\n")
} else {
    paths <- file.path(c(tilde, toupper(tilde), tolower(tilde)), "foo")
    print(cbind(
        Path = paths,
        `Contracted Path` = path.contract(paths),
        `Contracted Path (ignoring case)` = path.contract(paths, ignore.case = TRUE),
        `Contracted Path (with case)` = path.contract(paths, ignore.case = FALSE)
    ), quote = FALSE)
}

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.