EVOLUTION-MANAGER
Edit File: withr.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Execute code in temporarily altered environment</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for withr {withr}"><tr><td>withr {withr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Execute code in temporarily altered environment</h2> <h3>Description</h3> <p>All functions prefixed by <code>with_</code> work as follows. First, a particular aspect of the global environment is modified (see below for a list). Then, custom code (passed via the <code>code</code> argument) is executed. Upon completion or error, the global environment is restored to the previous state. Each <code>with_</code> function has a <code>local_</code> variant, which instead resets the state when the current evaluation context ends (such as the end of a function). </p> <h3>Arguments pattern</h3> <table summary="Rd table"> <tr> <td style="text-align: left;"> <code>new</code> </td><td style="text-align: left;"> <code style="white-space: pre;">[various]</code> </td><td style="text-align: left;"> Values for setting </td> </tr> <tr> <td style="text-align: left;"> <code>code</code> </td><td style="text-align: left;"> <code style="white-space: pre;">[any]</code> </td><td style="text-align: left;"> Code to execute in the temporary environment </td> </tr> <tr> <td style="text-align: left;"> <code>...</code> </td><td style="text-align: left;"> </td><td style="text-align: left;"> Further arguments </td> </tr> <tr> <td style="text-align: left;"> </td> </tr> </table> <h3>Usage pattern</h3> <p><code>with_...(new, code, ...)</code> </p> <h3>withr functions</h3> <ul> <li> <p><code><a href="with_collate.html">with_collate()</a></code>: collation order </p> </li> <li> <p><code><a href="with_dir.html">with_dir()</a></code>: working directory </p> </li> <li> <p><code><a href="with_envvar.html">with_envvar()</a></code>: environment variables </p> </li> <li> <p><code><a href="with_libpaths.html">with_libpaths()</a></code>: library paths, replacing current libpaths </p> </li> <li> <p><code><a href="with_locale.html">with_locale()</a></code>: any locale setting </p> </li> <li> <p><code><a href="with_makevars.html">with_makevars()</a></code>: Makevars variables </p> </li> <li> <p><code><a href="with_options.html">with_options()</a></code>: options </p> </li> <li> <p><code><a href="with_par.html">with_par()</a></code>: graphics parameters </p> </li> <li> <p><code><a href="with_path.html">with_path()</a></code>: <code>PATH</code> environment variable </p> </li> <li> <p><code><a href="with_sink.html">with_sink()</a></code>: output redirection </p> </li></ul> <h3>Creating new "with" functions</h3> <p>All <code>with_</code> functions are created by a helper function, <code><a href="with_.html">with_()</a></code>. This functions accepts two arguments: a setter function and an optional resetter function. The setter function is expected to change the global state and return an "undo instruction". This undo instruction is then passed to the resetter function, which changes back the global state. In many cases, the setter function can be used naturally as resetter. </p> <h3>Author(s)</h3> <p><strong>Maintainer</strong>: Lionel Henry <a href="mailto:lionel@rstudio.com">lionel@rstudio.com</a> </p> <p>Authors: </p> <ul> <li><p> Jim Hester </p> </li> <li><p> Kirill Müller <a href="mailto:krlmlr+r@mailbox.org">krlmlr+r@mailbox.org</a> </p> </li> <li><p> Kevin Ushey <a href="mailto:kevinushey@gmail.com">kevinushey@gmail.com</a> </p> </li> <li><p> Hadley Wickham <a href="mailto:hadley@rstudio.com">hadley@rstudio.com</a> </p> </li> <li><p> Winston Chang </p> </li></ul> <p>Other contributors: </p> <ul> <li><p> Jennifer Bryan [contributor] </p> </li> <li><p> Richard Cotton [contributor] </p> </li> <li><p> RStudio [copyright holder] </p> </li></ul> <h3>See Also</h3> <p>Useful links: </p> <ul> <li> <p><a href="https://withr.r-lib.org">https://withr.r-lib.org</a> </p> </li> <li> <p><a href="https://github.com/r-lib/withr#readme">https://github.com/r-lib/withr#readme</a> </p> </li> <li><p> Report bugs at <a href="https://github.com/r-lib/withr/issues">https://github.com/r-lib/withr/issues</a> </p> </li></ul> <h3>Examples</h3> <pre> getwd() with_dir(tempdir(), getwd()) getwd() Sys.getenv("WITHR") with_envvar(c("WITHR" = 2), Sys.getenv("WITHR")) Sys.getenv("WITHR") with_envvar(c("A" = 1), with_envvar(c("A" = 2), action = "suffix", Sys.getenv("A")) ) # local variants are best used within other functions f <- function(x) { local_envvar(c("WITHR" = 2)) Sys.getenv("WITHR") } Sys.getenv("WITHR") </pre> <hr /><div style="text-align: center;">[Package <em>withr</em> version 2.5.0 <a href="00Index.html">Index</a>]</div> </body></html>