<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/wrapr-r-code-sweeteners/"></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/08/wrapr-r-code-sweeteners/"> 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 rel="nofollow" href="https://winvector.github.io/wrapr/" target="_blank"><code>wrapr</code></a> is an <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">R</a> package that supplies powerful tools for writing and debugging R code.</p>
<div>
<img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/08/wraprs.png?w=660" alt="Wraprs" title="wraprs.png" data-recalc-dims="1">
</div>
<p><span></span></p>
<p>Primary <code>wrapr</code> services include:</p>
<ul>
<li><code>let()</code></li>
<li><code>%.>%</code> (dot arrow pipe)</li>
<li><code>:=</code> (named map builder)</li>
<li><code>λ()</code> (anonymous function builder)</li>
<li><code>DebugFnW()</code></li>
</ul>
<h2><code>let()</code></h2>
<p><code>let()</code> allows execution of arbitrary code with substituted variable names (note this is subtly different than binding values for names as with <code>base::substitute()</code> or <code>base::with()</code>).</p>
<p>The function is simple and powerful. It treats strings as variable names and re-writes expressions as if you had used the denoted variables. For example the following block of code is equivalent to having written &quot;<code>a + a</code>&quot;.</p>
<div>
<pre><code><span>library</span>(<span>&quot;wrapr&quot;</span>)

a &lt;-<span> </span><span>7</span>

<span>let</span>(
  <span>c</span>(<span>VAR =</span> <span>'a'</span>),
  
  VAR +<span> </span>VAR
)
 <span># [1] 14</span></code></pre>
</div>
<p>This is useful in re-adapting non-standard evaluation interfaces (NSE interfaces) so one can script or program over them.</p>
<p>We are trying to make <code>let()</code> self teaching and self documenting (to the extent that makes sense). For example try the arguments &quot;<code>eval=FALSE</code>&quot; prevent execution and see what <em>would</em> have been executed, or <code>debug=TRUE</code> to have the replaced code printed in addition to being executed:</p>
<div>
<pre><code><span>let</span>(
  <span>c</span>(<span>VAR =</span> <span>'a'</span>),
  <span>eval =</span> <span>FALSE</span>,
  {
    VAR +<span> </span>VAR
  }
)
 <span># {</span>
 <span># a + a</span>
 <span># }</span>

<span>let</span>(
  <span>c</span>(<span>VAR =</span> <span>'a'</span>),
  <span>debugPrint =</span> <span>TRUE</span>,
  {
    VAR +<span> </span>VAR
  }
)
 <span># {</span>
 <span># a + a</span>
 <span># }</span>
 <span># [1] 14</span></code></pre>
</div>
<p>Please see <code>vignette('let', package='wrapr')</code> for more examples. For working with <code>dplyr</code> 0.7.* we suggest also taking a look at an alternate approach called <a rel="nofollow" href="https://github.com/WinVector/seplyr/blob/master/README.md" target="_blank"><code>seplyr</code></a>.</p>
<h2><code>%.>%</code> (dot arrow pipe)</h2>
<p><code>%.>%</code> dot arrow pipe is a strict pipe with intended semantics:</p>
<blockquote>
<p>&quot;<code>a %.>% b</code>&quot; is to be treated as if the user had written &quot;<code>{ . &lt;- a; b };</code>&quot; with &quot;<code>%.>%</code>&quot; being treated as left-associative, and <code>.</code>-side effects removed.</p>
</blockquote>
<p>That is: <code>%.>%</code> does not alter any function arguments that are not explicitly named. It is not defined as <code>a %.% b ~ b(a)</code> (roughly <code>dplyr</code>‘s original pipe) or as the large set of differing cases constituting <code>magrittr::%>%</code>. <code>%.>%</code> is designed to be explicit and simple.</p>
<p>The effect looks is show below.</p>
<p>The following two expressions should be equivalent:</p>
<div>
<pre><code><span>cos</span>(<span>exp</span>(<span>sin</span>(<span>4</span>)))
 <span># [1] 0.8919465</span>

<span>4</span> %.>%<span> </span><span>sin</span>(.) %.>%<span> </span><span>exp</span>(.) %.>%<span> </span><span>cos</span>(.)
 <span># [1] 0.8919465</span></code></pre>
