Skip to content

withr 3.0.0

Performance of withr

  • defer() is now a thin wrapper around base::on.exit(). This is possible thanks to two contributions that we made to R 3.5:

    • We added an argument for FIFO cleanup: on.exit(after = FALSE).
    • Calling sys.on.exit() elsewhere than top-level didn’t work. This is needed for manual invokation with deferred_run().

    Following this change, defer() is now much faster (although still slower than on.exit() which is a primitive function and about as fast as it gets). This also increases the compatibility of defer() with on.exit() (all handlers are now run in the expected order even if they are registered with on.exit()) and standalone versions of defer().

Breaking change

  • When source() is used with a local environment, as opposed to globalenv() (the default), you now need to set options(withr.hook_source = TRUE) to get proper withr support (running defer() or local_ functions at top-level of a script). THis support is disabled by default in local environments to avoid a performance penalty in normal usage of withr features.

Other features and bugfixes

,* local_tempfile() now writes lines in UTF-8 (#210) and always uses \n for newlines (#216).

withr 2.5.2

CRAN release: 2023-10-30

  • Fixes for CRAN checks.

withr 2.5.1

CRAN release: 2023-09-26

  • Fixes for CRAN checks.

withr 2.5.0

CRAN release: 2022-03-03

  • defer() and all local_*() functions now work when run inside of a .Rmd. The deferred expressions are executed when knitr exits.

  • defer() and local_ functions now work within source(). The deferred expressions are executed when source() exits.

  • with_() and local_() gain a get argument. Supply a getter function to create with and local functions that are robust to early exits.

    When supplied, this restoration pattern is used:

    old <- get()

    Instead of:

    old <- set(new)

    This ensures proper restoration of the old state when an early exit occurs during set() (for instance when a deprecation warning is caught, see #191).

  • These with_ and local_ functions are now robust to early exits (see next bullet):

    • _locale()
    • _envvar()
    • _libpaths()
    • _options()
    • _par()
    • _path()
    • _seed()
  • with_namespace() and local_namespace() now pass warn.conflicts to attach() (@kyleam, #185).

  • local_rng_version() and local_seed() no longer warn when restoring sample.kind to "Rounding" (#167).

  • with_seed() now preserves the current values of RNGkind() (#167).

  • with_collate() is no longer affected by the LC_COLLATE environment variable set to “C” (#179).

  • Local evaluations in the globalenv() (as opposed to top-level ones) are now unwound in the same way as regular environments.

  • local_tempfile() gains a lines argument so, if desired, you can pre-fill the temporary file with some data.

withr 2.4.3

CRAN release: 2021-11-30

withr 2.4.2

CRAN release: 2021-04-18

withr 2.4.1

CRAN release: 2021-01-26

  • Tests which require capabilities("cairo") are now skipped.

withr 2.4.0

CRAN release: 2021-01-16

withr 2.3.0

CRAN release: 2020-09-22


  • local_tempfile() argument new is deprecated, in favor of returning the path to the new tempfile. calls like local_tempfile("xyz") should be replaced with xyx <- local_tempfile() in your code (#141).

New features

Minor improvements and fixes

withr 2.2.0

CRAN release: 2020-04-20

withr 2.1.2

CRAN release: 2018-03-15

withr 2.1.1

CRAN release: 2017-12-19

  • Fixes test failures with testthat 2.0.0

  • with_file() function to automatically remove files.

withr 2.1.0

CRAN release: 2017-11-01

withr 2.0.0

CRAN release: 2017-07-28

  • Each with_ function now has a local_ variant, which reset at the end of their local scope, generally at the end of the function body.

  • New functions with_seed() and with_preserve_seed() for running code with a given random seed (#45, @krlmlr).

withr 1.0.2

CRAN release: 2016-06-20

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

withr 1.0.1

CRAN release: 2016-02-04

withr 1.0.0

CRAN release: 2015-09-23

  • First Public Release