<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-1-4-1-now-up-on-cran/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/05/wrapr-1-4-1-now-up-on-cran/"> 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 1.4.1</code></a> is <a rel="nofollow" href="https://cran.r-project.org/package=wrapr" target="_blank">now available on CRAN</a>. <code>wrapr</code> is a really neat <code>R</code> package both organizing, meta-programming, and debugging R code. This update generalizes the dot-pipe feature’s dot S3 features.</p>
<div>
<center><img alt="" src="https://i1.wp.com/github.com/WinVector/wrapr/raw/master/tools/wraprs.png?w=660&ssl=1" data-recalc-dims="1"></center>
</div>
<p>Please give it a try!</p>
<p><span></span></p>
<p>
<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"><code>R</code></a> package that supplies powerful tools for writing and debugging <code>R</code> code.</p>
<h2>Introduction</h2>
<p>Primary <code>wrapr</code> services include:</p>
<ul>
<li><code>let()</code> (let block)</li>
<li><code>%.>%</code> (dot arrow pipe)</li>
<li><code>build_frame()</code>/<code>draw_frame()</code></li>
<li><code>:=</code> (named map builder)</li>
<li><code>DebugFnW()</code> (function debug wrappers)</li>
<li><code>λ()</code> (anonymous function builder)</li>
</ul>
<h2><a rel="nofollow" href="https://winvector.github.io/wrapr/articles/let.html" target="_blank"><code>let()</code></a></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;a + a&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># $VAR</span>
 <span># [1] &quot;a&quot;</span>
 <span># </span>
 <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. Some formal documentation can be found <a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/wrapr_let.pdf" target="_blank">here</a>.</p>
