Debian Codegraph @

R/path.R

raw
1 # path -----------------------------------------------------------------------
2
3 get_path <- function(...) {
4 strsplit(Sys.getenv("PATH"), .Platform$path.sep)[[1]]
5 }
6
7 set_path <- function(path, action = c("prefix", "suffix", "replace")) {
8 action <- match.arg(action)
9
10 path <- as_character(path)
11 path <- normalizePath(path, mustWork = FALSE)
12
13 old <- get_path()
14 path <- merge_new(old, path, action)
15
16 path <- paste(path, collapse = .Platform$path.sep)
17 Sys.setenv(PATH = path)
18 invisible(old)
19 }
20
21 #' PATH environment variable
22 #'
23 #' Temporarily change the system search path.
24 #'
25 #' @template with
26 #' @param new `[character]`\cr New `PATH` entries
27 #' @param action `[character(1)]`\cr Should new values `"replace"`, `"prefix"`
28 #' (the default) or `"suffix"` existing paths
29 #' @inheritParams with_collate
30 #' @seealso [Sys.setenv()]
31 #' @examples
32 #' # temporarily modify the system PATH, *prefixing* the current path
33 #' with_path(getwd(), Sys.getenv("PATH"))
34
35 #' # temporarily modify the system PATH, *appending* to the current path
36 #' with_path(getwd(), Sys.getenv("PATH"), "suffix")
37 #' @export
38 with_path <- with_(
39 set_path,
40 reset = function(old) set_path(old, "replace"),
41 get = get_path
42 )
43
44 #' @rdname with_path
45 #' @export
46 local_path <- local_(
47 set_path,
48 reset = function(old) set_path(old, "replace"),
49 get = get_path
50 )