# withr

> Run R code with temporarily modified global state

Source: https://talira.ai/catalog/withr-e27swbej
Every listing: https://talira.ai/catalog.md

- Kind: Open source
- Types: Library
- Categories: [libraries / general purpose](https://talira.ai/categories/libraries/general-purpose), [languages / r](https://talira.ai/categories/languages/r)
- Licence: MIT[^1]
- Pricing: Free and open source[^1]
- Free tier: yes[^1]
- Backed by: [Posit](https://talira.ai/orgs/posit.md)

withr is an R package of functions that change a piece of global state for the duration of a block of code and restore it afterwards, so side effects don't leak. Each setting has a with_*() form that wraps a block and a local_*() form that lasts until the calling function exits: the working directory, environment variables, options, the locale, PATH and library paths, the random seed, graphics parameters and devices, temporary and normal files, and file and database connections. Many of the functions began in devtools. It's widely used in package tests, alongside [testthat](https://talira.ai/catalog/testthat-7gmdb2r0.md).

## Links

- Website: https://withr.r-lib.org/
- GitHub: https://github.com/r-lib/withr
- Docs: https://withr.r-lib.org/reference/

## Problems it solves

- change an environment variable or option in a test without leaving it changed afterwards
- run part of my R code in another working directory and always switch back
- make sure temporary files and connections get cleaned up when my function exits

## Features

- with_*() functions change a setting for one block of code and restore it
- local_*() functions keep a change until the calling function exits
- Working directory, environment variables, options, locale, PATH and library paths
- Seeds, graphics parameters and graphics devices
- Temporary files and file or database connections that are cleaned up automatically

## Trust

Overall: 0.37 out of 1.
Signals: maintenance 0.46, adoption 0.45, momentum 0.11, community 0.63, backing 0.30, extensibility 0.00.

- last commit 48 days ago
- 1 commits in the last 90 days
- latest release v3.0.3 5 months ago
- 182 GitHub stars
- 36 contributors
- backed by Posit

## Sources

[^1]: https://github.com/r-lib/withr/blob/main/DESCRIPTION (checked 27 Nov 2026)
