<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/pipes-in-r-tutorial-for-beginners/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>You might have already seen or used the pipe operator when you're working with packages such as <code>dplyr</code>, <code>magrittr</code>,… But do you know where pipes and the famous <code>%>%</code> operator come from, what they exactly are, or how, when and why you should use them? Can you also come up with some alternatives? </p>
<p>This tutorial will give you an introduction to pipes in R and will cover the following topics:</p>
<nav>
<ul>
<li><a rel="nofollow" href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/#intro" target="_blank">Pipe Operator in R: Introduction</a>
<ul>
<li>Short History of the Pipe Operator in R</li>
<li>What Is It?</li>
<li>Why Use It?</li>
<li>Additional Pipes</li>
</ul>
</li>
<li><a rel="nofollow" href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/#howto" target="_blank">How To Use Pipes in R</a>
<ul>
<li>Basic Piping </li>
<li>Argument Placeholder</li>
<li>Re-using Placeholder for Attributes</li>
<li>Building Unary Functions</li>
<li>Compound Assignment Pipe Operations</li>
<li>Tee Operations with the Tee Operator</li>
<li>Exposing Data Variables with the Exposition Operator </li>
</ul>
</li>
<li><a rel="nofollow" href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/#dplyr" target="_blank"><code>dplyr</code> and <code>magrittr</code></a></li>
<li><a rel="nofollow" href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/#shortcuts" target="_blank">RStudio Keyboard Shortcuts</a></li>
<li><a rel="nofollow" href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/#notuse" target="_blank">When Not To Use the Pipe Operator in R</a></li>
<li><a rel="nofollow" href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/#alternatives" target="_blank">Alternatives to Pipes in R</a><br>

<p>


<p>Are you interested in learning more about manipulating data in R with <code>dplyr</code>? Take a look at DataCamp's <a rel="nofollow" href="https://www.datacamp.com/courses/dplyr-data-manipulation-r-tutorial?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">Data Manipulation in R with <code>dplyr</code></a> course. </p>
<p><a></a></p>
<h2>Pipe Operator in R: Introduction</h2>
<p>To understand what the pipe operator in R is and what you can do with it, it's necessary to consider the full picture, to learn the history behind it. Questions such as &quot;where does this weird combination of symbols come from and why was it made like this?&quot; might be on top of your mind. You'll discover the answers to these and more questions in this section. </p>
<p>Now, you can look at the history from three perspectives: from a mathematical point of view, from a holistic point of view of programming languages, and from the point of view of the R language itself. You'll cover all three in what follows! </p>
<h3>History of the Pipe Operator in R</h3>
<h4>Mathematical History</h4>
<p>If you have two functions, let's say $f : B → C$ and $g : A → B$, you can chain these functions together by taking the output of one function and inserting it into the next. In short, &quot;chaining&quot; means that you pass an intermediate result onto the next function, but you'll see more about that later. </p>
<p>For example, you can say, $f(g(x))$: $g(x)$ serves as an input for $f()$, while $x$, of course, serves as input to $g()$. </p>
<p>If you would want to note this down, you will use the notation $f ◦ g$, which reads as &quot;f follows g&quot;. Alternatively, you can visually represent this as:</p>
<p><img alt="" src="https://i0.wp.com/res.cloudinary.com/dyd911kmh/image/upload/f_auto,q_auto:best/v1510846626/Pipe-Mathematical_gczmab.png?w=660" data-recalc-dims="1"></p>
<p></p>
<caption>Image Credit: James Balamuta, <a rel="nofollow" href="http://stat385.thecoatlessprofessor.com/assets/lectures/07-PipingData/lec07-piping-data.pdf" target="_blank">&quot;Piping Data&quot;</a></caption>
<h4>Pipe Operators in Other Programming Languages</h4>
<p>As mentioned in the introduction to this section, this operator is not new in programming: in the Shell or Terminal, you can pass command from one to the next with the pipeline character <code>|</code>. Similarly, F# has a forward pipe operator, which will prove to be important later on! Lastly, it's also good to know that Haskell contains many piping operations that are derived from the Shell or Terminal. </p>
<h4>Pipes in R</h4>
<p>Now that you have seen some history of the pipe operator in other programming languages, it's time to focus on R. The history of this operator in R starts, according to <a rel="nofollow" href="http://adolfoalvarez.cl/plumbers-chains-and-famous-painters-the-history-of-the-pipe-operator-in-r/" target="_blank">this fantastic blog post</a> written by Adolfo Álvarez, on January 17th, 2012, when an anonymous user asked the following question in <a rel="nofollow" href="https://stackoverflow.com/questions/8896820/how-to-implement-fs-forward-pipe-operator-in-r" target="_blank">this Stack Overflow post</a>:</p>
<blockquote><p>
How can you implement F#'s forward pipe operator in R? The operator makes it possible to easily chain a sequence of calculations. For example, when you have an input data and want to call functions <code>foo</code> and <code>bar</code> in sequence, you can write <code>data |> foo |> bar</code>?
</p></blockquote>
<p>The answer came from Ben Bolker, professor at McMaster University, who replied:</p>
<blockquote><p>
I don't know how well it would hold up to any real use, but this seems (?) to do what you want, at least for single-argument functions …
</p></blockquote>
<pre>&quot;%>%&quot; &lt;- function(x,f) do.call(f,list(x))
pi %>% sin
[1] 1.224606e-16
pi %>% sin %>% cos
[1] 1
cos(sin(pi))
[1] 1
</pre>
<p>About nine months later, <a rel="nofollow" href="http://hadley.nz/" target="_blank">Hadley Wickham</a> started the <code>dplyr</code> package on GitHub. You might now know Hadley, Chief Scientist at RStudio, as the author of many popular R packages (such as this last package!) and as the instructor for DataCamp's <a rel="nofollow" href="https://www.datacamp.com/courses/writing-functions-in-r?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">Writing Functions in R</a> course. </p>
<p>Be however it may, it wasn't until 2013 that the first pipe <code>%.%</code> appears in this package. As Adolfo Álvarez rightfully mentions in his blog post, the function was denominated <code>chain()</code>, which had the purpose to simplify the notation for the application of several functions to a single data frame in R. </p>
<p>The <code>%.%</code> pipe would not be around for long, as Stefan Bache proposed an alternative on the 29th of December 2013, that included the operator as you might now know it:</p>
<pre>iris %>%
  subset(Sepal.Length > 5) %>%
  aggregate(. ~ Species, ., mean)
