with_ {withr} | R Documentation |
This function is a "constructor" for with_...
functions. It
is only needed if you want to alter some global state which is
not covered by the existing with_...
functions, see
withr-package for an overview.
with_(set, reset = set, envir = parent.frame())
set |
|
reset |
|
envir |
|
[function(new, code, ...)]
A function with at least two arguments,
new
: New state to use
code
: Code to run in that state.
If there are more arguments to the function passed in set
they are
added to the returned function. If set
does not have arguments,
the returned function only has a code
argument.
with_(setwd) global_stack <- list() set_global_state <- function(state, msg = "Changing global state.") { global_stack <- c(list(state), global_stack) message(msg) state } reset_global_state <- function(state) { old_state <- global_stack[[1]] global_stack <- global_stack[-1] stopifnot(identical(state, old_state)) } with_(set_global_state, reset_global_state)