Giter Site home page Giter Site logo

withr's Introduction

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.

  • set_makevars()
  • defer()
  • defer_parent()
  • with_collate() / local_collate() - collation order
  • with_connection() / local_connection() - R connections.
  • with_db_connection() / local_db_connection()
  • with_dir() / local_dir() - working directory
  • with_environment() / local_environment()
  • 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_message_sink() / local_message_sink()
  • with_options() / local_options() - options
  • with_output_sink() / local_output_sink()
  • with_par() / local_par() - graphics parameters
  • with_path() / local_path() - PATH environment variable
  • with_preserve_seed()
  • with_rng_version() / local_rng_version()
  • with_seed()
  • with_temp_libpaths() / local_temp_libpaths()
  • with_timezone() / local_timezone()
  • with_*() and local_*() functions for the built in R devices, bmp, cairo_pdf, cairo_ps, pdf, postscript, svg, tiff, xfig, png, jpeg.
  • with_package() / local_package(), with_namespace() / local_namespace() and with_environment() / local_environment()
    • to run code with modified object search paths.
  • with_tempfile() / local_tempfile() - Create and clean up a temp file.
  • with_file() / local_file() - Create and clean up a normal file.

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

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 functions

These functions are variants of the corresponding with_() function, but instead of resetting the value at the end of the function call they reset when the current context goes out of scope. This is most useful within a function.

f <- function(x) {
  local_envvar(c("WITHR" = 2))
  Sys.getenv("WITHR")
}

f()
#> [1] "2"
Sys.getenv("WITHR")
#> [1] ""

See Also

withr's People

Contributors

jimhester avatar krlmlr avatar hadley avatar gaborcsardi avatar lionel- avatar alexcipro avatar dragosmg avatar javierluraschi avatar lauracion avatar mtmorgan avatar romainfrancois avatar zkamvar avatar

Watchers

James Cloos avatar  avatar

Forkers

guhjy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.