</pre>
<p>Bache continued to work with this pipe operation and at the end of 2013, the <code>magrittr</code> package came to being. In the meantime, Hadley Wickham continued to work on <code>dplyr</code> and in April 2014, the <code>%.%</code> operator got replaced with the one that you now know, <code>%>%</code>. </p>
<p>Later that year, Kun Ren published the <code>pipeR</code> package on GitHub, which incorporated a different pipe operator, <code>%>>%</code>, which was designed to add more flexibility to the piping process. However, it's safe to say that the <code>%>%</code> is now established in the R language, especially with the recent popularity of the Tidyverse. </p>
<h3>What Is It?</h3>
<p>Knowing the history is one thing, but that still doesn't give you an idea of what F#'s forward pipe operator is nor what it actually does in R. </p>
<p>In F#, the pipe-forward operator <code>|></code> is syntactic sugar for chained method calls. Or, stated more simply, it lets you pass an intermediate result onto the next function. </p>
<p><strong>Remember</strong> that &quot;chaining&quot; means that you invoke multiple method calls. As each method returns an object, you can actually allow the calls to be chained together in a single statement, without needing variables to store the intermediate results. </p>
<p>In R, the pipe operator is, as you have already seen, <code>%>%</code>. If you're not familiar with F#, you can think of this operator as being similar to the <code>+</code> in a <code>ggplot2</code> statement. Its function is very similar to that one that you have seen of the F# operator: it takes the output of one statement and makes it the input of the next statement. When describing it, you can think of it as a &quot;THEN&quot;.</p>
<p>Take, for example, following code chunk and read it aloud:</p>
<pre>class=&quot;lang-{r}&quot;>iris %>%
  subset(Sepal.Length > 5) %>%
  aggregate(. ~ Species, ., mean)
</pre>
<p>You're right, the code chunk above will translate to something like &quot;you take the Iris data, then you subset the data and then you aggregate the data&quot;. </p>
<p>This is one of the most powerful things about the Tidyverse. In fact, having a standardized chain of processing actions is called &quot;a pipeline&quot;. Making pipelines for a data format is great, because you can apply that pipeline to incoming data that has the same formatting and have it output in a <code>ggplot2</code> friendly format, for example.</p>
<h3>Why Use It?</h3>
<p>R is a functional language, which means that your code often contains a lot of parenthesis, <code>(</code> and <code>)</code>. When you have complex code, this often will mean that you will have to nest those parentheses together. This makes your R code hard to read and understand. Here's where <code>%>%</code> comes in to the rescue!</p>
<p>Take a look at the following example, which is a typical example of nested code:</p>
<pre>class=&quot;lang-R&quot;># Initialize `x`
x &lt;- c(0.109, 0.359, 0.63, 0.996, 0.515, 0.142, 0.017, 0.829, 0.907)

# Compute the logarithm of `x`, return suitably lagged and iterated differences, 
# compute the exponential function and round the result
round(exp(diff(log(x))), 1)
</pre>
<ol>
<li>3.3</li>
<li>1.8</li>
<li>1.6</li>
<li>0.5</li>
<li>0.3</li>
<li>0.1</li>
<li>48.8</li>
<li>1.1</li>
</ol>
<p>With the help of <code>%&lt;%</code>, you can rewrite the above code as follows:</p>
<pre>class=&quot;lang-R&quot;># Import `magrittr`
library(magrittr)

