<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/another-r-non-standard-evaluation-idea/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2017/03/another-r-non-standard-evaluation-idea/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://jcarroll.com.au/">Jonathan Carroll</a> had a an interesting <a target="_blank" rel="nofollow" href="https://cran.r-project.org/"><code>R</code></a> language idea: to <a target="_blank" rel="nofollow" href="https://twitter.com/carroll_jono/status/842142292253196290">use <code>@</code>-notation</a> to request value substitution in a non-standard evaluation environment (inspired by <a target="_blank" rel="nofollow" href="https://dev.mysql.com/doc/refman/5.7/en/user-variables.html">msyql User-Defined Variables</a>).</p>
<p>He even picked the right image:</p>
<p><img title="PandorasBox.jpg" alt="PandorasBox" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/03/PandorasBox.jpg?resize=321%2C264" border="0" data-recalc-dims="1"><span></span></p>
<p>The idea is kind of reverse from some Lisp ideas (&quot;evaled unless ticked&quot;), but an interesting possibility. We can play along with it a bit in <a target="_blank" rel="nofollow" href="https://cran.r-project.org/"><code>R</code></a> as follows. The disadvantages of our simulation include:</p>
<ul>
<li>The user must both call <code>wrapr::ateval</code> and place their code in quotes.</li>
<li>The effect is still achieved through string substitution.</li>
</ul>
<p>But here it is for what it is worth:</p>
<div>
<pre><code><span># devtools::install_github(&quot;WinVector/wrapr&quot;)</span>
<span>library</span>(<span>&quot;wrapr&quot;</span>)
<span>library</span>(<span>&quot;dplyr&quot;</span>)</code></pre>
</div>
<p>The original example function from the Tweet:</p>
<div>
<pre><code>f &lt;-<span> </span>function(col1, col2, new_col_name) {
  <span>ateval</span>(<span>'mtcars %>% mutate(@new_col_name = @col1 + @col2)'</span>)
}</code></pre>
</div>
<p>And the requested effect actually realized:</p>
<div>
<pre><code>d &lt;-<span> </span><span>f</span>(<span>'gear'</span>, <span>'carb'</span>, <span>'nonsense'</span>)
<span>head</span>(d)</code></pre>
</div>
<pre><code>##    mpg cyl disp  hp drat    wt  qsec vs am gear carb nonsense
## 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4        8
## 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4        8
## 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1        5
## 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1        4
## 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2        5
## 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1        4</code></pre>
<p>The point is a standard function (<code>f()</code>) can accept user column names and then itself use convenient non-standard evaluation notation (in this case <code>dplyr</code>) to perform operations. This package-based simulation is not as good as actual language support, but simulations like this are how we collect experience with possible new language features.</p>
<p>The real point is a user wants a flexible language with macros and functions (<code>R</code> uses an intermediate form called <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Fexpr">&quot;an Fexpr&quot;</a> for just about everything) that they can both use interactively and program over. This means they eventually want an execution environment where they can both pass in parametric values (what <code>R</code> calls standard evaluation) and the ability to have code elements treated directly as values (a convenience and related to what <code>R</code> calls non-standard evaluation).</p>
<p>The classic Lisp solution organizes things a bit differently, and uses various &quot;back-tick&quot; notations to specify control of the interpretation of symbols. I think <code>R</code> has picked a different set of defaults as to how symbols, values, expressions, and execution interact- so any notation is going to be a bit different.</p>
<p>The development version of <code>wrapr</code> can be found <a target="_blank" rel="nofollow" href="https://github.com/WinVector/wrapr">here</a> (<code>atexpr</code> is not yet in the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=wrapr">CRAN version</a>, which supplies the <a target="_blank" rel="nofollow" href="https://github.com/WinVector/Examples/blob/master/wrapr/example.md"><code>let</code></a> alternative). The example shown in this article can be found in markdown form <a target="_blank" rel="nofollow" href="https://github.com/WinVector/Examples/blob/master/wrapr/atexpr.md">here</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2017/03/another-r-non-standard-evaluation-idea/"> R – Win-Vector Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Pk6HYVwgg_4"></a></div>