title: Partially evaluate expression when doing non-standard evaluation in R
body: <p>I'm digging my head into R's non-standard evaluation mechanisms. Halfway there, I'm still stuck from time to time. ;-)</p>
  
  <p>I made a little <code>calculator</code> that pretty-prints the calculation together with the result:</p>
  
  <pre><code>calculator &lt;- function(e) cat(deparse(substitute(e)), "=", e)
  </code></pre>
  
  <p>This works fine: </p>
  
  <pre><code>calculator(1 + 2)
  </code></pre>
  
  <p>prints:</p>
  
  <pre><code>1 + 2 = 3
  </code></pre>
  
  <p>However, when calling the <code>calculator</code> with:</p>
  
  <pre><code>a &lt;- 1; b &lt;- 2
  calculator(a + b)
  </code></pre>
  
  <p>the output is:</p>
  
  <pre><code>a + b = 3
  </code></pre>
  
  <p><em>How can I tweak my <code>calculator</code> in order it prints <code>1 + 2 = 3</code> also in the later case?</em></p>
  
  <hr>
  
  <p>I tried:</p>
  
  <pre><code>calculator2 &lt;- function(e) {
    ex &lt;- lapply(substitute(e), function(x) ifelse(is.numeric(x), eval(x), x))
    cat(deparse(ex), "=", e)
  }
  
  calculator2(1 + 2)
  # list(+, 1, 2) = 3
  calculator2(a + b)
  # list(+, a, b) = 3
  </code></pre>
  
  <p>This obviously doesn't work. After the <code>lapply</code> call I have a list and <code>cat</code> prints <code>list(+, 1, 2)</code> to the console.</p>
  
  <p>So I made another try:</p>
  
  <pre><code>calculator3 &lt;- function(e) {
    ex &lt;- substitute(e)
    for(i in 1:length(ex)) {
      if(is.numeric(ex[[i]])) ex[[i]] &lt;- eval(ex[[i]])
    }
    cat(deparse(ex), "=", e)
  }
  
  calculator3(1 + 2)
  # 1 + 2 = 3
  calculator3(a + b)
  # a + b = 3
  </code></pre>
  
  <p>..which is the same as my first version..</p>