<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/better-r-code-with-wrapr-dot-arrow/"></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/better-r-code-with-wrapr-dot-arrow/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Our <a rel="nofollow" href="https://www.r-project.org/" target="_blank"><code>R</code></a> package <a rel="nofollow" href="https://cran.r-project.org/package=wrapr" target="_blank"><code>wrapr</code></a> supplies a &quot;piping operator&quot; that we feel is a real improvement in R code piped-style coding.</p>
<p>The idea is: with <code>wrapr</code>‘s &quot;dot arrow&quot; pipe &quot;<code>%.>%</code>&quot; the expression &quot;<code>A %.>% B</code>&quot; is treated very much like &quot;<code>{. &lt;- A; B}</code>&quot;. In particular this lets users think of &quot;<code>A %.>% B(.)</code>&quot; as a left-to-right way to write &quot;<code>B(A)</code>&quot; (i.e. under the convention of writing-out the dot arguments, the pipe looks a bit like left to right function composition, call this <a rel="nofollow" href="http://www.win-vector.com/blog/2018/03/r-tip-make-arguments-explicit-in-magrittr-dplyr-pipelines/" target="_blank">explicit dot notation</a>).</p>
<p>This sort of notation becomes useful when we compose many steps. Some consider &quot;<code>A %.>% B(.) %.>% C(.) %.>% D(.)</code>&quot; to be easier to read and easier to maintain than &quot;<code>D(C(B(A)))</code>&quot;.</p>
<p><span></span></p>
<p>
<p>In terms of the popular <a rel="nofollow" href="https://cran.r-project.org/package=magrittr" target="_blank"><code>magrittr</code></a> pipe: <code>wrapr</code> dot arrow &quot;<code>A %.>% B</code>&quot; behaves a lot like &quot;<code>A %>% {B}</code>&quot; (&quot;<code>{}</code>&quot; on the right being <code>magrittr</code>‘s special notation to treat the right-hand side as an expression instead of as a function call). Like <code>magrittr</code>, <code>wrapr</code> dot arrow can be used to write &quot;<code>sin(5)</code>&quot; as &quot;<code>5 %.>% sin</code>&quot;, though the preferred <code>wrapr</code> notation is the <a rel="nofollow" href="http://www.win-vector.com/blog/2018/03/r-tip-make-arguments-explicit-in-magrittr-dplyr-pipelines/" target="_blank">explicit dot notation</a>: &quot;<code>5 %.>% sin(.)</code>&quot;. Unlike <code>magrittr</code>, <code>wrapr</code> deliberately does not accept &quot;only parenthesis notation&quot; &quot;<code>5 %>% sin()</code>&quot; (our view is: if the user goes to all the trouble to tell us there are no arguments, <code>wrapr</code> dot arrow should take their word for it).</p>
<p><code>wrapr</code> dot arrow strives to be regular (have few different operating modes), giving the user a lot options and a lot of expressive power. Please see <a rel="nofollow" href="http://www.win-vector.com/blog/2018/04/magrittr-and-wrapr-pipes-in-r-an-examination/" target="_blank">here</a> for a small study we conducted on this.</p>
<p>Lets show a few examples that all share a secret feature that we will reveal at the end of this article (note, any example prior to here is not part of the secret).</p>
<p>First we can pipe into package qualified functions (with or without the explicit dot notation).</p>
<div>
<pre><code><span>library</span>(<span>&quot;wrapr&quot;</span>)

<span>5</span> <span>%.>%</span><span> </span>base<span>::</span>sin</code></pre>
</div>
<pre><code>## [1] -0.9589243</code></pre>
<p>In the function notation (without the dot) argument names are preserved.</p>
<div>
<pre><code><span>library</span>(<span>&quot;wrapr&quot;</span>)

d &lt;-<span> </span><span>5</span>

d <span>%.>%</span><span> </span>substitute</code></pre>
</div>
<pre><code>## d</code></pre>
<div>
<pre><code>d <span>%.>%</span><span> </span>base<span>::</span>substitute</code></pre>
</div>
<pre><code>## d</code></pre>
<p>Also, piping into functions held as <code>list</code> items is supported.</p>
<div>
<pre><code><span>library</span>(<span>&quot;wrapr&quot;</span>)

obj &lt;-<span> </span><span>list</span>(<span>f =</span> sin)

