EVOLUTION-MANAGER
Edit File: with_env.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: Evaluate an expression within a given 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 with_env {rlang}"><tr><td>with_env {rlang}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Evaluate an expression within a given environment</h2> <h3>Description</h3> <p><a href="https://lifecycle.r-lib.org/articles/stages.html#deprecated"><img src="../help/figures/lifecycle-deprecated.svg" alt='[Deprecated]' /></a> </p> <p>These functions evaluate <code>expr</code> within a given environment (<code>env</code> for <code>with_env()</code>, or the child of the current environment for <code>locally</code>). They rely on <code><a href="eval_bare.html">eval_bare()</a></code> which features a lighter evaluation mechanism than base R <code><a href="../../base/html/eval.html">base::eval()</a></code>, and which also has some subtle implications when evaluting stack sensitive functions (see help for <code><a href="eval_bare.html">eval_bare()</a></code>). </p> <p><code>locally()</code> is equivalent to the base function <code><a href="../../base/html/eval.html">base::local()</a></code> but it produces a much cleaner evaluation stack, and has stack-consistent semantics. It is thus more suited for experimenting with the R language. </p> <h3>Usage</h3> <pre> with_env(env, expr) locally(expr) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>env</code></td> <td> <p>An environment within which to evaluate <code>expr</code>. Can be an object with a <code><a href="get_env.html">get_env()</a></code> method.</p> </td></tr> <tr valign="top"><td><code>expr</code></td> <td> <p>An expression to evaluate.</p> </td></tr> </table> <h3>Examples</h3> <pre> # with_env() is handy to create formulas with a given environment: env <- child_env("rlang") f <- with_env(env, ~new_formula()) identical(f_env(f), env) # Or functions with a given enclosure: fn <- with_env(env, function() NULL) identical(get_env(fn), env) # Unlike eval() it doesn't create duplicates on the evaluation # stack. You can thus use it e.g. to create non-local returns: fn <- function() { g(current_env()) "normal return" } g <- function(env) { with_env(env, return("early return")) } fn() # Since env is passed to as_environment(), it can be any object with an # as_environment() method. For strings, the pkg_env() is returned: with_env("base", ~mtcars) # This can be handy to put dictionaries in scope: with_env(mtcars, cyl) </pre> <hr /><div style="text-align: center;">[Package <em>rlang</em> version 1.0.6 <a href="00Index.html">Index</a>]</div> </body></html>