| 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( |
| 22 |
package, |
| 23 |
code, |
| 24 |
pos = 2, |
| 25 |
lib.loc = NULL, |
| 26 |
character.only = TRUE, |
| 27 |
logical.return = FALSE, |
| 28 |
warn.conflicts = FALSE, |
| 29 |
quietly = TRUE, |
| 30 |
verbose = getOption("verbose") |
| 31 |
) { |
| 32 |
# Only try to attach (and detach) the package if it is not already attached. |
| 33 |
if (!(package %in% .packages())) { |
| 34 |
suppressPackageStartupMessages( |
| 35 |
(get("library"))( |
| 36 |
package, |
| 37 |
pos = pos, |
| 38 |
lib.loc = lib.loc, |
| 39 |
character.only = character.only, |
| 40 |
logical.return = logical.return, |
| 41 |
warn.conflicts = warn.conflicts, |
| 42 |
quietly = quietly, |
| 43 |
verbose = verbose |
| 44 |
) |
| 45 |
) |
| 46 |
|
| 47 |
on.exit(detach(paste0("package:", package), character.only = TRUE)) |
| 48 |
} |
| 49 |
|
| 50 |
force(code) |
| 51 |
} |
| 52 |
|
| 53 |
#' @rdname with_package |
| 54 |
#' @export |
| 55 |
local_package <- function( |
| 56 |
package, |
| 57 |
pos = 2, |
| 58 |
lib.loc = NULL, |
| 59 |
character.only = TRUE, |
| 60 |
logical.return = FALSE, |
| 61 |
warn.conflicts = FALSE, |
| 62 |
quietly = TRUE, |
| 63 |
verbose = getOption("verbose"), |
| 64 |
.local_envir = parent.frame() |
| 65 |
) { |
| 66 |
suppressPackageStartupMessages( |
| 67 |
(get("library"))( |
| 68 |
package, |
| 69 |
pos = pos, |
| 70 |
lib.loc = lib.loc, |
| 71 |
character.only = character.only, |
| 72 |
logical.return = logical.return, |
| 73 |
warn.conflicts = warn.conflicts, |
| 74 |
quietly = quietly, |
| 75 |
verbose = verbose |
| 76 |
) |
| 77 |
) |
| 78 |
|
| 79 |
defer( |
| 80 |
detach(paste0("package:", package), character.only = TRUE), |
| 81 |
envir = .local_envir |
| 82 |
) |
| 83 |
} |
| 84 |
|
| 85 |
#' @rdname with_package |
| 86 |
#' @export |
| 87 |
with_namespace <- function(package, code, warn.conflicts = FALSE) { |
| 88 |
ns <- asNamespace(package) |
| 89 |
name <- format(ns) |
| 90 |
(get("attach"))(ns, name = name, warn.conflicts = warn.conflicts) |
| 91 |
on.exit(detach(name, character.only = TRUE)) |
| 92 |
force(code) |
| 93 |
} |
| 94 |
|
| 95 |
#' @rdname with_package |
| 96 |
#' @export |
| 97 |
local_namespace <- function( |
| 98 |
package, |
| 99 |
.local_envir = parent.frame(), |
| 100 |
warn.conflicts = FALSE |
| 101 |
) { |
| 102 |
ns <- asNamespace(package) |
| 103 |
name <- format(ns) |
| 104 |
(get("attach"))(ns, name = name, warn.conflicts = warn.conflicts) |
| 105 |
defer(detach(name, character.only = TRUE), envir = .local_envir) |
| 106 |
} |
| 107 |
|
| 108 |
#' @rdname with_package |
| 109 |
#' @inheritParams base::attach |
| 110 |
#' @export |
| 111 |
with_environment <- function( |
| 112 |
env, |
| 113 |
code, |
| 114 |
pos = 2L, |
| 115 |
name = format(env), |
| 116 |
warn.conflicts = FALSE |
| 117 |
) { |
| 118 |
(get("attach"))(env, name = name, pos = pos, warn.conflicts = warn.conflicts) |
| 119 |
on.exit(detach(name, character.only = TRUE)) |
| 120 |
force(code) |
| 121 |
} |
| 122 |
|
| 123 |
#' @rdname with_package |
| 124 |
#' @export |
| 125 |
local_environment <- function( |
| 126 |
env, |
| 127 |
pos = 2L, |
| 128 |
name = format(env), |
| 129 |
warn.conflicts = FALSE, |
| 130 |
.local_envir = parent.frame() |
| 131 |
) { |
| 132 |
(get("attach"))(env, name = name, pos = pos, warn.conflicts = warn.conflicts) |
| 133 |
defer(detach(name, character.only = TRUE), envir = .local_envir) |
| 134 |
} |