<span>5</span> <span>%.>%</span><span> </span>obj<span>$</span>f</code></pre>
</div>
<pre><code>## [1] -0.9589243</code></pre>
<div>
<pre><code><span>5</span> <span>%.>%</span><span> </span>obj[[<span>'f'</span>]]</code></pre>
</div>
<pre><code>## [1] -0.9589243</code></pre>
<p>And piping into parenthesized expressions is supported, so it is safe to add clarifying parenthesis when one wishes.</p>
<div>
<pre><code><span>library</span>(<span>&quot;wrapr&quot;</span>)

<span>5</span> <span>%.>%</span><span> </span>(<span>1</span> <span>+</span><span> </span>.)</code></pre>
</div>
<pre><code>## [1] 6</code></pre>
<p>In the dot notation piping into nested functions is handled smoothly.</p>
<div>
<pre><code><span># From http://piccolboni.info/2015/09/pipe-operator-for-R.html</span>
<span>library</span>(<span>&quot;wrapr&quot;</span>)

<span>4</span> <span>%.>%</span><span> </span><span>sqrt</span>(<span>sqrt</span>(.))</code></pre>
</div>
<pre><code>## [1] 1.414214</code></pre>
<p><code>wrapr</code> dot arrow is compatible with <code>dplyr</code> &quot;pronoun&quot; notation (the &quot;<code>.data</code>&quot; and &quot;<code>.env</code>&quot; qualifiers below).</p>
<div>
<pre><code><span># Adapted from https://github.com/tidyverse/dplyr/issues/3286</span>
<span>suppressPackageStartupMessages</span>(<span>library</span>(<span>&quot;dplyr&quot;</span>))
<span>library</span>(<span>&quot;wrapr&quot;</span>)

cyl &lt;-<span> </span><span>4</span>

mtcars <span>%.>%</span><span> </span>
<span> </span><span>filter</span>(., .data<span>$</span>cyl <span>==</span><span> </span>.env<span>$</span>cyl) <span>%.>%</span><span> </span>
<span> </span><span>head</span>(.)</code></pre>
</div>
<pre><code>##    mpg cyl  disp hp drat    wt  qsec vs am gear carb
## 1 22.8   4 108.0 93 3.85 2.320 18.61  1  1    4    1
## 2 24.4   4 146.7 62 3.69 3.190 20.00  1  0    4    2
## 3 22.8   4 140.8 95 3.92 3.150 22.90  1  0    4    2
## 4 32.4   4  78.7 66 4.08 2.200 19.47  1  1    4    1
## 5 30.4   4  75.7 52 4.93 1.615 18.52  1  1    4    2
## 6 33.9   4  71.1 65 4.22 1.835 19.90  1  1    4    1</code></pre>
<p><code>wrapr</code> dot arrow can even be used <em>inside</em> <code>dplyr</code> expressions.</p>
<div>
<pre><code><span>suppressPackageStartupMessages</span>(<span>library</span>(<span>&quot;dplyr&quot;</span>))
<span>library</span>(<span>&quot;wrapr&quot;</span>)

mtcars <span>%.>%</span>
<span> </span><span>mutate</span>(., 
         <span>someMean =</span> . <span>%.>%</span><span> </span>
<span> </span><span>select</span>(., cyl, disp, carb) <span>%.>%</span><span> </span>
<span> </span><span>rowMeans</span>(.)) <span>%.>%</span><span> </span>
<span> </span><span>head</span>(.)</code></pre>
</div>
<pre><code>##    mpg cyl disp  hp drat    wt  qsec vs am gear carb  someMean
## 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4  56.66667
## 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4  56.66667
## 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1  37.66667
## 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1  88.33333
## 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 123.33333
## 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1  77.33333</code></pre>
<p>Some nested items can be subtle, for in the example below the dot in &quot;<code>rev(.)[2]</code>&quot; is actually not a top-level argument as the &quot;<code>rev(.)</code>&quot; expression is technically an argument to the <code>[]</code> operator. Unlike <code>magrittr</code>, <code>wrapr</code> does not change its substitution behavior based on the presence or absence of an argument named &quot;<code>.</code>&quot; at the top-level of expressions, making it easy to reason about expression semantics.</p>
<div>
<pre><code><span>library</span>(<span>&quot;wrapr&quot;</span>)

<span>c</span>(<span>10</span>, <span>20</span>, <span>30</span>) <span>%.>%</span><span> </span><span>rev</span>(.)[<span>1</span>]</code></pre>
</div>
<pre><code>## [1] 30</code></pre>
<p><code>wrapr</code> dot arrow is compatible with <a rel="nofollow" href="https://cran.r-project.org/package=data.table" target="_blank"><code>data.table</code></a>, even with the common addition of visibility controlling <code>[]</code>-calls.</p>
<div>
<pre><code><span>library</span>(<span>&quot;data.table&quot;</span>)
<span>library</span>(<span>&quot;wrapr&quot;</span>)

