<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/r-tip-make-arguments-explicit-in-magrittr-dplyr-pipelines/"></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/03/r-tip-make-arguments-explicit-in-magrittr-dplyr-pipelines/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I think this is the <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">R Tip</a> that is going to be the most controversial yet. Its potential pitfalls include: it is a style prescription (which makes it different than and less immediately useful than something of the nature of <a rel="nofollow" href="http://www.win-vector.com/blog/2018/02/r-tip-force-named-arguments/" target="_blank">R Tip: Force Named Arguments</a>), and it is heterodox (this is not how <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> is taught by the original authors, and not how it is commonly used). However, I have not been at all good at anticipating which tips get which sort of reception (and this valuable feedback, public and private, is part of what I get of this series).</p>
<p>On to the tip (which only applies if you are a <code>magrittr</code> pipeline user).</p>
<p><a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">tip</a>: when using <code>magrittr</code> pipelines consider making them more explicit, and more readable (especially to novices) by using explicit dot-arguments throughout.</p>
<p><span></span></p>
<p>The advice is: write pipelines that look like the this:</p>
<pre>
suppressPackageStartupMessages(library(&quot;dplyr&quot;))

starwars %>%
  filter(., height > 200) %>%
  select(., height, mass) %>%
  head(.)
</pre>
<p>And avoid overly concise pipelines such as the this:</p>
<pre>
starwars %>%
  filter(height > 200) %>%
  select(height, mass) %>%
  head
</pre>
<p>The guidance is: each step in a <em>simple</em> <code>magrittr</code> pipeline is a function call that has at least one of its arguments directly written as “<code>.</code>“. Example: “<code>atan2(3, .)</code>” is a simple step, but neither “<code>atan</code>” nor “<code>atan2(abs(.), 5)</code>” is a simple step.</p>
<p>The intended point is: the first pipeline is more explicit and regular. This makes it easier to explain and easier for newcomers to read. For pipelines <em>limited to this style</em>: approximately each step is run in sequence as if the value of the last step were in a variable named “<code>.</code>“.</p>
<p>Note: the exact <code>magrittr</code> semantics are in fact more detailed that what I just said. The idea is to start newcomers in a sub-dialect of <code>magrittr</code> that has a simpler correct mental model before (or if ever) moving to <a rel="nofollow" href="https://www.rdocumentation.org/packages/magrittr/versions/1.5?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">the full details</a>. The full details are perhaps more than a <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> should be expected to remember. It is a bit much to expect a non-cognoscenti always remember that “<code>5 %>% atan2(3, .)</code>” is completely different than “<code>5 %>% atan2(3, abs(.))</code>“, and that “<code>5 %>% {. + 1}</code>” is completely different than “<code>5 %>% (. + 1)</code>“.</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/03/r-tip-make-arguments-explicit-in-magrittr-dplyr-pipelines/"> 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/HgGmXR_zHFs" height="1"></a></div>