<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/announcing-wrapr-1-6-2/"></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/09/announcing-wrapr-1-6-2/"> 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> <code>1.6.2</code> is now up on <a rel="nofollow" href="https://cran.r-project.org/package=wrapr" target="_blank">CRAN</a>. We have some neat new features for <a rel="nofollow" href="https://www.r-project.org/" target="_blank"><code>R</code></a> users to try (in addition to many <a rel="nofollow" href="http://www.win-vector.com/blog/2018/01/supercharge-your-r-code-with-wrapr/" target="_blank">earlier <code>wrapr</code> goodies</a>).</p>
<p><center><br>
<a rel="nofollow" href="https://winvector.github.io/wrapr/" target="_blank"><img src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/11/wraprs.png?w=660" data-recalc-dims="1"></a><br>
</center></p>
<p><span></span></p>
<p>
<p>The first is the <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/grapes-in_block-grapes.html" target="_blank"><code>%in_block%</code></a> alternate notation for <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/let.html" target="_blank"><code>let()</code></a>.</p>
<p>The <a rel="nofollow" href="https://winvector.github.io/wrapr/index.html" target="_blank"><code>wrapr</code></a> <code>let()</code>-block allows easy replacement of names in name-capturing interfaces (such as <code>transform()</code>), as we show below.</p>
<div>
<pre><code><span>library</span>(<span>&quot;wrapr&quot;</span>)

column_mapping &lt;-<span> </span><span>qc</span>(
  <span>AREA_COL =</span> Sepal.Area,
  <span>LENGTH_COL =</span> Sepal.Length,
  <span>WIDTH_COL =</span> Sepal.Width
)

<span># let-block notation</span>
<span>let</span>(
  <span>alias =</span> column_mapping,
  
  iris <span>%.>%</span>
<span> </span><span>transform</span>(.,
              <span>AREA_COL =</span> (pi<span>/</span><span>4</span>)<span>*</span>LENGTH_COL<span>*</span>WIDTH_COL) <span>%.>%</span>
<span> </span><span>subset</span>(., 
           <span>select =</span> <span>qc</span>(Species, AREA_COL)) <span>%.>%</span>
<span> </span><span>head</span>(.)
)</code></pre>
</div>
<pre><code>##   Species Sepal.Area
## 1  setosa   14.01936
## 2  setosa   11.54535
## 3  setosa   11.81239
## 4  setosa   11.19978
## 5  setosa   14.13717
## 6  setosa   16.54049</code></pre>
<p>The <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/qc.html" target="_blank"><code>qc()</code></a> notation allowed us to specify a named-<code>vector</code> without quotes. <code>qc(a = b)</code> is equivalent to <code>c(&quot;a&quot; = &quot;b&quot;)</code>.</p>
<p>With the <code>%in_block%</code> operator notation one writes the <code>let()</code>-block as an in-line operator supplying the mapping into a code block. The above example can now be re-written as the following.</p>
<div>
<pre><code><span># %in_block% notation</span>
column_mapping <span>%in_block%</span><span> </span>{
  iris <span>%.>%</span>
<span> </span><span>transform</span>(., 
              <span>AREA_COL =</span> (pi<span>/</span><span>4</span>)<span>*</span>LENGTH_COL<span>*</span>WIDTH_COL)  <span>%.>%</span>
<span> </span><span>subset</span>(., 
           <span>select =</span> <span>qc</span>(Species, AREA_COL)) <span>%.>%</span>
<span> </span><span>head</span>(.)
}</code></pre>
</div>
<pre><code>##   Species Sepal.Area
## 1  setosa   14.01936
## 2  setosa   11.54535
## 3  setosa   11.81239
## 4  setosa   11.19978
## 5  setosa   14.13717
## 6  setosa   16.54049</code></pre>
<p>This notation can be handy for defining functions.</p>
<div>
<pre><code>compute_area &lt;-<span> </span><span>function</span>(
  .data, 
  area_col, 
  length_col, 
  width_col) <span>c</span>(  <span># End of function argument definiton</span>
    <span>AREA_COL =</span> area_col,
    <span>LENGTH_COL =</span> length_col,
    <span>WIDTH_COL =</span> width_col
  ) <span>%in_block%</span><span> </span>{ <span># End of argument mapping block</span>
    .data <span>%.>%</span>
<span> </span><span>transform</span>(., 
                <span>AREA_COL =</span> (pi<span>/</span><span>4</span>)<span>*</span>LENGTH_COL<span>*</span>WIDTH_COL)
  }              <span># End of function body block</span>

iris <span>%.>%</span>
<span> </span><span>compute_area</span>(., 
               <span>'Sepal.Area'</span>, <span>'Sepal.Length'</span>, <span>'Sepal.Width'</span>) <span>%.>%</span>
<span> </span><span>compute_area</span>(., 
               <span>'Petal.Area'</span>, <span>'Petal.Length'</span>, <span>'Petal.Width'</span>) <span>%.>%</span>
