File: path.R

package info (click to toggle)
r-cran-withr 3.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: sh: 13; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# path -----------------------------------------------------------------------

get_path <- function(...) {
  strsplit(Sys.getenv("PATH"), .Platform$path.sep)[[1]]
}

set_path <- function(path, action = c("prefix", "suffix", "replace")) {
  action <- match.arg(action)

  path <- as_character(path)
  path <- normalizePath(path, mustWork = FALSE)

  old <- get_path()
  path <- merge_new(old, path, action)

  path <- paste(path, collapse = .Platform$path.sep)
  Sys.setenv(PATH = path)
  invisible(old)
}

#' PATH environment variable
#'
#' Temporarily change the system search path.
#'
#' @template with
#' @param new `[character]`\cr New `PATH` entries
#' @param action `[character(1)]`\cr Should new values `"replace"`, `"prefix"`
#'   (the default) or `"suffix"` existing paths
#' @inheritParams with_collate
#' @seealso [Sys.setenv()]
#' @examples
#' # temporarily modify the system PATH, *prefixing* the current path
#' with_path(getwd(), Sys.getenv("PATH"))

#' # temporarily modify the system PATH, *appending* to the current path
#' with_path(getwd(), Sys.getenv("PATH"), "suffix")
#' @export
with_path <- with_(
  set_path,
  reset = function(old) set_path(old, "replace"),
  get = get_path
)

#' @rdname with_path
#' @export
local_path <- local_(
  set_path,
  reset = function(old) set_path(old, "replace"),
  get = get_path
)