title: How can I manage environments to wrap functions and avoid environment issues?
body: <p>Here are 4 functions which mimics a family of functions from basic to advanced.</p>
  
  <pre><code>f1 &lt;- function(expr,envir) {
    eval(expr,envir)
  }
  
  f2 &lt;- function(expr) {
    expr &lt;- substitute(expr)
    f1(expr,parent.frame())
  }
  
  f3 &lt;- function(x) {
    lapply(1:3, function(i) {
      f2(x+i)
    })
  }
  
  f4 &lt;- function(...) {
    f3(...)
  }
  </code></pre>
  
  <p><code>f1</code> is the fundamental one, <code>f2</code> calls <code>f1</code> with an expression, <code>f3</code> iteratively calls <code>f2</code> with <code>x</code> defined in its body frame.</p>
  
  <p>Calling <code>f3</code> yields</p>
  
  <pre><code>&gt; f3(1)
  [[1]]
  [1] 2
  
  [[2]]
  [1] 3
  
  [[3]]
  [1] 4
  </code></pre>
  
  <p>which has no problem because the <code>x</code> is evaluated correctly in <code>parent.frame()</code> of <code>f2</code>. However, if a wrapper function <code>f4</code> is called, an error occurs:</p>
  
  <pre><code>&gt; f4()
  Error in eval(expr, envir, enclos) : 
    argument "x" is missing, with no default
  </code></pre>
  
  <p>I inspect the parent environments local to <code>f2</code> in line and find that <code>parent.frame(3)</code> contains <code>x: missing</code>.</p>
  
  <p>Is there a way or good practice to manage the environments so that I can wrap functions like <code>f4</code> without worrying too much about the parent environment issues?</p>