<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/magrittr-and-wrapr-pipes-in-r-an-examination/"></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/04/magrittr-and-wrapr-pipes-in-r-an-examination/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Let’s consider piping in <a rel="nofollow" href="https://www.r-project.org/" target="_blank"><code>R</code></a> both using the <a rel="nofollow" href="https://cran.r-project.org/package=magrittr" target="_blank"><code>magrittr</code></a> package and using the <a rel="nofollow" href="https://cran.r-project.org/package=wrapr" target="_blank"><code>wrapr</code></a> package.</p>
<p><span></span></p>
<p>
<div>
<h2><code>magrittr</code> pipelines</h2>
<p>The <code>magittr</code> pipe glyph “<code>%>%</code>” is the most popular piping symbol in <code>R</code>.<br>
<a rel="nofollow" href="https://cran.r-project.org/web/packages/magrittr/README.html" target="_blank"><code>magrittr</code> documentation</a> describes <code>%>%</code> as follow.</p>
<blockquote>
<p>
<b>Basic piping:</b></p>
<ul>
<li>
<code>x %>% f</code> is equivalent to <code>f(x)</code>
</li>
<li>
<code>x %>% f(y)</code> is equivalent to <code>f(x, y)</code>
</li>
<li>
<code>x %>% f %>% g %>% h</code> is equivalent to <code>h(g(f(x)))</code>
</li>
</ul>
<p><b>The argument placeholder</b></p>
<ul>
<li>
<code>x %>% f(y, .)</code> is equivalent to <code>f(y, x)</code>
</li>
<li>
<code>x %>% f(y, z = .)</code> is equivalent to <code>f(y, z = x)</code>
</li>
</ul>
<p><b>Re-using the placeholder for attributes</b></p>
<p>It is straight-forward to use the placeholder several times in a right-hand side expression. However, when the placeholder only appears in a nested expressions magrittr will still apply the first-argument rule. The reason is that in most cases this results more clean code.</p>
<p><code>x %>% f(y = nrow(.), z = ncol(.))</code> is equivalent to <code>f(x, y = nrow(x), z = nrow(x))</code></p>
<p>The behavior can be overruled by enclosing the right-hand side in braces:</p>
<p><code>x %>% {f(y = nrow(.), z = ncol(.))}</code> is equivalent to <code>f(y = nrow(x), z = nrow(x))</code></p>
</blockquote>
<p>That is a bit of simplification, but is the taught mental model.<br>
<a rel="nofollow" href="http://r4ds.had.co.nz/pipes.html" target="_blank">Grolemund, Wickham, <em>R for Data Science</em>, O’Reilly Media, 2017; “Pipes”</a> describes the <code>magrittr</code> pipe as follows.</p>
<blockquote>
<pre>
 foo_foo %>%
   hop(through = forest) %>%
   scoop(up = field_mouse) %>%
   bop(on = head)
</pre>
<p>[…]</p>
<p>The pipe works by performing a “lexical transformation”: behind the scenes, magrittr reassembles the code in the pipe to a form that works by overwriting an intermediate object. When you run a pipe like the one above, magrittr does something like this:</p>
<pre>
 my_pipe &lt;- function(.) {
   . &lt;- hop(., through = forest)
   . &lt;- scoop(., up = field_mice)
   bop(., on = head)
 }
 my_pipe(foo_foo)
