<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/the-case-for-using-in-r/"></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/the-case-for-using-in-r/"> 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/"><code>R</code></a> has a number of assignment operators (at least “<code>&lt;-</code>“, “<code>=</code>“, and “<code>-></code>“; plus “<code>&lt;&lt;-</code>” and “<code>->></code>” which have different semantics).</p>
<p>The <code>R</code>-style guides routinely insist on “<code>&lt;-</code>” as being the only preferred form. In this note we are going to <em>try</em> to make the case for “<code>-></code>” when using <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=magrittr">magrittr</a> pipelines.</p>
<p><a target="_blank" rel="nofollow" href="https://commons.wikimedia.org/wiki/File:Honor%C3%A9_Daumier_017_(Don_Quixote).jpg">
<p><center><br>
<img title="Honore_Daumier_017_Don_Quixote.jpg" alt="Honore Daumier 017 Don Quixote" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/12/Honore_Daumier_017_Don_Quixote.jpg?fit=660%2C300" border="0" data-recalc-dims="1">
<p>
Don Quijote and Sancho Panza, by Honoré Daumier<br>
<br>
</center><span></span></p>
<p>
<h2>Assignment in <code>R</code></h2>
<p><code>R</code>‘s preferred assignment operator is “<code>&lt;-</code>“. This is in the popular style guides. If you write using this style you can organize your code so that:</p>
<ul>
<li>“<code>&lt;-</code>” always means assignment</li>
<li>“<code>=</code>” always means function argument binding</li>
<li>“<code>==</code>” always means comparison.</li>
</ul>
<p>This has some advantages, and is the public style. Also “<code>=</code>” is much harder to use inside R’s <code>base::quote</code> method than “<code>&lt;-</code>“, so there are still cases where the semantics of “<code>=</code>” and “<code>&lt;-</code>” are different (though I think they all involve the distinction trying specify argument binding versus assignment while inside a function call’s argument list).</p>
<p>I have previously written that <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2013/04/prefer-for-assignment-in-r/">given the choice I prefer “<code>=</code>” for assignment</a>. It has the advantages that:</p>
<ul>
<li>“<code>&lt;-</code>” is has a different meaning to many readers. In <code>R</code> “<code>x&lt;-3</code>” assigns the value <code>3</code> to a variable named <code>x</code>, in other popular programming languages (where new <code>R</code> users may be coming from) “<code>x&lt;-3</code>” denotes comparing <code>x</code> to <code>-3</code>.</li>
<li>“<code>=</code>” is a single character, so it can not be ruined by the insertion of a space. “<code>x&lt; -3</code>” does not assign the value <code>3</code> to a variable named <code>x</code>, it compares <code>x</code> to <code>-3</code>. I would not mind so much if “<code>x&lt; -3</code>” was a syntax error (as “<code>x&lt; =3</code>” is), but it is valid code that quietly does something very different than “<code>x&lt;-3</code>“. If you have taught <code>R</code> enough you have experience helping students undo this bug.</li>
<li>“<code>=</code>” is on the keyboard (as “←” was when <a target="_blank" rel="nofollow" href="http://developer.r-project.org/equalAssign.html">arrow like assignments were themselves introduced</a>).
<li>“<code>=</code>” is easier to paste into HTML as it does not require escape coding such as “<code>&amp;lt;</code>“.</li>
<li>It is the symbol used in most every other popular current programming language for assignment.</li>
<li>There is an asymmetric cost of mistakes. Typing “<code>=</code>” when you meant “<code>&lt;-</code>” is usually harmless. Typing “<code>&lt;-</code>” in a context where “<code>=</code>” was needed is not caught by <code>R</code> and fairly bad (please see <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2013/04/prefer-for-assignment-in-r/">here</a> for details). So if you get out of the habit of using “<code>&lt;-</code>” one type of bug become less likely.</li>
<li>There is a cognitive benefit in reducing the number of low-value distinctions you need to maintain, especially for beginners. If we think of the mind as having <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two">“seven plus or minus two” slots for current information</a> do we really want to waste 11 to 20 percent of our students’ attention on something like this when teaching? The beginner does not need to worry over the differences between value assignment and argument binding at all times. In fact it is a useful generalization to think of argument binding as a safe transient value assignment.</li>
</ul>
<p>Now I said “given the choice” which means to work with others you have to use “<code>&lt;-</code>” or at least admit that you are being stubborn. I teach “<code>&lt;-</code> for assignment” as I do not wish to set up students for ridicule (and they being less informed on the history or <code>R</code> are less equipped to defend theirselves on this issue).</p>
<p>That being said I still don’t actually like “<code>&lt;-</code>“. And in fact I am not sure why the <code>R</code> community has so fetishized its use. “<code>&lt;-</code>” comes form an era when it was actually a symbol on the keyboard and two other <code>S</code> assignment operators from that era (“<code>_</code>” and “<code>:=</code>“) have have not survived in the <code>R</code> language (please see <a target="_blank" rel="nofollow" href="http://developer.r-project.org/equalAssign.html">here</a>). I think the style is largely enforced as a kind of argot or “inside language” to express loyalty to <code>R</code>.</p>
<h2>A deliberately provocative proposal</h2>
<p>That being said I have really come to like using <code>R</code>‘s “<code>-></code>” operator. I know I can’t always get away with it but consider the advantage using “<code>-></code>” brings to western readers (meaning users of Greek derived alphabets): you can then simply read code from left to right. If I am not allowed to use “<code>=</code>” I want something back in exchange, and “<code>-></code>” actually has some interesting advantages. Let us set up a proposal that is admittedly incompatible with my previous argument.</p>
<p>Consider the following statement:</p>
<p><code>
<pre>
  x = 3 + 4
</pre>
<p></p>
<p>This is read in R, and most common programming languages, as “assign the value of 3 + 4 to the variable x.” We know to read it this way because “assignment has lower <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Order_of_operations">operator precedence</a> than plus.” Roughly this means there implicit parenthesization rules that mean “<code>x=3+4</code>” is actually shorthand for “<code>x=(3+4)</code>” (roughly because in <code>R</code> explicit use of parentheses also controls the auto-printing behavior of values). But consider the same statement written with “<code>-></code>“:</p>
<p><code>
<pre>
  3 + 4 -> x
</pre>
<p></p>
<p>The semantics still come from operator precedence rules, but now the syntax is emphasizing the same thing: the calculation happens before (to the left of) the assignment. This may not seem like much to experienced programmers- but that is because so many programming languages use the frankly unnatural “<code>x=3+4</code>” notation (so we are used to it).</p>
<p>A substantial advantage comes when using <code>magrittr</code> pipes in R.</p>
<p>Suppose I write the following <code>magrittr</code> pipeline:</p>
<p><code>
<pre>
# Count number of NA in columns x,y, 
# and z using pure dplyr notation
# or back-end agnostic dplyr code.  
# This involves avoiding use of $
# or things like multiple intermediate 
# values in dplyr::summarize.
# This is a useful example as 
# complete.cases isn't available on
# all dplyr data services.
# ifelse() is to ensure type 
# conversions on remote SQL.

library(&quot;dplyr&quot;)
my_db &lt;- dplyr::src_sqlite(&quot;:memory:&quot;, create = TRUE)

data.frame(
           x = c(1, 2, 2),
           y = c(3, 5, NA),
           z = c(NA, 'a', 'b'),
           rowNum = 1:3,
           stringsAsFactors = FALSE
          ) %>%
  copy_to(my_db, ., 'd') %>%
  mutate(nna = ifelse(is.na(x),1,0) +
               ifelse(is.na(y),1,0) + 
               ifelse(is.na(z),1,0)) %>%
  arrange(rowNum) -> dres
</pre>
<p></p>
<p>In this notation we see that now “<code>-></code>” is itself a pipe compatible operator that moves values to variables. The pipeline itself is already moving left to right top to down. Placing the assignment first would give us an ugly two directional flow.</p>
<p>Non semantic changes in the pipeline are now syntactically cheap and localized (as they <em>should</em> be). For example: want to land intermediate results for reasons of efficiency or necessary side-effects? Solution: insert “<code>-> varName LINEBREAK varName %>%</code>” at will, as you already do with <code>dplyr::collapse()</code> and <code>dplyr::compute()</code>.

<p>The syntax is now working for us instead of against us. I feel once you start using <code>magrittr</code> pipelines (which are written left to right, as we did here) the next logical step is use “<code>-></code>” for consistency.</p>
<h3>Syntax Matters</h3>
<p>The following code has essentially the same semantics as the previous <code>magrittr</code> pipes, without needing a piping operator.</p>
<p><code>
<pre>
data.frame(
           x = c(1, 2, 2),
           y = c(3, 5, NA),
           z = c(NA, 'a', 'b'),
           rowNum = 1:3,
           stringsAsFactors = FALSE
          ) -> .
  copy_to(my_db, ., 'd2') -> .
  mutate(., nna = ifelse(is.na(x),1,0) +
                  ifelse(is.na(y),1,0) + 
                  ifelse(is.na(z),1,0)) -> .
  arrange(., rowNum) -> dres
</pre>
<p></p>
<p>The above code has the advantage that it is easier to debug in that you can stop at any stage and the intermediate results are convenient to inspect. However, there was no great call for code in this style (or the matching beginning of line “<code>. &lt;-</code>” version) prior to the introduction of <code>magrittr</code>. It just isn’t as enjoyable to use a mere coding convention as it is to use <code>magrittr</code> pipe syntax.</p>
<h2>Conclusion</h2>
<ul>
<li>I honestly think in a <code>magrittr</code> world “<code>-></code>” is a natural assignment operator and could make teaching <code>R</code> easier. It reads more fluidly once you get used to it and come to expect assignment to be written late (i.e. once you know where to look).</li>
<li>I can not currently recommend actually using “<code>-></code>” in other people’s projects as it is not currently allowed under the most popular <code>R</code> style guides. Both: <a target="_blank" rel="nofollow" href="http://adv-r.had.co.nz/Style.html"><em>Advanced R</em> by Hadley Wickham</a> and <a target="_blank" rel="nofollow" href="https://google.github.io/styleguide/Rguide.xml#assignment">Google’s R Style Guide</a> say: “Use <code>&lt;-</code>, not <code>=</code>, for assignment.“</li>
<li>I would like to propose that “<code>-></code>” be considered an allowed assignment operator with the stricture code should not reverse directions too often (as that is, in fact, confusing). If you control one of the named style guides, please do consider my suggestion.</li>
</ul>

<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/the-case-for-using-in-r/"> 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/pW1udwDvTug"></a></div></code></code></code></code></a>