• Fixes for CRAN checks.

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 LIFO cleanup: on.exit(after = FALSE).
    • Calling sys.on.exit() elsewhere than top-level didn’t work. This is needed for manual invocation 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

  • Fixes for CRAN checks.
  • Fixes for CRAN checks.
  • 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()
    on.exit(set(old))
    set(new)
    action()

    Instead of:

    old <- set(new)
    on.exit(set(old))
    action()

    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.

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

Deprecations

  • 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

  • Fixes test failures with testthat 2.0.0

  • with_file() function to automatically remove files.

  • 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).

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