Run Code 'With' Temporarily Modified Global 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.


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")
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

News

1.0.2

  • with_makevars() gains an assignment argument to allow specifying additional assignment types.

1.0.1

  • Relaxed R version requirement to 3.0.2 (#35, #39).
  • New with_output_sink() and with_message_sink() (#24).

1.0.0

First Public Release

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("withr")

2.0.0 by Jim Hester, 3 months ago


http://github.com/r-lib/withr#readme


Report a bug at http://github.com/r-lib/withr/issues


Browse source code at https://github.com/cran/withr


Authors: Jim Hester [aut, cre], Kirill M<U+00FC>ller [aut], Kevin Ushey [aut], Hadley Wickham [aut], Winston Chang [aut], RStudio [cph]


Documentation:   PDF Manual  


GPL (>= 2) license


Imports stats, graphics

Suggests testthat


Imported by DBItest, batchtools, caret, covr, cyclocomp, devtools, drake, flippant, gdtools, learnr, prettycode, rcmdcheck, sessioninfo, sparklyr, usethis.

Suggested by IRdisplay, bigrquery, cranlike, crayon, debugme, desc, dplyr, ggExtra, harvestr, keyring, liteq, miniCRAN, pathological, processx, rprojroot, secret, surveydata, testthat, tibble, zip.


See at CRAN