File: libpaths.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 (73 lines) | stat: -rw-r--r-- 1,644 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#' @include with_.R

# lib ------------------------------------------------------------------------

set_libpaths <- function(paths, action = "replace") {
  paths <- as_character(paths)
  paths <- normalizePath(paths, mustWork = TRUE)

  old <- .libPaths()
  paths <- merge_new(old, paths, action)

  .libPaths(paths)
  invisible(old)
}

get_libpaths <- function(...) {
  .libPaths()
}

set_temp_libpath <- function(action = "prefix") {
  paths <- tempfile("temp_libpath")
  dir.create(paths)
  set_libpaths(paths, action = action)
}

#' Library paths
#'
#' Temporarily change library paths.
#'
#' @template with
#' @param new `[character]`\cr New library paths
#' @param action `[character(1)]`\cr should new values `"replace"`, `"prefix"` or
#'   `"suffix"` existing paths.
#' @inheritParams with_collate
#' @seealso [.libPaths()]
#' @family libpaths
#' @examples
#' .libPaths()
#' new_lib <- tempfile()
#' dir.create(new_lib)
#' with_libpaths(new_lib, print(.libPaths()))
#' unlink(new_lib, recursive = TRUE)
#' @export
with_libpaths <- with_(set_libpaths, .libPaths, get = get_libpaths)

#' @rdname with_libpaths
#' @export
local_libpaths <- local_(set_libpaths, .libPaths, get = get_libpaths)

#' Library paths
#'
#' Temporarily prepend a new temporary directory to the library paths.
#'
#' @template with
#' @seealso [.libPaths()]
#' @inheritParams with_libpaths
#' @family libpaths
#' @export
with_temp_libpaths <- with_(
  set_temp_libpath,
  .libPaths,
  get = get_libpaths,
  new = FALSE
)

#' @rdname with_temp_libpaths
#' @export
local_temp_libpaths <- local_(
  set_temp_libpath,
  .libPaths,
  get = get_libpaths,
  new = FALSE
)