I recently wrote about ggplot2 style, and in this one I’d like to discuss the pipe operator, %>%
. These notes are drawn from the comments on the pipe section of the tidyverse style guide.
Newlines and the pipe
For the pipe operator, %>%
, I recommend you can keep the pipe on the same line for 2 (occassionally 3) pipes, else there should always be a newline. You should also consider whether the pipe is adding any clarity to the steps, when there is only one pipe.
library(tidyr)
library(dplyr)
# Acceptable
count(iris, Species)
# OK
iris %>% count(Species)
# good
iris %>%
count(Species) %>%
ggplot(aes(x = n,
y = Species))
# OK-ish
iris %>% group_by(Species) %>% tally()
# better
iris %>%
group_by(Species) %>%
tally()
# good!
iris %>%
select(Sepal.Width,
Sepal.Length,
Petal.Length) %>%
mutate(Sepal.Petal.Length = Sepal.Length + Petal.Length) %>%
summarise()
# bad
iris %>% select(Sepal.Width, Sepal.Length, Petal.Length) %>%
mutate(Sepal.Petal.Length = Sepal.Length + Petal.Length) %>%
I have two reasons for this:
- It’s easier to edit and change code.
iris %>%
select(Sepal.Width,
# Sepal.Length,
Petal.Length) %>%
mutate(Sepal.Petal.Length = Sepal.Length + Petal.Length) %>%
summarise()
- It reads quicker
- You can use the pipe to input into ggplot
iris %>%
select(Sepal.Width,
# Sepal.Length,
Petal.Length) %>%
mutate(Sepal.Petal.Length = Sepal.Length + Petal.Length) %>%
# summarize() %>%
ggplot(aes(x = Sepal.Petal.Length,
y = Sepal.Width)) +
geom_point()