title: Pass a function to `update` defined inside the function calling `update`
body: <p>I want to call <code>update</code> inside a function. However, the argument to be passed (a) must a symbol referring to a function, and (b) this function is created dynamically inside the function based on other arguments.</p>
  <p>If something similar is done but in the global environment, the following code works:</p>
  <pre class="lang-r prettyprint-override"><code>set.seed(8700)
  x &lt;- rnorm(10)
  y &lt;- rnorm(10)
  fctgen &lt;- function(FUN, n) {
      function() do.call(FUN, list(n = n))
    }
  z_global &lt;- fctgen(rnorm, 10)
  lm_out &lt;- lm(y ~ x)
  update(lm_out, . ~ . + z_global())
  
  #Output
  #Call:
  #lm(formula = y ~ x + z_global(10))
  #
  #Coefficients:
  # (Intercept)             x  z_global(10)  
  #     0.08648       0.49040       0.18212
  </code></pre>
  <p>The following version does not work because <code>z_local</code> is not in the environment in which <code>update</code> evaluates the updated call.</p>
  <pre class="lang-r prettyprint-override"><code>set.seed(8700)
  x &lt;- rnorm(10)
  y &lt;- rnorm(10)
  lm_out &lt;- lm(y ~ x)
  fctgen &lt;- function(FUN, n) {
      function() do.call(FUN, list(n = n))
    }
  tmpfct1 &lt;- function(fit, FUN) {
      z_local &lt;- fctgen(FUN = FUN, n = nrow(model.frame(lm_out)))
      update(fit, . ~ . + z_local())
    }
  tmpfct1(lm_out, rnorm)
  # Error in z_local() : could not find function &quot;z_local&quot;
  </code></pre>
  <p>How can I let <code>update</code> find <code>z_local</code>? For some reasons, <code>z_local</code> must be passed as a symbol to <code>update</code>, and its definition will be generated dynamically inside the function. Therefore, I would like to see how this problem can be solved by somehow calling <code>update</code> (e.g., by <code>eval</code> with an alternative environment?) and let <code>update</code> find <code>z_local</code> itself when evaluating a call. The actual function I will pass to <code>update</code> is much more complicated than the one in the example, and so embedding its content directly in the argument to <code>update</code> is not an option.</p>
  <p>(Note: This is similar to another question I asked : <a href="https://stackoverflow.com/questions/68789112/in-r-how-to-let-update-find-a-variable-created-inside-the-function-calling-up">In R, How to let update() find a variable created inside the function calling update()?</a>, and that example can indeed be solved. However, I have a different scenario that may need other solutions. Therefore, I create this question.)</p>