</pre>
</blockquote>
<p>Roughly they are saying <code>x %>% f(ARGS)</code> can be considered shorthand for <code>{ . &lt;- x; f(., ARGS) }</code> where the evaluation in question happens in a temporary environment.
</div>
<div>
<h2>A Mental Model for Planning Pipelines</h2>
<p>To safely and confidently use piping one must eventually know what all of the commonly used related notations mean. For example it is important to know what each of the following evaluate to:</p>
<ul>
<li><code>5 %>% sin</code>: the notation demonstrated in the <code>magrittr</code> excerpt.</li>
<li><code>5 %>% sin()</code>: possibly the notation one would abstract from the <em>R for Data Science</em> excerpt.</li>
<li><code>5 %>% sin(.)</code>: the notation <a rel="nofollow" href="http://www.win-vector.com/blog/2018/03/r-tip-make-arguments-explicit-in-magrittr-dplyr-pipelines/" target="_blank">we recomend</a> (especially for <a rel="nofollow" href="http://www.win-vector.com/blog/2017/08/more-on-the-part-time-r-user/" target="_blank">the part time <code>R</code> user</a>).</li>
</ul>
<p>Also, there are questions of how one pipes into general expressions (instead of names, functions, or partially specified function evaluation signatures).<br>
These may seem like details: but they are the steps required to move from copying code from examples and hoping it works (a state of learned helplessness, especially when simple variations fail) or having an effective (even if approximate) mental model for the operators one has decided to work with and plan over.
</p>
</div>
<div>
<h2><code>wrapr</code> pipelines</h2>
<p><code>wrapr</code> supplies its own piping glyph: <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/grapes-.-greater-than-grapes.html" target="_blank">“dot pipe” <code>%.>%</code></a>. <code>wrapr</code>’s goal is to supply an operator that is a regular and safe with <code>a %.>% b</code> being <em>approximately</em> <a rel="nofollow" href="https://en.wikipedia.org/wiki/Syntactic_sugar" target="_blank">syntactic sugar</a> for <code>{ . &lt;- a; b }</code> (with, visible side-effects, i.e. we can actually see the “<code>.</code>” assignment happen).</p>
<pre><code>library(&quot;wrapr&quot;)