# Perform the same computations on `x` as above
x %>% log() %>%
    diff() %>%
    exp() %>%
    round(1)
</pre>
<p>Does this seem difficult to you? No worries! You'll learn more on how to go about this later on in this tutorial.</p>
<p><strong>Note</strong> that you need to import the <a rel="nofollow" href="https://www.rdocumentation.org/packages/magrittr/versions/1.5?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank"><code>magrittr</code> library</a> to get the above code to work. That's because the pipe operator is, as you read above, part of the <code>magrittr</code> library and is, since 2014, also a part of <code>dplyr</code>. If you forget to import the library, you'll get an error like <code>Error in eval(expr, envir, enclos): could not find function &quot;%>%&quot;</code>. </p>
<p>Also note that it isn't a formal requirement to add the parentheses after <code>log</code>, <code>diff</code> and <code>exp</code>, but that, within the R community, some will use it to increase the readability of the code. </p>
<p>In short, here are four reasons why you should be using pipes in R:</p>
<ul>
<li>You'll structure the sequence of your data operations from left to right, as apposed to from inside and out;</li>
<li>You'll avoid nested function calls; </li>
<li>You'll minimize the need for local variables and function definitions; And</li>
<li>You'll make it easy to add steps anywhere in the sequence of operations. </li>
</ul>
<p>These reasons are taken from the <a rel="nofollow" href="https://cran.r-project.org/web/packages/magrittr/magrittr.pdf" target="_blank"><code>magrittr</code> documentation itself</a>. Implicitly, you see the arguments of readability and flexibility returning.</p>
<h3>Additional Pipes</h3>
<p>Even though <code>%>%</code> is the (main) pipe operator of the <code>magrittr</code> package, there are a couple of other operators that you should know and that are part of the same package:</p>
<ul>
<li>The compound assignment operator <code>%&lt;>%</code>; </li>
</ul>
<pre>class=&quot;lang-R&quot;># Initialize `x` 
x &lt;- rnorm(100)

# Update value of `x` and assign it to `x`
x %&lt;>% abs %>% sort
</pre>
<ul>
<li>The tee operator <code>%T>%</code>;</li>
</ul>
<pre>class=&quot;lang-R&quot;>rnorm(200) %>%
matrix(ncol = 2) %T>%
plot %>% 
colSums
</pre>
<p><strong>Note</strong> that it's good to know for now that the above code chunk is actually a shortcut for: </p>
<pre>rnorm(200) %>%
matrix(ncol = 2) %T>%
{ plot(.); . } %>% 
colSums
</pre>
<p>But you'll see more about that later on! </p>
<ul>
<li>The exposition pipe operator <code>%$%</code>.</li>
</ul>
<pre>class=&quot;lang-R&quot;>data.frame(z = rnorm(100)) %$% 
  ts.plot(z)
</pre>
<p>Of course, these three operators work slightly differently than the main <code>%>%</code> operator. You'll see more about their functionalities and their usage later on in this tutorial!</p>
<p><strong>Note</strong> that, even though you'll most often see the <code>magrittr</code> pipes, you might also encounter other pipes as you go along! Some examples are <a rel="nofollow" href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/www.rdocumentation.org/packages/wrapr/versions/1.0.1?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank"><code>wrapr</code></a>'s dot arrow pipe <code>%.>%</code> or to dot pipe <code>%>.%</code>, or the <a rel="nofollow" href="http://r-posts.com/pipes-in-r-tutorial-for-beginners/www.win-vector.com/blog/2016/12/magrittrs-doppelganger" target="_blank">Bizarro pipe <code>->.;</code></a>.</p>
<p><a></a></p>
<h2>How to Use Pipes in R</h2>
<p>Now that you know how the <code>%>%</code> operator originated, what it actually is and why you should use it, it's time for you to discover how you can actually use it to your advantage. You will see that there are quite some ways in which you can use it! </p>
<h3>Basic Piping</h3>
<p>Before you go into the more advanced usages of the operator, it's good to first take a look at the most basic examples that use the operator. In essence, you'll see that there are 3 rules that you can follow when you're first starting out:</p>
<ul>
<li><code>f(x)</code> can be rewritten as <code>x %>% f</code></li>
</ul>
<p>In short, this means that functions that take one argument, <code>function(argument)</code>, can be rewritten as follows: <code>argument %>% function()</code>. Take a look at the following, more practical example to understand how these two are equivalent:</p>
<pre>class=&quot;lang-R&quot;># Compute the logarithm of `x` 
log(x)