<p>For working with <code>dplyr</code> <code>0.7.*</code> we strongly suggest <code>wrapr::let()</code> (or even 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><a rel="nofollow" href="https://winvector.github.io/wrapr/articles/dot_pipe.html" target="_blank"><code>%.>%</code> (dot pipe or dot arrow)</a></h2>
<p><code>%.>%</code> dot arrow pipe is a pipe with intended semantics:</p>
<blockquote>
<p>
&quot;<code>a %.>% b</code>&quot; is to be treated <em>approximately</em> as if the user had written &quot;<code>{ . &lt;- a; b };</code>&quot; with &quot;<code>%.>%</code>&quot; being treated as left-associative.
</p>
</blockquote>
<p>Other <code>R</code> pipes include <a rel="nofollow" href="https://cran.r-project.org/package=magrittr" target="_blank"><code>magrittr</code></a> and <a rel="nofollow" href="https://cran.r-project.org/package=pipeR" target="_blank"><code>pipeR</code></a>.</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 as we show below.</p>
<div>
<pre><code><span>1</span>:<span>4</span> %.>%<span> </span>sin
 <span># [1] 0.8414710 0.9092974 0.1411200 -0.7568025</span>
<span>1</span>:<span>4</span> %.>%<span> </span><span>sin</span>(.)
 <span># [1] 0.8414710 0.9092974 0.1411200 -0.7568025</span>
<span>1</span>:<span>4</span> %.>%<span> </span>base::sin
 <span># [1] 0.8414710 0.9092974 0.1411200 -0.7568025</span>
<span>1</span>:<span>4</span> %.>%<span> </span>base::<span>sin</span>(.)
 <span># [1] 0.8414710 0.9092974 0.1411200 -0.7568025</span>

<span>1</span>:<span>4</span> %.>%<span> </span>function(x) { x +<span> </span><span>1</span> }
 <span># [1] 2 3 4 5</span>
<span>1</span>:<span>4</span> %.>%<span> </span>(function(x) { x +<span> </span><span>1</span> })
 <span># [1] 2 3 4 5</span>

<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. Some formal documentation can be found <a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/wrapr_pipe.pdf" target="_blank">here</a>.</p>
<ul>
<li>
Some obvious &quot;dot-free&quot;&quot; right-hand sides are rejected. Pipelines are meant to move values through a sequence of transforms, and not just for side-effects. Example: <code>5 %.>% 6</code> deliberately stops as <code>6</code> is a right-hand side that obviously does not use its incoming value. This check is only applied to values, not functions on the right-hand side.
</li>
<li>
Trying to pipe into a an &quot;zero argument function evaluation expression&quot; such as <code>sin()</code> is prohibited as it looks too much like the user declaring <code>sin()</code> takes no arguments. One must pipe into either a function, function name, or an non-trivial expression (such as <code>sin(.)</code>). A useful error message is returned to the user: <code>wrapr::pipe does not allow direct piping into a no-argument function call expression (such as &quot;sin()&quot; please use sin(.))</code>.
</li>
<li>
Some reserved words can not be piped into. One example is <code>5 %.>% return(.)</code> is prohibited as the obvious pipe implementation would not actually escape from user functions as users may intend.
</li>
<li>
Obvious de-references (such as <code>$</code>, <code>::</code>, <code>@</code>, and a few more) on the right-hand side are treated performed (example: <code>5 %.>% base::sin(.)</code>).
</li>
<li>
Outer parenthesis on the right-hand side are removed (example: <code>5 %.>% (sin(.))</code>).
</li>
<li>
Anonymous function constructions are evaluated so the function can be applied (example: <code>5 %.>% function(x) {x+1}</code> returns 6, just as <code>5 %.>% (function(x) {x+1})(.)</code> does).
</li>
<li>
Checks and transforms are not performed on items inside braces (example: <code>5 %.>% { function(x) {x+1} }</code> returns <code>function(x) {x+1}</code>, not 6).
</li>
</ul>
<h2><a rel="nofollow" href="https://winvector.github.io/wrapr/reference/build_frame.html" target="_blank"><code>build_frame()</code></a>/<a rel="nofollow" href="https://winvector.github.io/wrapr/reference/draw_frame.html" target="_blank"><code>draw_frame()</code></a></h2>
<p><code>build_frame()</code> is a convenient way to type in a small example <code>data.frame</code> in natural row order. This can be very legible and saves having to perform a transpose in one’s head. <code>draw_frame()</code> is the complimentary function that formats a given <code>data.frame</code> (and is a great way to produce neatened examples).</p>
<div>
<pre><code>x &lt;-<span> </span><span>build_frame</span>(
   <span>&quot;measure&quot;</span>                   , <span>&quot;training&quot;</span>, <span>&quot;validation&quot;</span> |
<span> &quot;minus binary cross entropy&quot;</span>, <span>5</span>         , -<span>7</span>           |
<span> &quot;accuracy&quot;</span>                  , <span>0.8</span>       , <span>0.6</span>          )
<span>print</span>(x)
 <span># measure training validation</span>
 <span># 1 minus binary cross entropy 5.0 -7.0</span>
 <span># 2 accuracy 0.8 0.6</span>
<span>str</span>(x)
 <span># 'data.frame': 2 obs. of 3 variables:</span>
 <span># $ measure : chr &quot;minus binary cross entropy&quot; &quot;accuracy&quot;</span>
 <span># $ training : num 5 0.8</span>
 <span># $ validation: num -7 0.6</span>
<span>cat</span>(<span>draw_frame</span>(x))
 <span># build_frame(</span>
 <span># &quot;measure&quot; , &quot;training&quot;, &quot;validation&quot; |</span>
 <span># &quot;minus binary cross entropy&quot;, 5 , -7 |</span>
 <span># &quot;accuracy&quot; , 0.8 , 0.6 )</span></code></pre>
</div>
<h2><a rel="nofollow" href="https://winvector.github.io/seplyr/articles/named_map_builder.html" target="_blank"><code>:=</code> (named map builder)</a></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>
<p>The named map builder is <a rel="nofollow" href="https://winvector.github.io/seplyr/articles/named_map_builder.html" target="_blank">designed to synergize with <code>seplyr</code></a>.</p>
<h2><a rel="nofollow" href="https://winvector.github.io/wrapr/articles/DebugFnW.html" target="_blank"><code>DebugFnW()</code></a></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>
<h2><a rel="nofollow" href="https://winvector.github.io/wrapr/articles/lambda.html" target="_blank"><code>λ()</code> (anonymous function builder)</a></h2>
<p><code>λ()</code> is a concise abstract function creator or &quot;<a rel="nofollow" href="https://en.wikipedia.org/wiki/Lambda_calculus" target="_blank">lambda abstraction</a>&quot;. 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>
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>Installing</h2>
<p>Install with either:</p>
<div>
<pre><code><span>install.packages</span>(<span>&quot;wrapr&quot;</span>)</code></pre>
</div>
<p>or</p>
<div>
<pre><code><span># install.packages(&quot;devtools&quot;)</span>
devtools::<span>install_github</span>(<span>&quot;WinVector/wrapr&quot;</span>)</code></pre>
</div>
<h2>More Information</h2>
<p>More details on <code>wrapr</code> capabilities can be found in the following two technical articles:</p>
<ul>
<li>
<a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/wrapr_let.pdf" target="_blank">let</a>
</li>
<li>
<a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/wrapr_pipe.pdf" target="_blank">Dot-Pipe</a>
</li>
</ul>
<h2>Note</h2>
<p>Note: <code>wrapr</code> is meant only for &quot;tame names&quot;, that is: variables and column names that are also valid <em>simple</em> (without quotes) <code>R</code> variables names.</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/2018/05/wrapr-1-4-1-now-up-on-cran/"> 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/8l628wA48qE" height="1"></a></div>