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
state. Each with_
function has a local_
variant, which instead resets
the state when the current evaluation context ends (such as the end of a
function).
Arguments pattern
new | [various] | Values for setting |
code | [any] | Code to execute in the temporary environment |
... | Further arguments |
withr functions
with_collate()
: collation orderwith_dir()
: working directorywith_envvar()
: environment variableswith_libpaths()
: library paths, replacing current libpathswith_locale()
: any locale settingwith_makevars()
: Makevars variableswith_options()
: optionswith_par()
: graphics parameterswith_path()
:PATH
environment variablewith_sink()
: output redirection
Creating new "with" functions
All 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.
Author
Maintainer: Lionel Henry lionel@posit.co
Authors:
Jim Hester
Kirill Müller krlmlr+r@mailbox.org
Kevin Ushey kevinushey@gmail.com
Hadley Wickham hadley@posit.co
Winston Chang
Other contributors:
Jennifer Bryan [contributor]
Richard Cotton [contributor]
Posit Software, PBC [copyright holder, funder]
Examples
getwd()
#> [1] "/home/runner/work/withr/withr/docs/reference"
with_dir(tempdir(), getwd())
#> [1] "/tmp/RtmpcHnEKr"
getwd()
#> [1] "/home/runner/work/withr/withr/docs/reference"
Sys.getenv("WITHR")
#> [1] ""
with_envvar(c("WITHR" = 2), Sys.getenv("WITHR"))
#> [1] "2"
Sys.getenv("WITHR")
#> [1] ""
with_envvar(c("A" = 1),
with_envvar(c("A" = 2), action = "suffix", Sys.getenv("A"))
)
#> [1] "1 2"
# local variants are best used within other functions
f <- function(x) {
local_envvar(c("WITHR" = 2))
Sys.getenv("WITHR")
}
Sys.getenv("WITHR")
#> [1] ""