Pipes

In the primers (and in Lesson 4) you were introduced to the pipe operator |>, which takes the object on the left of the pipe and passes it as the first argument to the function on the right.

Like, you can find the average of the numbers 1, 2, 3, 4, and 5 like this:

mean(c(1, 2, 3, 4, 5))
## [1] 3

Or like this:

c(1, 2, 3, 4, 5) |> mean()
## [1] 3

Pipe styles

There are actually two pipe operators in R, and you’ll see both in the wild.

René Magritte, “The Treachery of Images”

In 99% of cases, the two pipes are the same (see here for details about their differences; or see this too). The magrittr %>% is still wildly popular and you’ll see code online that uses it. The main downside to the magrittr pipe is that it requires that you load a package to use it. If you run library(tidyverse), you’ll have access to it, and in this class you’ll load tidyverse 100% of the time, so you can use %>% without any problems.

I switched my R classes away from %>% to |> starting in January 2024, mostly following the example of Hadley Wickham (inventor of ggplot, dplyr, and pretty much all the tidyverse). The first edition of R for Data Science by Hadley Wickham and Garrett Grolemund used the magrittr pipe (%>%) extensively, which is partially why it became so popular. In the second edition of R for Data Science, though, published in June 2023, they switched to the native pipe (|>). I took that as a cue to do the same in my own R work.

So I’m teaching you the native pipe (|>). But don’t panic if you see the magrittr pipe (%>%) out there. And feel free to use it too. Again, 99% of the time, it works the same:

# {magrittr} pipe
c(1, 2, 3, 4, 5) %>% mean()
## [1] 3
# Native pipe
c(1, 2, 3, 4, 5) |> mean()
## [1] 3

Just don’t mix the two pipes in the same chain of functions. This is bad:

mpg |> 
  group_by(...) %>%
  summarize(...) |>
  filter(...) %>%
  select(...)

Pipes and fancy fonts

The |> is actually designed to look like a triangle pointing to the left: ▷

But in pretty much all fonts, the | character is too tall and doesn’t align with the top and bottom of the left side of the > character, so it doesn’t quite look right.

If you want to be fancy and cool, you can download and install a special programming font named Fira Code that uses some typographic magic to make certain combinations of characters appear differently in RStudio.

Like, check out the == and != and <= and |> characters normally:

Sample code with a normal font

And now look at them with Fira Code, where they’re magically transformed into fancier combinations:

Sample code with Fira Code

The underlying code is the same—the != combination isn’t actually converted to a ≠ character. It just looks that way.

Install the font, then go to Tools > Global Options… > Appearance and change the editor font if you want to use Fira Code in RStudio.

Pipe keyboard shortcut

In RStudio, you can insert a pipe operator with a keyboard shortcut:

  • On macOS: + + M1

  • On Windows: ctrl + shift + M

1 The “M” here refers to the magrittr pipe

You can control which kind of pipe is inserted by going to Tools > Global options… > Code > Editing and toggling the “Use native pipe operator” option:

RStudio pipe options