# calculate sin(5)
5 %.>% sin(.)</code></pre>
<pre><code>## [1] -0.9589243</code></pre>
<pre><code># 5 left in dot, a visible side-effect
print(.)</code></pre>
<pre><code>## [1] 5</code></pre>
<pre><code># clear dot, so no later failing example 
# falsely appears to work
rm(list = &quot;.&quot;)</code></pre>
<p>We think <code>wrapr</code> piping is very comprehensible (non-magic) expression oriented pipe with a few rules and additional admonitions:</p>
<ul>
<li>Use explicit dots, i.e. write <code>5 %.>% sin(.)</code> and not <code>5 %.>% sin()</code> or <code>5 %.>% sin</code>. It <a rel="nofollow" href="http://www.win-vector.com/blog/2018/03/r-tip-make-arguments-explicit-in-magrittr-dplyr-pipelines/" target="_blank">good to make it obvious to the reader that “<code>.</code>” is a free-name in the right-hand side expression</a>, allowing the easy application of the convention of treating the right-hand side expression as an implicit function of “<code>.</code>”.</li>
<li>You get some free de-referencing such as in <code>5 %.>% sin</code> and function application as in <code>5 %.>% function(x) { sin(x) }</code>.</li>
<li>Outer parentheses do not change meaning (as is commonly the case outside pipelines, modulo <code>R</code>’s visibility controls).</li>
<li>Outer braces treat contents as raw statements, turning off <code>wrapr</code> convenience transforms and safety checking. This is compatible with the subtle <code>R</code> convention that brace-blocks <code>{}</code> are considered more opaque and not as eagerly looked into as parenthesized expressions (one such example can be found <a rel="nofollow" href="https://radfordneal.wordpress.com/2010/08/19/speeding-up-parentheses-and-lots-more-in-r/" target="_blank">here</a>).</li>
<li><code>wrapr</code> is grammar in the sense some statements are deliberately not part of the accepted notation. Some of the “errors” in the next set of examples are in fact <code>wrapr</code> refusing certain pipelines.</li>
<li>Advanced users can extend <code>wrapr</code> by using <code>R</code> <code>S3</code> methodology to specify their own rules for various classes (such as building <a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/ggplot2_piped.md" target="_blank">pipable <code>ggplot2</code> code</a>). Technical details can be found <a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/wrapr_pipe.pdf" target="_blank">here</a>.</li>
</ul>
</div>
<div>
<h2>Examples</h2>
<p>Let’s consider the following attempts of writing piped variations of <code>sin(5)</code> in both <code>magritter</code> and <code>wrapr</code> notations.</p>
<pre><code>exprs = c(
  &quot;5 PIPE_GLYPH sin&quot;,
  &quot;5 PIPE_GLYPH sin()&quot;,
  &quot;5 PIPE_GLYPH sin(.)&quot;,
  &quot;5 PIPE_GLYPH base::sin&quot;,
  &quot;5 PIPE_GLYPH base::sin()&quot;,
  &quot;5 PIPE_GLYPH base::sin(.)&quot;,
  &quot;5 PIPE_GLYPH ( sin )&quot;,
  &quot;5 PIPE_GLYPH ( sin() )&quot;,
  &quot;5 PIPE_GLYPH ( sin(.) )&quot;,
  &quot;5 PIPE_GLYPH { sin }&quot;,
  &quot;5 PIPE_GLYPH { sin() }&quot;,
  &quot;5 PIPE_GLYPH { sin(.) }&quot;,
  &quot;5 PIPE_GLYPH function(x) { sin(x) }&quot;,
  &quot;5 PIPE_GLYPH ( function(x) { sin(x) } )&quot;,
  &quot;5 PIPE_GLYPH { function(x) { sin(x) } }&quot;,
  &quot;f &lt;- function(x) { sin(x) }; 5 PIPE_GLYPH f&quot;
  )</code></pre>
<p>The point is in a room full of students in a lab setting if you show them “<code>5 %>% sin</code>” some of them are going to try variations or have variations from their work that are important to them. This possibly includes: package-qualifying the function name, wrapping expressions in parenthesis, altering arguments, building functions, and retrieving functions from data structures. The pipeline (for convenience) tries to lower the distinctions between expressions, functions, and function names. However the pipeline notation does not completely eliminate the differences.<br>
A non-expert <a rel="nofollow" href="https://cran.r-project.org/package=magrittr" target="_blank"><code>magrittr</code></a>/<a rel="nofollow" href="https://cran.r-project.org/package=dplyr" target="_blank"><code>dplyr</code></a> user might expect all the pipe examples we are about to discuss to evaluate to <code>sin(5)</code> = -0.9589243. As <code>R</code> is routinely used by self-described non-programmers (such as scientists, analysts, and statisticians) the non-expert or <a rel="nofollow" href="http://www.win-vector.com/blog/2017/08/lets-have-some-sympathy-for-the-part-time-r-user/" target="_blank">part time <code>R</code> user</a> is a very important class of <code>R</code> users (and in fact distinct from beginning <code>R</code> users). So how a system meets or misses simplified expectations is quite important in <code>R</code>.<br>
To run our examples we will use a fairly involved function <a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/magritr_diff_wrapr.md" target="_blank"><code>work_examples()</code></a> that takes the vector of examples and returns an annotated <code>data.frame</code> of evaluation results. For completeness this code is given here, but can be safely skipped when reading this article.</p>
<p>Now we can work our examples, and return the comparison in tabular format.</p>
<pre><code>work_examples(exprs, sin(5)) %.>%
  knitr::kable(., format = &quot;html&quot;, escape = FALSE) %.>%
  column_spec(., 1:4, width = &quot;1.75in&quot;) %.>%
  kable_styling(., &quot;striped&quot;, full_width = FALSE)</code></pre>
<table>
<thead>
<tr>
<th>
magrittr expr
</th>
<th>
magrittr res
</th>
<th>
wrapr expr
</th>
<th>
wrapr res
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
5 %>% sin
</td>
<td>
<span>-0.959</span>
</td>
<td>
5 %.>% sin
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% sin()
</td>
<td>
<span>-0.959</span>
</td>
<td>
5 %.>% sin()
</td>
<td>
wrapr::pipe_step.default does not allow direct piping into a no-argument function call expression (such as “sin()”, please use sin(.)).
</td>
</tr>
<tr>
<td>
5 %>% sin(.)
</td>
<td>
<span>-0.959</span>
</td>
<td>
5 %.>% sin(.)
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% base::sin
</td>
<td>
unused argument (sin)
</td>
<td>
5 %.>% base::sin
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% base::sin()
</td>
<td>
<span>-0.959</span>
</td>
<td>
5 %.>% base::sin()
</td>
<td>
wrapr::pipe_step.default does not allow direct piping into a no-argument function call expression (such as “base::sin()”, please use base::sin(.)).
</td>
</tr>
<tr>
<td>
5 %>% base::sin(.)
</td>
<td>
<span>-0.959</span>
</td>
<td>
5 %.>% base::sin(.)
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% ( sin )
</td>
<td>
<span>-0.959</span>
</td>
<td>
5 %.>% ( sin )
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% ( sin() )
</td>
<td>
0 arguments passed to ‘sin’ which requires 1
</td>
<td>
5 %.>% ( sin() )
</td>
<td>
wrapr::pipe_step.default does not allow direct piping into a no-argument function call expression (such as “sin()”, please use sin(.)).
</td>
</tr>
<tr>
<td>
5 %>% ( sin(.) )
</td>
<td>
object ‘.’ not found
</td>
<td>
5 %.>% ( sin(.) )
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% { sin }
</td>
<td>
.Primitive(“sin”)
</td>
<td>
5 %.>% { sin }
</td>
<td>
.Primitive(“sin”)
</td>
</tr>
<tr>
<td>
5 %>% { sin() }
</td>
<td>
0 arguments passed to ‘sin’ which requires 1
</td>
<td>
5 %.>% { sin() }
</td>
<td>
0 arguments passed to ‘sin’ which requires 1
</td>
</tr>
<tr>
<td>
5 %>% { sin(.) }
</td>
<td>
<span>-0.959</span>
</td>
<td>
5 %.>% { sin(.) }
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% function(x) { sin(x) }
</td>
<td>
Anonymous functions myst be parenthesized
</td>
<td>
5 %.>% function(x) { sin(x) }
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% ( function(x) { sin(x) } )
</td>
<td>
<span>-0.959</span>
</td>
<td>
5 %.>% ( function(x) { sin(x) } )
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
5 %>% { function(x) { sin(x) } }
</td>
<td>
function (x) { sin(x) }
</td>
<td>
5 %.>% { function(x) { sin(x) } }
</td>
<td>
function (x) { sin(x) }
</td>
</tr>
<tr>
<td>
f &lt;-function(x) { sin(x) }; 5 %>% f
</td>
<td>
<span>-0.959</span>
</td>
<td>
f &lt;-function(x) { sin(x) }; 5 %.>% f
</td>
<td>
<span>-0.959</span>
</td>
</tr>
</tbody>
</table>
<p>As can now see, some statements were not roughly equivalent to <code>sin(5)</code>.<br>
One related case to consider is the following (which we run by hand, as it seems to default <code>knitr</code> or <code>kableExtra</code> <code>html</code> styling, note: the “‘\[’” and other formatting errors are an artifacts of <code>HTML</code> quoting/rendering, and not part of the expressions):</p>
<pre><code>c(&quot;lst &lt;- list(h = sin); 5 PIPE_GLYPH lst$h&quot;,
  &quot;lst &lt;- list(h = sin); 5 PIPE_GLYPH lst$h()&quot;,
  &quot;lst &lt;- list(h = sin); 5 PIPE_GLYPH lst$h(.)&quot;,
  &quot;lst &lt;- list(h = sin); 5 PIPE_GLYPH lst[['h']]&quot;,
  &quot;lst &lt;- list(h = sin); 5 PIPE_GLYPH lst[['h']]()&quot;,
  &quot;lst &lt;- list(h = sin); 5 PIPE_GLYPH lst[['h']](.)&quot;) %.>%
  work_examples(., sin(5)) %.>%
  knitr::kable(., format = &quot;html&quot;, escape = FALSE)</code></pre>
<table>
<thead>
<tr>
<th>
magrittr expr
</th>
<th>
magrittr res
</th>
<th>
wrapr expr
</th>
<th>
wrapr res
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
lst &lt;-list(h = sin); 5 %>% lst$h
</td>
<td>
3 arguments passed to ‘$’ which requires 2
</td>
<td>
lst &lt;-list(h = sin); 5 %.>% lst$h
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
lst &lt;-list(h = sin); 5 %>% lst$h()
</td>
<td>
<span>-0.959</span>
</td>
<td>
lst &lt;-list(h = sin); 5 %.>% lst$h()
</td>
<td>
wrapr::pipe_step.default does not allow direct piping into a no-argument function call expression (such as “lst$h()”, please use lst$h(.)).
</td>
</tr>
<tr>
<td>
lst &lt;-list(h = sin); 5 %>% lst$h(.)
</td>
<td>
<span>-0.959</span>
</td>
<td>
lst &lt;-list(h = sin); 5 %.>% lst$h(.)
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
lst &lt;-list(h = sin); 5 %>% lst[[‘h’]]
</td>
<td>
incorrect number of subscripts
</td>
<td>
lst &lt;-list(h = sin); 5 %.>% lst[[‘h’]]
</td>
<td>
<span>-0.959</span>
</td>
</tr>
<tr>
<td>
lst &lt;-list(h = sin); 5 %>% lst[[‘h’]]()
</td>
<td>
<span>-0.959</span>
</td>
<td>
lst &lt;-list(h = sin); 5 %.>% lst[[‘h’]]()
</td>
<td>
wrapr::pipe_step.default does not allow direct piping into a no-argument function call expression (such as “lst[[”h“]]()”, please use lst[[“h”]](.)).
</td>
</tr>
<tr>
<td>
lst &lt;-list(h = sin); 5 %>% lst[[‘h’]](.)
</td>
<td>
<span>-0.959</span>
</td>
<td>
lst &lt;-list(h = sin); 5 %.>% lst[[‘h’]](.)
</td>
<td>
<span>-0.959</span>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<h2>Analysis</h2>
<div>
<h3><code>magrittr</code> Results</h3>
<p>The <code>magrittr</code> exceptions include the following.</p>
<ul>
<li><code>::</code> is a function, as so many things are in <code>R</code>. So <code>base::sin</code> is not really the package qualified name for <code>sin()</code>, it is actually shorthand for <code>`::`(&quot;base&quot;, &quot;sin&quot;)</code> which is a function evaluation that performs the look-up. So <code>5 %>% base::sin</code> expands to an analogue of <code>. &lt;- 5; `::`(., &quot;base&quot;, &quot;sin&quot;)</code>, leading to the observed error message.</li>
<li><code>()</code> is <code>magrittr</code>’s “evaluate before piping into” notation, so <code>5 %>% ( sin() )</code> and <code>5 %>% ( sin(.) )</code> both throw an error as evaluation is attempted before any alteration of arguments is attempted.</li>
<li><code>{}</code> is <code>magrittr</code>’s “treat the contents as raw statements” notation (which is not in fact <code>magrittr</code>’s default behavior). Thus <code>magrittr</code>’s function evaluation signature alteration transforms are not applied to <code>5 %>% { sin }</code> or <code>5 %>% { sin() }</code>.</li>
</ul>
<p>Again, the above are not <code>magrittr</code> bugs, they are just how <code>magrittr</code>’s behavior differs from a very regular or naive internalization of <code>magrittr</code> rules. Notice neither of “<code>()</code>” nor “<code>{}</code>” are neutral notations in <code>magrittr</code> (the first adds an extra evaluation, and second switches to an expression mode with fewer substitutions). Also note the above is an argument for preferring “<code>sin(.)</code>” to “<code>sin()</code>”, or “<code>sin</code>”; as “<code>sin(.)</code>” had the most regular <code>magrittr</code> behavior (not changing with the introduction of “<code>()</code>”, “<code>{}</code>”, or “<code>base::</code>”).<br>
Regularity is especially important for <a rel="nofollow" href="http://www.win-vector.com/blog/2017/08/lets-have-some-sympathy-for-the-part-time-r-user/" target="_blank">part time users</a>, as you want reasonable variations of what is taught to work so that experimentation is positive and not an exercise in learned helplessness. It is convenient when your tools happen to work the way you might remember.
</div>
<div>
<h3><code>wrapr</code> Results</h3>
<p>The <code>wrapr</code> error messages and non-numeric returns are driven by the following:</p>
<ul>
<li><code>5 %.>% sin()</code> is not an allowed <code>wrapr</code> notation. The <code>wrapr</code> philosophy is not to alter evaluation signatures. The error message is signalling that the statement is not valid <code>wrapr</code> grammar (not well formed in terms of <code>wrapr</code> rules). Notice the error message suggests the alternate notation <code>sin(.)</code>. Similar rules apply for <code>base::sin()</code>. Then intent is that outer parenthesis are non-semantic, they do not change change <code>wrapr</code> pipe behavior.</li>
<li><code>5 %.>% { sin }</code> returns just the <code>sin</code> function. This is because <code>{}</code> triggers <code>wrapr</code>’s “leave the contents alone” behavior.</li>
</ul>
<p>The user only encounters two exceptions in the above variations. The first is “don’t write <code>sin()</code>”, which comes with a clear error message and help (“try <code>sin(.)</code>”). The second is “outer <code>{}</code> treats its contents as raw statements, turning off transforms and checking.<br>
<code>wrapr</code> is hoping to stay close the principle of least surprise.<br>
The hope is that <code>wrapr</code> piping is easy, powerful, useful, and not <em>too</em> different than <code>a %.>% b</code> being treated as almost syntactic sugar for <code>{ . &lt;- a; b }</code>.</p>
<div>
<h4>Aesthetics</h4>
<p>An obvious down-side of <code>wrapr</code> piping is the excess dots both in the operator and in the evaluation arguments. We <em>strongly</em> feel the extra dots in the evaluation arguments is actually <a rel="nofollow" href="http://www.win-vector.com/blog/2018/03/r-tip-make-arguments-explicit-in-magrittr-dplyr-pipelines/" target="_blank">a good trade in losing some conciseness in exchange for useful explicitness</a>. We do not consider the extra dot in the pipe operator to be a problem (especially if you <a rel="nofollow" href="http://www.win-vector.com/blog/2017/11/rstudio-keyboard-shortcuts-for-pipes/" target="_blank">bind the operator to a keyboard shortcut</a>). If the extra dot in the pipe operator is such a deal-breaker, consider that it could be gotten rid of by copying the pipe operator to your notation of choice (such as executing <code>`%>%` &lt;- wrapr::`%.>%`</code> or <code>`%.%` &lt;- wrapr::`%.>%`</code> at the top of your work). However such re-mappings are needlessly confusing and it is best to use the operator glyph that <code>wrapr</code> directly supplies.
</div>
</div>
</div>
<div>
<h2>Non-function examples</h2>
<p>We can also try a few simpler expressions, that do not have an explicit function marker such as <code>sin(.)</code>.</p>
<pre><code>c(&quot;5 PIPE_GLYPH 1 + .&quot;,
  &quot;5 PIPE_GLYPH (1 + .)&quot;,
  &quot;5 PIPE_GLYPH {1 + .}&quot;) %.>%
  work_examples(., 6) %.>%
  knitr::kable(., format = &quot;html&quot;, escape = FALSE) %.>%
  column_spec(., 1:4, width = &quot;1.75in&quot;) %.>%
  kable_styling(., &quot;striped&quot;, full_width = FALSE)</code></pre>
<table>
<thead>
<tr>
<th>
magrittr expr
</th>
<th>
magrittr res
</th>
<th>
wrapr expr
</th>
<th>
wrapr res
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
5 %>% 1 + .
</td>
<td>
attempt to apply non-function
</td>
<td>
5 %.>% 1 + .
</td>
<td>
wrapr::pipe_step.default does not allow direct piping into simple values such as class:numeric, type:double.
</td>
</tr>
<tr>
<td>
5 %>% (1 + .)
</td>
<td>
non-numeric argument to binary operator
</td>
<td>
5 %.>% (1 + .)
</td>
<td>
<span>6</span>
</td>
</tr>
<tr>
<td>
5 %>% {1 + .}
</td>
<td>
<span>6</span>
</td>
<td>
5 %.>% {1 + .}
</td>
<td>
<span>6</span>
</td>
</tr>
</tbody>
</table>
<p>Some of what caused exceptions above is “<code>5 %ANYTHING% 1 + .</code>” is parsed (due to <code>R</code>’s operator precedence rules) as “<code>(5 %ANYTHING% 1) + .</code>”. So without extra grouping notations (“()” or “{}”) this is not a well-formed pipeline. With <code>wrapr</code> it is safe to add in parenthesis, with <code>magrittr</code> one must use <code>{}</code> (though this can not be used with <code>5 %>% {sin}</code>).
</div>
<div>
<h2>The Importance of Strictness</h2>
<p>For some operations that are unlikely to work close to reasonable user intent <code>wrapr</code> includes checks to warn-off the user. The following shows a few more examples of this “defense of grammar.”</p>
<pre><code>5 %.>% 7</code></pre>
<pre><code>## Error in pipe_step.default(pipe_left_arg, pipe_right_arg, pipe_environment, : wrapr::pipe_step.default does not allow direct piping into simple values such as class:numeric,  type:double.</code></pre>
<pre><code># magrittr's error message for the above is something of the form:
# &quot;Error in function_list[[k]](value) : attempt to apply non-function&quot;

5 %.>% .</code></pre>
<pre><code>## Error in pipe_step.default(pipe_left_arg, pipe_right_arg, pipe_environment = pipe_environment, : wrapr::pipe_step.default does not allow direct piping into simple values such as class:numeric,  type:double.</code></pre>
<pre><code># note: the above error message is improved to:
# &quot;wrapr::pipe does not allow direct piping into '.'&quot;
# in wrapr 1.4.1

5 %.>% return(.)</code></pre>
<pre><code>## Error in pipe_step.default(pipe_left_arg, pipe_right_arg, pipe_environment, : wrapr::pipe_step.default does not allow direct piping into certain reserved words or control structures (such as &quot;return&quot;).</code></pre>
<p>Throwing errors in these situations is based on the principle that non-signalling errors (often leading to result corruption) are much worse than signalling errors. The “<code>return</code>” example is an interesting case in point.<br>
Let’s first take a look at the effect with <code>magrittr</code>. Suppose we were writing a simple function to find for a positive integer returns the smallest non-trivial (greater than <code>1</code> <em>and</em> less than the value in question) positive integer divisor of the value in question (returning <code>NA</code> if there is none such). Such a function might work like the following.</p>
<pre><code>f_base &lt;- function(x) {
  u &lt;- min(ceiling(sqrt(x)), x-1L)
  i &lt;- 2L
  while(i&lt;=u) {
    if((x %% i)==0) {
      return(i)
    }
    i &lt;- i + 1L
  }
  NA_integer_
}

f_base(37)</code></pre>
<pre><code>## [1] NA</code></pre>
<pre><code>f_base(35)</code></pre>
<pre><code>## [1] 5</code></pre>
<p>Now suppose we try to get fancy and use “<code>i %>% return</code>” instead of “<code>return(i)</code>”. This produces a function that thinks all integer are prime. The reason is: <code>magrittr</code> can call the <code>return()</code> function, but in this situation <code>return()</code> can’t manage the control path of the original function.</p>
<pre><code>f_magrittr &lt;- function(x) {
  u &lt;- min(ceiling(sqrt(x)), x-1L)
  i &lt;- 2L
  while(i&lt;=u) {
    if((x %% i)==0) {
      i %>% return
    }
    i &lt;- i + 1L
  }
  NA_integer_
}

f_magrittr(37)</code></pre>
<pre><code>## [1] NA</code></pre>
<pre><code>f_magrittr(35)</code></pre>
<pre><code>## [1] NA</code></pre>
<p>Now suppose we tried the same thing with <code>wrapr</code> pipe and write <code>i %.>% return(.)</code>.</p>
<pre><code>f_wrapr &lt;- function(x) {
  u &lt;- min(ceiling(sqrt(x)), x-1L)
  i &lt;- 2L
  while(i&lt;=u) {
    if((x %% i)==0) {
      i %.>% return(.)
    }
    i &lt;- i + 1L
  }
  NA_integer_
}

f_wrapr(37)</code></pre>
<pre><code>## [1] NA</code></pre>
<pre><code>f_wrapr(35)</code></pre>
<pre><code>## Error in pipe_step.default(pipe_left_arg, pipe_right_arg, pipe_environment, : wrapr::pipe_step.default does not allow direct piping into certain reserved words or control structures (such as &quot;return&quot;).</code></pre>
<p><code>wrapr</code> also can not handle <code>return()</code> control flow correctly, however it (helpfully) throws an exception to indicate the problem.
</div>
<div>
<h2>Conclusion</h2>
<p><code>R</code> usually has more than one good way to perform tasks. In this case we talked about two methods of building pipelines in <code>R</code>: <code>magrittr</code> and <code>wrapr</code>. There are more methods (some of which are listed <a rel="nofollow" href="https://github.com/WinVector/wrapr/blob/master/extras/wrapr_pipe.pdf" target="_blank">here</a>). Our preferred pipe is the <code>wrapr</code> dot-pipe, and in the of style academic priority we try to credit alternatives and share fair comparisons (as we have done here). Priority is important to respect (as in: <code>magrittr</code> is powerful, popular, came well before, and greatly influences <code>wrapr</code> dot-pipe), but it is not monopoly rights (for example: the <a rel="nofollow" href="http://www.win-vector.com/blog/2016/12/using-replyrlet-to-parameterize-dplyr-expressions/#comment-66361" target="_blank">public CRAN release/announcement</a> of <a rel="nofollow" href="https://cran.r-project.org/web/packages/wrapr/vignettes/let.html" target="_blank"><code>let()</code></a>, our popular and still preferred substitution methodology and originally part of <code>replyr</code>, predates the public <a rel="nofollow" href="https://cran.r-project.org/src/contrib/Archive/rlang/" target="_blank">CRAN release</a>/<a rel="nofollow" href="https://blog.rstudio.com/2017/04/13/dplyr-0-6-0-coming-soon/" target="_blank">announcement</a> of <code>rlang</code>/<code>tidyeval</code> code re-writing methods). In client work we use whatever style is most compatible with the client’s work and needs, for example we feel it does not make sense to take a legacy <code>dplyr</code> project and attempt to switch the pipe notation late in the game (and one does not want to needlessly mix notations).
</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/2018/04/magrittr-and-wrapr-pipes-in-r-an-examination/"> 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/22scx12pKYg" height="1"></a></div>