<span> </span><span>subset</span>(., 
         <span>select =</span> <span>c</span>(<span>&quot;Species&quot;</span>, <span>&quot;Sepal.Area&quot;</span>, <span>&quot;Petal.Area&quot;</span>)) <span>%.>%</span>
<span> </span><span>head</span>(.)</code></pre>
</div>
<pre><code>##   Species Sepal.Area Petal.Area
## 1  setosa   14.01936  0.2199115
## 2  setosa   11.54535  0.2199115
## 3  setosa   11.81239  0.2042035
## 4  setosa   11.19978  0.2356194
## 5  setosa   14.13717  0.2199115
## 6  setosa   16.54049  0.5340708</code></pre>
<p>We can think of the above function definition notation as having two blocks: the alias defining block (the portion before &quot;<code>%in_block%</code>&quot;) and the templated function body (the portion after &quot;<code>%in_block%</code>&quot;). Notice how easy it is to use this notation to convert a non-standard (or name/code-capturing interface) into a value-oriented interface. The point is value-oriented interfaces are much more re-usable and easier to program over (use in for-loops, applies, and functions).</p>
<p>The second new feature is the <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/orderv.html" target="_blank"><code>orderv()</code></a> function, a value-oriented adapter for <code>base::order()</code>. <code>orderv()</code> uses a vector of column names to compute an ordering permutation for a <code>data.frame</code>. We can use it as we show below.</p>
<div>
<pre><code><span>library</span>(<span>&quot;wrapr&quot;</span>)

sort_columns &lt;-<span> </span><span>qc</span>(mpg, hp, gear)
ordering &lt;-<span> </span><span>orderv</span>(mtcars[ , sort_columns, <span>drop =</span> <span>FALSE</span>],
                   <span>decreasing =</span> <span>TRUE</span>,
                   <span>method =</span> <span>&quot;radix&quot;</span>)
<span>head</span>(mtcars[ordering, , <span>drop =</span> <span>FALSE</span>])</code></pre>
</div>
<pre><code>##                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2</code></pre>
<p>Of course we have also have all the steps wrapped in a convenient function: <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/sortv.html" target="_blank"><code>sortv()</code></a>.</p>
<div>
<pre><code>mtcars <span>%.>%</span>
<span> </span><span>sortv</span>(., 
        sort_columns,  
        <span>decreasing =</span> <span>TRUE</span>,
        <span>method =</span> <span>&quot;radix&quot;</span>) <span>%.>%</span>
<span> </span><span>head</span>(.)</code></pre>
</div>
<pre><code>##                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2</code></pre>
<p>For details on &quot;<code>method = &quot;radix&quot;</code>&quot; please see our earlier tip <a rel="nofollow" href="http://www.win-vector.com/blog/2018/08/r-tip-use-radix-sort/" target="_blank">here</a>.</p>
<p>A third new feature is <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/mk_formula.html" target="_blank"><code>mk_formula()</code></a>. <code>mk_formula()</code> is used to build simple formulas for modeling tasks (which may have a large number of variables) without any string processing or parsing.</p>
<p>Our usual advice for building simple formulas has been to use the <code>paste()</code>-based methods exhibited in <a rel="nofollow" href="http://www.win-vector.com/blog/2018/09/r-tip-how-to-pass-a-formula-to-lm/" target="_blank">&quot;R Tip: How to Pass a formula to lm&quot;</a>. This remains good advice. However <code>mk_formula()</code> is a more concise and more hygienic alternative. An example is given below.</p>
<div>
<pre><code><span># specifications of how to model,</span>
<span># coming from somewhere else</span>
outcome &lt;-<span> &quot;mpg&quot;</span>
variables &lt;-<span> </span><span>c</span>(<span>&quot;cyl&quot;</span>, <span>&quot;disp&quot;</span>, <span>&quot;hp&quot;</span>, <span>&quot;carb&quot;</span>)

<span># our modeling effort, </span>
<span># fully parameterized!</span>
f &lt;-<span> </span>wrapr<span>::</span><span>mk_formula</span>(outcome, variables)
<span>print</span>(f)</code></pre>
</div>
<pre><code>## mpg ~ cyl + disp + hp + carb</code></pre>
<div>
<pre><code>
model &lt;-<span> </span><span>lm</span>(f, <span>data =</span> mtcars)
<span>print</span>(model)</code></pre>
</div>
<pre><code>## 
## Call:
## lm(formula = f, data = mtcars)
## 
## Coefficients:
## (Intercept)          cyl         disp           hp         carb  
##   34.021595    -1.048523    -0.026906     0.009349    -0.926863</code></pre>
<p>The above notation is good for programming over modeling tasks.</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/09/announcing-wrapr-1-6-2/"> 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/mPFzamxINnc" height="1"></a></div>