<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/magrittrs-doppelganger/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/12/magrittrs-doppelganger/"> 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 target="_blank" rel="nofollow" href="https://cran.r-project.org/">R</a> picked up a nifty way to organize sequential calculations in May of 2014: <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=magrittr"><code>magrittr</code></a> by Stefan Milton Bache and Hadley Wickham. <code>magrittr</code> is now quite popular and also has become the backbone of current <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=dplyr"><code>dplyr</code></a> practice. </p>
<p>If you read <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2016/12/the-case-for-using-in-r/">my last article on assignment</a> carefully you may have noticed I wrote some code that was equivalent to a <code>magrittr</code> pipleline without using the “<code>%>%</code>” operator. This note will expand (tongue in cheek) that notation into an alternative to <code>magrittr</code> that you should never use.</p>
<p><center><br>
<img title="SupermanBizarro.jpg" alt="SupermanBizarro" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/12/SupermanBizarro.jpg?resize=317%2C467" border="0" data-recalc-dims="1"></center></p>
<p>What follows is a joke (though everything does work as I state it does, nothing is faked).<span></span></p>
<h2><code>magrittr</code></h2>
<blockquote><p>[<code>magrittr</code>] Provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. There is flexible support for the type of right-hand side expressions. For more information, see package vignette. To quote Rene Magritte, “Ceci n’est pas un pipe.”</p>
<p><a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=magrittr">(from the package description)</a>
</blockquote>
<p>Once you read up on <code>magrittr</code> and try some examples you tend to be sold. <code>magrittr</code> is a graceful notation for chaining multiple calculations and managing intermediate results. For our example consider in <code>R</code> the following chain of function applications:</p>
<p><code>
<pre>
sqrt(tan(cos(sin(7))))

 # [1] 1.006459

library(&quot;magrittr&quot;)
7 %>% sin() %>% cos() %>% tan() %>% sqrt()

 # [1] 1.006459
</pre>
<p></p>
<p>Both are artificial examples, but the <code>magrittr</code> notation is much easier to read. The pipe notation removes some of the pain of chaining so many functions and is a good realization of the mathematical <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Function_composition">function composition</a> operator traditionally written as “<code>(g ⚬ f)(x) = g(f(x))</code>“. The replacing of nesting with composition allows us to read left to right instead of right to left.</p>
<h2>Bizarro <code>magrittr</code></h2>
<p><code>magrittr</code> itself is largely what is called “<a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Syntactic_sugar">syntactic sugar</a>” (though if you look at the code, say by “<code>print(magrittr::`%>%`)</code>” you will see <code>magrittr</code> commands some fairly heroic control of the evaluation order to achieve its effect). If we didn’t care about syntax we could write processing pipelines without <code>magrittr::`%>%`</code> as follows.<br>
<code>
<pre>
# &quot;Piping&quot; without magrittr.

7 ->.; sin(.) ->.; cos(.) ->.; tan(.) ->.; sqrt(.)

 # [1] 1.006459
</pre>
<p></p>
<p>The above is essentially the same pipeline (modulo some issues regarding printing, and the visibility and lifetime of “<code>.</code>“). We could even write it with the industry preferred left arrow by using “<code>;.&lt;-</code>” throughout (though we would need to use “<code>->.;.&lt;-</code>” to start such a pipleline). What I am saying if we thought of “<code>->.;</code>” as an atomic (indivisible plus non-mixable) glyph (as we are already encouraged to think of “<code>&lt;-</code>” as) then that glyph is pretty much a piping operator. In a perverse sense “<code>->.;</code>” is a poor man’s “<code>%>%</code>“. Oddly enough we can think of the semicolon as doing the heavy lifting as it is a statement sequencer (and <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Monad_(functional_programming)">functional programming monads</a> can be thought of as “programmable semicolons”).</p>
<h2>Things Get Worse</h2>
<p>“<code>->.;</code>” may be slightly faster than “<code>%>%</code>“. It makes sense, as the semicolon-hack is doing a lot less for us than a true <code>magrittr</code> pipe. This difference (which is not important) is only going to show up when when we have a tiny amount of data, where the expression control remains a significant portion of the processing time (which it never is in practice!). <code>magrittr</code> is in fact fast, it is just that doing nothing is a tiny bit faster.</p>
<p>Everything below is a correct calculation, it is just a deliberate example of going too far measuring something that does not matter. The sensible conclusion is: use <code>magrittr</code>, despite the following sillyness.</p>
<p><code>
<pre>
library(&quot;microbenchmark&quot;)
library(&quot;magrittr&quot;)
library(&quot;ggplot2&quot;)
set.seed(234634)


fmagrittr &lt;- function(d) {
  7 %>% sin() %>% cos() %>% tan() %>% sqrt()
}

fmagrittrdot &lt;- function(d) {
  7 %>% sin(.) %>% cos(.) %>% tan(.) %>% sqrt(.)
}

fsemicolon &lt;- function(d) {
  d ->.; sin(.) ->.; cos(.) ->.; tan(.) ->.; sqrt(.)
}

bm &lt;- microbenchmark(
  fmagrittr(7),
  fmagrittrdot(7),
  fsemicolon(7),
  control=list(warmup=100L,order='random'),
  times=10000L
)

print(bm)

 # Unit: nanoseconds
 #             expr    min       lq       mean   median       uq      max neval
 #     fmagrittr(7) 133447 140496.5 161386.520 146348.5 151024.0 35967729 10000
 #  fmagrittrdot(7) 123223 130531.0 146873.208 135918.5 140309.5  2759953 10000
 #    fsemicolon(7)    910   1347.5   1585.334   1535.0   1711.0    41647 10000

t.test(bm$time[bm$expr!='fsemicolon(7)'],
       bm$time[bm$expr=='fsemicolon(7)'])

 # 	Welch Two Sample t-test
 # 
 # data:  bm$time[bm$expr != &quot;fsemicolon(7)&quot;] and bm$time[bm$expr == &quot;fsemicolon(7)&quot;]
 # t = 79.106, df = 19999, p-value &lt; 2.2e-16
 # alternative hypothesis: true difference in means is not equal to 0
 # 95 percent confidence interval:
 #  148764.8 156324.3
 # sample estimates:
 #  mean of x  mean of y 
 # 154129.864   1585.334 

highcut &lt;- quantile(bm$time,probs=0.95)
table(bm$expr[bm$time>=highcut])

 #    fmagrittr(7) fmagrittrdot(7)   fsemicolon(7) 
 #            1007             493               0 

ggplot(data=as.data.frame(bm),aes(x=time,color=expr)) +
  geom_density(adjust=0.3) +
  facet_wrap(~expr,ncol=1,scales = 'free_y') +
  scale_x_continuous(limits = c(min(bm$time),highcut))
</pre>
<p></p>
<p><img title="Rplot02.png" alt="Rplot02" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/12/Rplot02-1.png?w=660" border="0" data-recalc-dims="1"></p>
<h2>Conclusion</h2>
<p>I am most emphatically <em>not</em> suggesting use of “<code>->.;</code>” as a poor man’s “<code>%>%</code>“! But there is a relation, both “<code>%>%</code>” and semicolon are about sequencing statements.</p>
<p>Again, everything above was a joke (though nothing was fake, everything does run as I claimed it did).</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/2016/12/magrittrs-doppelganger/"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/YtdNLLdkYDw"></a></div></code></code></code>