Execute code in temporarily altered environment
All functions prefixed by
with_ work as follows. First, a particular
aspect of the global environment is modified (see below for a list).
Then, custom code (passed via the
code argument) is executed.
Upon completion or error, the global environment is restored to the previous
[various] Values for setting
[any] Code to execute in the temporary environment
... Further arguments
with_...(new, code, ...)
with_collate: collation order
with_dir: working directory
with_envvar: environment variables
with_libpaths: library paths, replacing current libpaths
with_locale: any locale setting
with_makevars: Makevars variables
with_par: graphics parameters
with_sink: output redirection
Creating new "with" functions
with_ functions are created by a helper function,
with_. This functions accepts two arguments:
a setter function and an optional resetter function. The setter function is
expected to change the global state and return an "undo instruction".
This undo instruction is then passed to the resetter function, which changes
back the global state. In many cases, the setter function can be used
naturally as resetter.
getwd() with_dir(tempdir(), getwd()) getwd() Sys.getenv("HADLEY") with_envvar(c("HADLEY" = 2), Sys.getenv("HADLEY")) Sys.getenv("HADLEY") with_envvar(c("A" = 1), with_envvar(c("A" = 2), action = "suffix", Sys.getenv("A")) )