<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/programming-over-r/"></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/04/programming-over-r/"> 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://cran.r-project.org/"><code>R</code></a> is a very fluid language amenable to <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Metaprogramming">meta-programming</a>, or alterations of the language itself. This has allowed the late user-driven introduction of a number of powerful features such as <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=magrittr">magrittr pipes</a>, <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=foreach">the foreach system</a>, <a target="_blank" rel="nofollow" href="http://www.jottr.org/2016/07/a-future-for-r-slides-from-user-2016.html">futures</a>, <a target="_blank" rel="nofollow" href="https://github.com/Rdatatable/data.table/wiki">data.table</a>, and <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=dplyr">dplyr</a>. Please read on for some small meta-programming effects we have been experimenting with.</p>
<p><a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Drawing_Hands"><img title="NewImage.png" alt="NewImage" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/04/NewImage-14.png?w=660" border="0" data-recalc-dims="1"></a><span></span></p>
<p>
<h2>Meta-Programming</h2>
<p>Meta-programming is a powerful tool that allows one to re-shape a programming language or write programs that automate parts of working with a programming language.</p>
<p>Meta-programming itself has the central contradiction that one hopes nobody else is doing meta-programming, but that they are instead dutifully writing referentially transparent code that is safe to perform transformations over, so that one can safely introduce their own clever meta-programming. For example: one would hate to lose the ability to use a powerful package such as <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/future/index.html">future</a> because we already “used up all the referential transparency” for some minor notational effect or convenience.</p>
<p>That being said, <code>R</code> is an open system and it is fun to play with the notation. I have been experimenting with different notations for programming over <code>R</code> for a while, and thought I would demonstrate a few of them here.</p>
<h2>Let Blocks</h2>
<p>We have been using <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2017/03/wrapr-for-sweet-r-code/"><code>let</code> </a>to code over non-standard evaluation (NSE) packages in <code>R</code> for a while now. This allows code such as the following:</p>
<pre>
library(&quot;dplyr&quot;)
library(&quot;wrapr&quot;)

d &lt;- data.frame(x = c(1, NA))

cname &lt;- 'x'
rname &lt;- paste(cname, 'isNA', sep = '_')

let(list(COL = cname, RES = rname),
    d %>% mutate(RES = is.na(COL))
)

 #    x x_isNA
 # 1  1  FALSE
 # 2 NA   TRUE
</pre>
<p><code>let</code> is in fact quite handy notation that will work in a non-deprecated manner with both <code>dplyr 0.5</code> and <code>dplyr 0.6</code>. It is how we are future-proofing our current <code>dplyr</code> workflows.</p>
<h2>Unquoting</h2>
<p><code>dplyr 0.6</code> is introducing a new execution system (alternately called <code>rlang</code> or <code>tidyeval</code>, see <a target="_blank" rel="nofollow" href="https://blog.rstudio.org/2017/04/13/dplyr-0-6-0-coming-soon/">here</a>) which uses a notation more like the following (but fewer parenthesis, and with the ability to control left-hand side of an in-argument assignment):</p>
<pre>
beval(d %>% mutate(x_isNA = is.na((!!cname))))
</pre>
<p>The inability to re-map the right-hand side of the apparent assignment is because the “<code>(!! )</code>” notation doesn’t successfully masquerade as a lexical token valid on the left-hand side of assignments or function argument bindings.</p>
<p>And there was <a target="_blank" rel="nofollow" href="https://stat.ethz.ch/pipermail/r-devel/2017-March/073898.html">an R language proposal</a> for a notation like the following (but without the quotes, and with some care to keep it syntactically distinct from other uses of “@”):</p>
<pre>
ateval('d %>% mutate(@rname = is.na(@cname))')
</pre>
<p><code>beval</code> and <code>ateval</code> are just curiosities implemented to try and get a taste of the new <code>dplyr</code> notation, and we don’t recommend using them in production — their ad-hoc demonstration implementations are just not powerful enough to supply a uniform interface. <code>dplyr</code> itself seems to be replacing a lot of <code>R</code>‘s execution framework to achieve stronger effects. </p>
<h2>Write Arrow</h2>
<p>We are experimenting with “write arrow” (a deliberate homophone of “right arrow”). It allows the convenient storing of a pipe result into a variable chosen by name.</p>
<pre>
library(&quot;dplyr&quot;)
library(&quot;replyr&quot;)

'x' -> whereToStoreResult

7 %>% sin %>% cos %->_% whereToStoreResult

print(x)
 ## [1] 0.7918362
</pre>
<p>Notice, the value “7” is stored in the variable “x” not in a variable named “whereToStoreResult”. “whereToStoreResult” was able to name where to store the value <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2017/02/my-recent-barug-talk-parametric-programming-in-r-with-replyr/">parametrically</a>. </p>
<p>This allows code such as the following:</p>
<pre>
for(i in 1:3) { 
  i %->_% paste0('x',i)
}
</pre>
<p>(Please run the above to see the automatic creation of variables named “x1”, “x2”, and “x3”, storing values 1,2, and 3 respectively.)</p>
<p>We know left to right assignment is heterodox; but the notation is very slick if you are consistent with it, and add in some formatting rules (such as insisting on a line break after each pipe stage).</p>
<h2>Conclusion</h2>
<p>One wants to use meta-programming with care. In addition to bringing in desired convenience it can have unexpected effects and interactions deeper in a language or when exposed to other meta-programming systems. This is one reason why a “seemingly harmless” proposal such as <a target="_blank" rel="nofollow" href="https://stat.ethz.ch/pipermail/r-devel/2017-March/073889.html">“user defined unary functions”</a> or <a target="_blank" rel="nofollow" href="https://stat.ethz.ch/pipermail/r-devel/2017-March/073898.html">“at unquoting”</a> takes so long to consider. This is also why new language features are best tried in small packages first (so users can easily chose to include them or not in their larger workflow) to drive public <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Request_for_Comments">request for comments (RFC)</a> processes or allow the ideas to evolve (and not be frozen at their first good idea, a great example of community accepted change being <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/17002119/haskell-pre-monadic-i-o">Haskel’s switch from request chaining IO to monadic IO</a>; the first IO system “seemed inevitable” until it was completely replaced).</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/04/programming-over-r/"> 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/-s92aUQ4nR0"></a></div>