# Compute the logarithm of `x` 
x %>% log()
</pre>
<ul>
<li><code>f(x, y)</code> can be rewritten as <code>x %>% f(y)</code></li>
</ul>
<p>Of course, there are a lot of functions that don't just take one argument, but multiple. This is the case here: you see that the function takes two arguments, <code>x</code> and <code>y</code>. Similar to what you have seen in the first example, you can rewrite the function by following the structure <code>argument1 %>% function(argument2)</code>, where <code>argument1</code> is the <code>magrittr</code> placeholder and <code>argument2</code> the function call.</p>
<p>This all seems quite theoretical. Let's take a look at a more practical example:</p>
<pre>class=&quot;lang-R&quot;># Round pi
round(pi, 6)

# Round pi 
pi %>% round(6)
</pre>
<ul>
<li><code>x %>% f %>% g %>% h</code> can be rewritten as <code>h(g(f(x)))</code></li>
</ul>
<p>This might seem complex, but it isn't quite like that when you look at a real-life R example:</p>
<pre>class=&quot;lang-R&quot;># Import `babynames` data
library(babynames)
# Import `dplyr` library
library(dplyr)

# Load the data
data(babynames)

# Count how many young boys with the name &quot;Taylor&quot; are born
sum(select(filter(babynames,sex==&quot;M&quot;,name==&quot;Taylor&quot;),n))

# Do the same but now with `%>%`
babynames%>%filter(sex==&quot;M&quot;,name==&quot;Taylor&quot;)%>%
            select(n)%>%
            sum
</pre>
<p><strong>Note</strong> how you work from the inside out when you rewrite the nested code: you first put in the <code>babynames</code>, then you use <code>%>%</code> to first <code>filter()</code> the data. After that, you'll select <code>n</code> and lastly, you'll <code>sum()</code> everything. </p>
<p><strong>Remember</strong> also that you already saw another example of such a nested code that was converted to more readable code in the beginning of this tutorial, where you used the <code>log()</code>, <code>diff()</code>, <code>exp()</code> and <code>round()</code> functions to perform calculations on <code>x</code>. </p>
<h4>Functions that Use the Current Environment</h4>
<p>Unfortunately, there are some exceptions to the more general rules that were outlined in the previous section. Let's take a look at some of them here. </p>
<p>Consider this example, where you use the <code>assign()</code> function to assign the value <code>10</code> to the variable <code>x</code>. </p>
<pre>class=&quot;lang-R&quot;># Assign `10` to `x`
assign(&quot;x&quot;, 10)

# Assign `100` to `x` 
&quot;x&quot; %>% assign(100)

# Return `x`
x
</pre>
<p>10</p>
<p>You see that the second call with the <code>assign()</code> function, in combination with the pipe, doesn't work properly. The value of <code>x</code> is not updated. </p>
<p>Why is this?</p>
<p>That's because the function assigns the new value <code>100</code> to a temporary environment used by <code>%>%</code>. So, if you want to use <code>assign()</code> with the pipe, you must be explicit about the environment:</p>
<pre>class=&quot;lang-R&quot;># Define your environment
env &lt;- environment()

# Add the environment to `assign()`
&quot;x&quot; %>% assign(100, envir = env)

# Return `x`
x
</pre>
<p>100</p>
<h4>Functions with Lazy Evalution</h4>
<p>Arguments within functions are only computed when the function uses them in R. This means that no arguments are computed before you call your function! That means also that the pipe computes each element of the function in turn.</p>
<p>One place that this is a problem is <code>tryCatch()</code>, which lets you capture and handle errors, like in this example:</p>
<pre>class=&quot;lang-R&quot;>tryCatch(stop(&quot;!&quot;), error = function(e) &quot;An error&quot;)

stop(&quot;!&quot;) %>% 
  tryCatch(error = function(e) &quot;An error&quot;)
</pre>
<p>'An error'</p>
<pre>Error in eval(expr, envir, enclos): !
Traceback:


1. stop(&quot;!&quot;) %>% tryCatch(error = function(e) &quot;An error&quot;)

2. eval(lhs, parent, parent)

3. eval(expr, envir, enclos)

4. stop(&quot;!&quot;)
</pre>
<p>You'll see that the nested way of writing down this line of code works perfectly, while the piped alternative returns an error. Other functions with the same behavior are <code>try()</code>, <code>suppressMessages()</code>, and <code>suppressWarnings()</code> in base R.</p>
<h3>Argument Placeholder</h3>
<p>There are also instances where you can use the pipe operator as an argument placeholder. Take a look at the following examples:</p>
<ul>
<li><code>f(x, y)</code> can be rewritten as <code>y %>% f(x, .)</code></li>
</ul>
<p>In some cases, you won't want the value or the <code>magrittr</code> placeholder to the function call at the first position, which has been the case in every example that you have seen up until now. Reconsider this line of code:</p>
<pre></pre></ul></nav>