withr v2.0.0

0

Monthly downloads

0th

Percentile

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.

Readme

Withr - Run Code 'With' Modified State

Travis-CI Build Status AppVeyor Build Status Coverage Status CRAN Version

A set of functions to run code 'with' safely and temporarily modified global state. There are two sets of functions, those prefixed with with_ and those with local_. The former reset their state as soon as the code argument has been evaluated. The latter reset when they reach the end of their scope, usually at the end of a function body.

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() / local_collate() - collation order
  • with_dir() / local_dir() - working directory
  • with_envvar() / local_envvar() - environment variables
  • with_libpaths() / local_libpaths() - library paths
  • with_locale() / local_locale() - any locale setting
  • with_makevars() / local_makevars() - Makevars variables
  • with_options() / local_options() - options
  • with_par() / local_par() - graphics parameters
  • with_path() / local_path() - PATH environment variable

There are also with_() and local_() functions to construct new with_* and local_* functions if needed.

dir.create("test")
getwd()
#> [1] "/private/var/folders/dt/r5s12t392tb5sk181j3gs4zw0000gn/T/Rtmp8xR1aN"
with_dir("test", getwd())
#> [1] "/private/var/folders/dt/r5s12t392tb5sk181j3gs4zw0000gn/T/Rtmp8xR1aN/test"
getwd()
#> [1] "/private/var/folders/dt/r5s12t392tb5sk181j3gs4zw0000gn/T/Rtmp8xR1aN"
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

Functions in withr

Name Description
with_envvar Environment variables
with_libpaths Library paths
defer Defer Evaluation of an Expression
local_ Create a new "with" or "local" function
with_options Options
with_par Graphics parameters
with_locale Locale settings
with_makevars Makevars variables
with_sink Output redirection
with_temp_libpaths Library paths
with_path PATH environment variable
with_seed Random seed
with_collate Collation Order
with_dir Working directory
withr Execute code in temporarily altered environment
No Results!

Last month downloads

Details

Encoding UTF-8
URL http://github.com/r-lib/withr#readme
BugReports http://github.com/r-lib/withr/issues
License GPL (>= 2)
LazyData true
Collate 'local_.R' 'with_.R' 'collate.R' 'defer.R' 'dir.R' 'env.R' 'libpaths.R' 'locale.R' 'makevars.R' 'options.R' 'par.R' 'path.R' 'seed.R' 'wrap.R' 'sink.R' 'utils.R' 'with.R'
RoxygenNote 6.0.1
NeedsCompilation no
Packaged 2017-07-28 19:07:05 UTC; jhester
Repository CRAN
Date/Publication 2017-07-28 22:56:28 UTC

Include our badge in your README

[![Rdoc](http://www.rdocumentation.org/badges/version/withr)](http://www.rdocumentation.org/packages/withr)