withr 2.1.0 is now available on CRAN! withr makes working with global state in R safer and less error prone. It has only base package dependencies so is easily included in packages.

Install the latest version with:

Whither withr?

Many functions in R modify global state in some fashion. Some common examples are par() for graphics parameters, dir() to change the current directory and options() to set a global option. Using these functions is handy when using R interactively, because you can set them early in your experimentation and they will remain set for the duration of the session. However this makes programming with these settings difficult, because they make your function impure by modifying a global state. Therefore you should always strive to reset the previous state when the function exits.

One common idiom for dealing with this problem is to save the current state, make your change, then restore the previous state.

However this approach can fail if there’s an error before you are able to reset the options.

Using the base function on.exit() is a robust solution to this problem. on.exit() will run the code when the function is exited, regardless of whether it exits normally or with an error.

However this solution is somewhat cumbersome to work with. You need to remember to use an on.exit() call after each stateful call. In addition by default each on.exit() action will overwrite any previous on.exit() action in the same function unless you use the add = TRUE option. add = TRUE also adds additional code to the end of existing code, which means the code is not run in the Last-In, First-Out order you would generally prefer. It is also not possible to have this cleanup code performed before the function has finished.

withr is a solution to these issues. It defines a large set of functions for dealing with global settings in R, such as with_par(). These functions set one of the global settings for the duration of a block of code, then automatically reset it after the block is completed.

#> [1] "black"
#> [1] "black"

In addition to the with_* functions there are local_* variants whose effects last until the end of the function they are included in. These work similar to on.exit(), but you can set the options in one call rather than two.

New features

Here are some highlights of new functions for v2.1.0.

Graphics devices

There are now a comprehensive set of functions to deal with R’s builtin graphics devices.

These functions open a new graphics device, run some code, then automatically close the device.

Thanks to Richard Cotton’s great pull request for this feature!


There are two new functions for cleaning up connections in R. with_connection() allows you to automatically close R’s file connections. Here we create a writable file connection, write some lines to it with with_connection(), then open a read-only connection and read the file using local_connection().

with_db_connection() provides DBI connections to databases which automatically call DBI::dbDisconnect(). Here we create a new SQLite database, connect to it with with_db_connection(), and write a new table to it. We then create another connection with local_db_connection() and read from the table.


with_package() allows you to temporarily attach a package.

Other changes

There are many other bug fixes and other minor improvements in this release. You can see a complete list in the release notes.

A big thanks goes to all the community members who contributed code and opened issues since the last release!

@QuLogic, @krlmlr, @hadley, @wlandau-lilly, @jimhester, @kevinushey, and @richierocks