<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/helplet-packagereplyr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/12/helplet-packagereplyr/"> 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 bit more on our <a target="_blank" rel="nofollow" href="https://github.com/WinVector/replyr">replyr</a> <a target="_blank" rel="nofollow" href="https://cran.r-project.org/">R</a> package.<span></span><code>
<pre>
  library(&quot;replyr&quot;)
  help(let, package='replyr')
</pre>
<p></p>
<table summary="page for let {replyr}" width="100%">
<tr>
<td>let {replyr}</td>
<td>R Documentation</td>
</tr>
</table>
<h2>Prepare expr for execution with name substitutions specified in alias.</h2>
<h3>Description</h3>
<p><code>replyr::let</code> implements a mapping from desired names (names used directly in the expr code) to names used in the data. Mnemonic: &quot;expr code symbols are on the left, external data and function argument names are on the right.&quot;
</p>
<h3>Usage</h3>
<pre>
let(alias, expr)
</pre>
<h3>Arguments</h3>
<table summary="R argblock">
<tr valign="top">
<td><code>alias</code></td>
<td>
<p>mapping from free names in expr to target names to use.</p>
</td>
</tr>
<tr valign="top">
<td><code>expr</code></td>
<td>
<p>block to prepare for execution</p>
</td>
</tr>
</table>
<h3>Details</h3>
<p>Code adapted from <code>gtools::strmacro</code> by Gregory R. Warnes (License: GPL-2, this portion also available GPL-2 to respect gtools license). Please see the <code>replyr</code> <code>vignette</code> for some discussion of let and crossing function call boundaries: <code>vignette('replyr','replyr')</code>. Transformation is performed by substitution on the expression parse tree, so be wary of name collisions or aliasing.
</p>
<p>Something like <code>replyr::let</code> is only useful to get control of a function that is parameterized (in the sense it take column names) but non-standard (in that it takes column names from non-standard evaluation argument name capture, and not as simple variables or parameters). So <code>replyr:let</code> is not useful for non-parameterized functions (functions that work only over values such as <code>base::sum</code>), and not useful for functions take parameters in straightforward way (such as <code>base::merge</code>‘s &quot;<code>by</code>&quot; argument). <code>dplyr::mutate</code> is an example where we can use a <code>replyr::let</code> helper. <code>dplyr::mutate</code> is parameterized (in the sense it can work over user supplied columns and expressions), but column names are captured through non-standard evaluation (and it rapidly becomes unwieldy to use complex formulas with the standard evaluation equivalent <code>dplyr::mutate_</code>).
</p>
<h3>Value</h3>
<p>item ready to evaluate, need to apply with &quot;()&quot; to perform the evaluation in own environment.</p>
<h3>See Also</h3>
<p><code>replyr_mapRestrictCols</code>
</p>
<h3>Examples</h3>
<pre>

library('dplyr')
d &lt;- data.frame(Sepal_Length=c(5.8,5.7),
                Sepal_Width=c(4.0,4.4),
                Species='setosa',
                rank=c(1,2))

mapping = list(RankColumn='rank',GroupColumn='Species')
let(alias=mapping,
    expr={
       # Notice code here can be written in 
       # terms of known or concrete
       # names &quot;RankColumn&quot; and &quot;GroupColumn&quot;, 
       # but executes as if we
       # had written mapping specified 
       # columns &quot;rank&quot; and &quot;Species&quot;.
       #
       # restart ranks at zero.
       d %>% mutate(RankColumn=RankColumn-1) -> dres
       # confirm set of groups.
       unique(d$GroupColumn) -> groups
    })()
print(groups)
print(length(groups))
print(dres)

# It is also possible to pipe into let-blocks, 
# but it takes some extra notation
# (notice the extra &quot;. %>%&quot; at the beginning 
# and the extra &quot;()&quot; at the end).

d %>% let(alias=mapping,
         expr={
           . %>% mutate(RankColumn=RankColumn-1)
         })()()

# Or:

f &lt;- let(alias=mapping,
         expr={
           . %>% mutate(RankColumn=RankColumn-1)
         })()
d %>% f

# Be wary of using any assignment to attempt 
# side-effects in these &quot;delayed pipelines&quot;,
# as the assignment tends to happen during the 
# let dereference and not (as one would hope)
# during the later pipeline application.  Example:

g &lt;- let(alias=mapping,
         expr={
           . %>% mutate(RankColumn=RankColumn-1) -> ZZZ
         })()
print(ZZZ)
# Notice ZZZ has captured a copy of the 
# sub-pipeline and not waited for application of g.
# Applying g performs a calculation, 
# but does not overwrite ZZZ.

g(d)
print(ZZZ)
# Notice ZZZ is not a copy of g(d), 
# but instead still the pipeline fragment.


# let works by string substitution 
# aligning on word boundaries,
# so it does (unfortunately) 
# also re-write strings.
let(list(x='y'),'x')()

</pre>
<hr>
<div>[Package <em>replyr</em> version 0.1.1 Index]</div>

<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/2016/12/helplet-packagereplyr/"> 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/dAS2g9rzR8U"></a></div></code>