</div>
<p>The notation is quite powerful as it treats pipe stages as expression parameterized over the variable &quot;<code>.</code>&quot;. This means you do not need to introduce functions to express stages. The following is a valid dot-pipe:</p>
<div>
<pre><code><span>1</span>:<span>4</span> %.>%<span> </span>.^<span>2</span> 
 <span># [1] 1 4 9 16</span></code></pre>
</div>
<p>The notation is also very regular in that expressions have the same iterpretation be then surrounded by parenthesis, braces, or as-is:</p>
<div>
<pre><code><span>1</span>:<span>4</span> %.>%<span> </span>{ .^<span>2</span> } 
 <span># [1] 1 4 9 16</span>

<span>1</span>:<span>4</span> %.>%<span> </span>( .^<span>2</span> )
 <span># [1] 1 4 9 16</span></code></pre>
</div>
<p>Regularity can be a <em>big</em> advantage in teaching and comprehension. Please see <a rel="nofollow" href="http://www.win-vector.com/blog/2017/07/in-praise-of-syntactic-sugar/" target="_blank">&quot;In Praise of Syntactic Sugar&quot;</a> for more details.</p>
<h2><code>:=</code></h2>
<p><code>:=</code> is the &quot;named map builder&quot;. It allows code such as the following:</p>
<div>
<pre><code><span>'a'</span> :<span>=</span><span> 'x'</span>
 <span># a </span>
 <span># &quot;x&quot;</span></code></pre>
</div>
<p>The important property of named map builder is it accepts values on the left-hand side allowing the following:</p>
<div>
<pre><code>name &lt;-<span> 'variableNameFromElseWhere'</span>
name :<span>=</span><span> 'newBinding'</span>
 <span># variableNameFromElseWhere </span>
 <span># &quot;newBinding&quot;</span></code></pre>
</div>
<p>A nice property is <code>:=</code> commutes (in the sense of algebra or category theory) with <code>R</code>‘s concatenation function <code>c()</code>. That is the following two statements are equivalent:</p>
<div>
<pre><code><span>c</span>(<span>'a'</span>, <span>'b'</span>) :<span>=</span><span> </span><span>c</span>(<span>'x'</span>, <span>'y'</span>)
 <span># a b </span>
 <span># &quot;x&quot; &quot;y&quot;</span>

<span>c</span>(<span>'a'</span> :<span>=</span><span> 'x'</span>, <span>'b'</span> :<span>=</span><span> 'y'</span>)
 <span># a b </span>
 <span># &quot;x&quot; &quot;y&quot;</span></code></pre>
</div>
<h2><code>λ()</code></h2>
<p><code>λ()</code> is a concise abstract function creator. It is a placeholder that allows the use of the λ-character for very concise function abstraction.</p>
<p>Example:</p>
<div>
<pre><code><span># Make sure lambda function builder is in our enironment.</span>
if(<span>exists</span>(<span>'defineLambda'</span>, 
          <span>envir=</span><span>as.environment</span>(<span>'package:wrapr'</span>), 
          <span>mode=</span><span>'function'</span>)) {
  <span># Note: prior to version 0.4.2 wrapr</span>
  <span># loaded a lambda-definition during</span>
  <span># package load. The following explicit </span>
  <span># wrapr::defineLambda() is more polite.</span>
  wrapr::<span>defineLambda</span>() 
}

<span># square numbers 1 through 4</span>
<span>sapply</span>(<span>1</span>:<span>4</span>, λ(x, x^<span>2</span>))
 <span># [1] 1 4 9 16</span></code></pre>
</div>
<h2><code>DebugFnW()</code></h2>
<p><code>DebugFnW()</code> wraps a function for debugging. If the function throws an exception the execution context (function arguments, function name, and more) is captured and stored for the user. The function call can then be reconstituted, inspected and even re-run with a step-debugger. Please see our <a rel="nofollow" href="https://youtu.be/-P9UzQuJSH8?list=PLAKBwakacHbQT51nPHex1on3YNCCmggZA" target="_blank">free debugging video series</a> and <code>vignette('DebugFnW', package='wrapr')</code> for examples.</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/08/wrapr-r-code-sweeteners/"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/5GyDaJvtm5g" height="1"></a></div>