d &lt;-<span> </span><span>data.table</span>(<span>x =</span> <span>c</span>(<span>2</span>, <span>1</span>, <span>3</span>))

d <span>%.>%</span><span> </span>
<span> </span>.[, y <span>:</span><span>=</span><span> </span>x <span>+</span><span> </span><span>1</span>] <span>%.>%</span><span> </span>
<span> </span><span>setorder</span>(., x)[]</code></pre>
</div>
<pre><code>##    x y
## 1: 1 2
## 2: 2 3
## 3: 3 4</code></pre>
<p>(Note: if the <code>wrapr</code> package is attached before <code>data.table</code> their will be a warning regarding &quot;<code>:=</code>&quot;. This is actually not a problem as <code>data.table</code> uses the &quot;<code>:=</code>&quot; symbol in its own parsing and in its own package context where it can not be confused with <code>wrapr</code>‘s definition.)</p>
<p>Another subtle form of expression nesting are operators such as <code>%in%</code>. In expressions such as &quot;<code>filter(d, a %in% .)</code>&quot;, the dot is not an argument to <code>filter()</code> but an argument to <code>%in%</code>.</p>
<div>
<pre><code><span># adapted from: https://stackoverflow.com/questions/46728387/piping-with-dot-inside-dplyrfilter</span>
<span>suppressPackageStartupMessages</span>(<span>library</span>(<span>&quot;dplyr&quot;</span>))
<span>library</span>(<span>&quot;wrapr&quot;</span>)

d &lt;-<span> </span><span>data.frame</span>(<span>a =</span> <span>c</span>(<span>1</span>,<span>2</span>,<span>3</span>),
                <span>b =</span> <span>c</span>(<span>4</span>,<span>5</span>,<span>6</span>))
<span>c</span>(<span>2</span>,<span>2</span>) <span>%.>%</span><span> </span><span>filter</span>(d, a <span>%in%</span><span> </span>.)</code></pre>
</div>
<pre><code>##   a b
## 1 2 5</code></pre>
<div>
<pre><code><span>c</span>(<span>2</span>,<span>2</span>) <span>%.>%</span><span> </span>dplyr<span>::</span><span>filter</span>(d, a <span>%in%</span><span> </span>.)</code></pre>
</div>
<pre><code>##   a b
## 1 2 5</code></pre>
<p>These are all of our examples. We are now ready to share their common secret.</p>
<blockquote>
<p>
<em>None</em> of the above examples work when translated into <code>magrittr</code> pipelines (unless one adds braces and explicit dot-arguments).
</p>
</blockquote>
<p>When we were not thinking about it deeply all of the above code likely seemed reasonable. In fact it <em>was</em> reasonable, it just doesn’t happen to follow <code>magrittr</code> conventions.</p>
<p>We call out just a couple to show the issues.</p>
<div>
<pre><code><span>library</span>(<span>&quot;magrittr&quot;</span>)

<span>5</span> <span>%>%</span><span> </span>base<span>::</span>sin</code></pre>
</div>
<pre><code>## Error in .::base: unused argument (sin)</code></pre>
<div>
<pre><code><span>library</span>(<span>&quot;magrittr&quot;</span>)

d &lt;-<span> </span><span>5</span>

d <span>%>%</span><span> </span>substitute</code></pre>
</div>
<pre><code>## value</code></pre>
<p>The above illustrates a problem we run into in promoting <code>wrapr</code> dot arrow: the misconception that the <code>magrittr</code> pipe leaves no room for improvement.</p>
<p>Our impression is <code>magrittr</code> users eventually learn and internalize what variations work and do not work for <code>magrittr</code>, and then learn to limit their coding to stay in the safe <code>magrittr</code> fragment. In our opinion once one learns the one limitation of <code>wrapr</code> dot arrow (the requirement of explicit dot arguments, due to the expression orientation of the pipe) one can use dot arrow as a much more powerful pipe that supports many more coding patterns (such piping into function references, as we showed above).</p>
<p>For those that are interested we have <a rel="nofollow" href="https://winvector.github.io/wrapr/articles/dot_pipe.html" target="_blank">tutorials</a> and <a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/wrapr_pipe.pdf" target="_blank">formal documentation</a>.</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/better-r-code-with-wrapr-dot-arrow/"> 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/Ppsz2dKZzi4" height="1"></a></div>