Learn R Programming

⚠️There's a newer version (3.0.1) of this package.Take me there.

Withr - Run Code 'With' Modified State

A set of functions to run code 'with' safely and temporarily modified global state.

Many of these functions were originally a part of the devtools package, this provides a simple package with limited dependencies to provide access to these functions.

  • with_collate() - collation order
  • with_dir() - working directory
  • with_envvar() - environment variables
  • with_libpaths() - library paths
  • with_locale() - any locale setting
  • with_makevars() - Makevars variables
  • with_options() - options
  • with_par() - graphics parameters
  • with_path() - PATH environment variable

There is also a with_() function to construct new with_* functions if needed.

dir.create("test")
#> Warning in dir.create("test"): 'test' already exists
getwd()
#> [1] "/tmp/RtmpaPrDI5"
with_dir("test", getwd())
#> [1] "/tmp/RtmpaPrDI5/test"
getwd()
#> [1] "/tmp/RtmpaPrDI5"
unlink("test")

Sys.getenv("HADLEY")
#> [1] ""
with_envvar(c("HADLEY" = 2), Sys.getenv("HADLEY"))
#> [1] "2"
Sys.getenv("HADLEY")
#> [1] ""

with_envvar(c("A" = 1),
  with_envvar(c("A" = 2), action = "suffix", Sys.getenv("A"))
)
#> [1] "1 2"

See Also

Copy Link

Version

Install

install.packages('withr')

Monthly Downloads

1,037,579

Version

1.0.2

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Jim Hester

Last Published

June 20th, 2016

Functions in withr (1.0.2)

withr

Execute code in temporarily altered environment
with_

Create a new "with" function
with_makevars

Makevars variables
with_options

Options
with_sink

Output redirection
with_temp_libpaths

Library paths
with_dir

Working directory
with_envvar

Environment variables
with_libpaths

Library paths
with_locale

Locale settings
with_par

Graphics parameters
with_path

PATH environment variable
with_collate

Collation Order