1
#' Execute code with a modified search path
2
#'
3
#' `with_package()` attaches a package to the search path, executes the code, then
4
#' removes the package from the search path. The package namespace is _not_
5
#' unloaded however. `with_namespace()` does the same thing, but attaches the
6
#' package namespace to the search path, so all objects (even unexported ones) are also
7
#' available on the search path.
8
#' @param package \code{[character(1)]}\cr package name to load.
9
#' @param env \code{[environment()]}\cr Environment to attach.
10
#' @param .local_envir `[environment]`\cr The environment to use for scoping.
11
#' @inheritParams defer
12
#' @inheritParams base::library
13
#' @template with
14
#' @examples
15
#' \dontrun{
16
#' with_package("ggplot2", {
17
#'   ggplot(mtcars) + geom_point(aes(wt, hp))
18
#' })
19
#' }
20
#' @export
21
with_package <- function(package, code, pos = 2, lib.loc = NULL,
22
  character.only = TRUE, logical.return = FALSE, warn.conflicts = FALSE,
23
  quietly = TRUE, verbose = getOption("verbose")) {
24

25
  # Only try to attach (and detach) the package if it is not already attached.
26 1
  if (!(package %in% .packages())) {
27 1
    suppressPackageStartupMessages(
28 1
      (get("library"))(package, pos = pos, lib.loc = lib.loc,
29 1
        character.only = character.only, logical.return = logical.return,
30 1
        warn.conflicts = warn.conflicts, quietly = quietly, verbose = verbose))
31

32 1
    on.exit(detach(paste0("package:", package), character.only = TRUE))
33
  }
34

35 1
  force(code)
36
}
37

38
#' @rdname with_package
39
#' @export
40
local_package <- function(package, pos = 2, lib.loc = NULL,
41
  character.only = TRUE, logical.return = FALSE, warn.conflicts = FALSE,
42
  quietly = TRUE, verbose = getOption("verbose"),
43
  .local_envir = parent.frame()) {
44

45 1
  suppressPackageStartupMessages(
46 1
    (get("library"))(package, pos = pos, lib.loc = lib.loc,
47 1
      character.only = character.only, logical.return = logical.return,
48 1
      warn.conflicts = warn.conflicts, quietly = quietly, verbose = verbose))
49

50 1
  defer(detach(paste0("package:", package), character.only = TRUE), envir = .local_envir)
51
}
52

53
#' @rdname with_package
54
#' @export
55
with_namespace <- function(package, code, warn.conflicts = FALSE) {
56 1
  ns <- asNamespace(package)
57 1
  name <- format(ns)
58 1
  (get("attach"))(ns, name = name, warn.conflicts = FALSE)
59 1
  on.exit(detach(name, character.only = TRUE))
60 1
  force(code)
61
}
62

63
#' @rdname with_package
64
#' @export
65
local_namespace <- function(package, .local_envir = parent.frame(), warn.conflicts = FALSE) {
66 1
  ns <- asNamespace(package)
67 1
  name <- format(ns)
68 1
  (get("attach"))(ns, name = name, warn.conflicts = FALSE)
69 1
  defer(detach(name, character.only = TRUE), envir = .local_envir)
70
}
71

72
#' @rdname with_package
73
#' @inheritParams base::attach
74
#' @export
75
with_environment <- function(env, code, pos = 2L, name = format(env),
76
  warn.conflicts = FALSE) {
77 1
  (get("attach"))(env, name = name)
78 1
  on.exit(detach(name, character.only = TRUE))
79 1
  force(code)
80
}
81

82
#' @rdname with_package
83
#' @export
84
local_environment <- function(env, pos = 2L, name = format(env),
85
  warn.conflicts = FALSE, .local_envir = parent.frame()) {
86 1
  (get("attach"))(env, name = name)
87 1
  defer(detach(name, character.only = TRUE), envir = .local_envir)
88
}

Read our documentation on viewing source code .

Loading