title: function scope using `eval` and `local`
body: <p>I have a function to evaluate a list of expressions and return a value based on which expression evaluates to true. Here are my expressions and values:</p>
  
  <pre><code>myexprs = list(expression(x &lt;= 5), expression(x &gt; 5 &amp; x &lt; 7), expression(x &gt;= 7))
  myvals = c(0, 1, 0)
  </code></pre>
  
  <p>I make a function that returns a function to evaluate a given number using <code>myexprs</code> and <code>myvals</code>. I use <code>lapply</code> to evaluate the expressions and return the value of <code>myvals</code> that matches which expression in <code>myexprs</code> evaluates to <code>TRUE</code>. If I do:</p>
  
  <pre><code>hscd = function(expr, score){
    function(x) score[unlist(lapply(expr, function(e) local(eval(e))))]
  }  
  
  test = hscd(myexprs, myvals)
  test(4)
  test(6)
  test(7)
  </code></pre>
  
  <p>It works. However, if I do</p>
  
  <pre><code>hscd = function(expr, score){
    function(x) score[unlist(lapply(expr, local))]
  }  
  
  test = hscd(myexprs, myvals)
  test(4)
  </code></pre>
  
  <p>It breaks. </p>
  
  <p><strong>My question:</strong> Why do I need to do <code>local(eval(expr))</code> rather than just doing <code>local(expr)</code>? From the <a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/eval.html" rel="nofollow">documentation</a> it seems like the latter method should work too.</p>
  
  <hr>
  
  <p><em>for completeness</em>: If I do</p>
  
  <pre><code>hscd = function(expr, score){
    function(x) score[unlist(lapply(expr, eval))]
  }  
  
  test = hscd(myexprs, myvals)
  test(4)
  </code></pre>
  
  <p>The function breaks because it looks for the variable <code>x</code> outside of the internal function, which mostly makes sense to me.</p>