2022-05-14 04:46:49 +08:00
# Workflow: Pipes {#sec-workflow-pipes}
2021-04-21 21:25:39 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| results: "asis"
#| echo: false
2022-12-16 14:41:10 +08:00
2022-05-14 04:46:49 +08:00
source("_common.R")
2022-04-27 21:01:00 +08:00
status("complete")
2022-02-16 05:34:31 +08:00
```
2022-02-21 05:15:13 +08:00
The pipe, `|>`, is a powerful tool for clearly expressing a sequence of operations that transform an object.
2022-05-06 04:43:59 +08:00
We briefly introduced pipes in the previous chapter, but before going too much farther, we want to give a few more details and discuss `%>%`, a predecessor to `|>`.
2022-02-24 03:33:21 +08:00
To add the pipe to your code, we recommend using the build-in keyboard shortcut Ctrl/Cmd + Shift + M.
2022-05-14 04:46:49 +08:00
You'll need to make one change to your RStudio options to use `|>` instead of `%>%` as shown in @fig-pipe-options; more on `%>%` shortly.
2022-02-24 03:33:21 +08:00
2022-05-08 13:32:25 +08:00
```{r}
2022-05-14 04:46:49 +08:00
#| label: fig-pipe-options
2022-05-08 13:32:25 +08:00
#| echo: false
2022-05-14 04:46:49 +08:00
#| fig-cap: >
2022-09-02 19:35:55 +08:00
#| To insert `|>`, make sure the "Use native pipe operator" option is checked.
2022-05-14 04:46:49 +08:00
#| fig-alt: >
2022-02-24 03:33:21 +08:00
#| Screenshot showing the "Use native pipe operator" option which can
#| be found on the "Editing" panel of the "Code" options.
2022-05-08 13:32:25 +08:00
2022-02-24 03:33:21 +08:00
knitr::include_graphics("screenshots/rstudio-pipe-options.png")
```
2021-04-21 21:25:39 +08:00
2022-02-17 05:51:27 +08:00
## Why use a pipe?
2021-04-21 21:25:39 +08:00
2022-05-06 04:43:59 +08:00
Each individual dplyr verb is quite simple, so solving complex problems typically requires combining multiple verbs.
2022-02-21 05:15:13 +08:00
For example, the last chapter finished with a moderately complex pipe:
2021-04-21 21:25:39 +08:00
2022-05-08 13:32:25 +08:00
```{r}
#| eval: false
2022-02-17 05:51:27 +08:00
flights |>
filter(!is.na(arr_delay), !is.na(tailnum)) |>
group_by(tailnum) |>
2022-11-19 06:07:54 +08:00
summarize(
2022-02-17 05:51:27 +08:00
delay = mean(arr_delay, na.rm = TRUE),
n = n()
)
2022-02-16 04:49:05 +08:00
```
2021-04-21 21:25:39 +08:00
2022-09-02 19:35:55 +08:00
Even though this pipe has four steps, it's easy to skim because the verbs come at the start of each line: start with the `flights` data, then filter, then group, then summarize.
2021-04-21 21:25:39 +08:00
2022-02-17 05:51:27 +08:00
What would happen if we didn't have the pipe?
2022-02-21 05:15:13 +08:00
We could nest each function call inside the previous call:
2022-02-16 04:49:05 +08:00
2022-05-08 13:32:25 +08:00
```{r}
#| eval: false
2022-11-19 06:07:54 +08:00
summarize(
2022-02-17 05:51:27 +08:00
group_by(
filter(
flights,
!is.na(arr_delay), !is.na(tailnum)
),
tailnum
),
delay = mean(arr_delay, na.rm = TRUE
),
n = n()
)
2022-02-16 04:49:05 +08:00
```
2022-02-21 05:15:13 +08:00
Or we could use a bunch of intermediate variables:
2022-02-16 04:49:05 +08:00
2022-05-08 13:32:25 +08:00
```{r}
#| eval: false
2022-02-17 05:51:27 +08:00
flights1 <- filter(flights, !is.na(arr_delay), !is.na(tailnum))
flights2 <- group_by(flights1, tailnum)
2022-11-19 06:07:54 +08:00
flights3 <- summarize(flight2,
2022-02-17 05:51:27 +08:00
delay = mean(arr_delay, na.rm = TRUE),
n = n()
)
2022-02-16 04:49:05 +08:00
```
2022-04-27 21:01:00 +08:00
While both of these forms have their time and place, the pipe generally produces data analysis code that's both easier to write and easier to read.
2022-02-21 05:15:13 +08:00
2022-02-16 05:34:31 +08:00
## magrittr and the `%>%` pipe
2022-02-16 04:49:05 +08:00
2022-04-27 21:01:00 +08:00
If you've been using the tidyverse for a while, you might be familiar with the `%>%` pipe provided by the **magrittr** package.
2022-04-28 20:40:12 +08:00
The magrittr package is included in the core tidyverse, so you can use `%>%` whenever you load the tidyverse:
2022-02-17 05:51:27 +08:00
2022-05-08 13:32:25 +08:00
```{r}
#| message: false
2022-02-17 05:51:27 +08:00
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
2022-11-19 06:07:54 +08:00
summarize(n = n())
2022-02-17 05:51:27 +08:00
```
2022-02-16 04:49:05 +08:00
2022-02-17 05:51:27 +08:00
For simple cases `|>` and `%>%` behave identically.
So why do we recommend the base pipe?
Firstly, because it's part of base R, it's always available for you to use, even when you're not using the tidyverse.
2022-02-24 03:33:21 +08:00
Secondly, `|>` is quite a bit simpler than `%>%`: in the time between the invention of `%>%` in 2014 and the inclusion of `|>` in R 4.1.0 in 2021, we gained a better understanding of the pipe.
This allowed the base implementation to jettison infrequently used and less important features.
2022-02-16 04:49:05 +08:00
2022-12-07 00:51:11 +08:00
## `|>` vs. `%>%`
2022-02-16 04:49:05 +08:00
2022-05-06 04:43:59 +08:00
While `|>` and `%>%` behave identically for simple cases, there are a few important differences.
2022-04-27 21:01:00 +08:00
These are most likely to affect you if you're a long-term user of `%>%` who has taken advantage of some of the more advanced features.
But they're still good to know about even if you've never used `%>%` because you're likely to encounter some of them when reading wild-caught code.
2022-02-16 04:49:05 +08:00
2022-02-24 03:33:21 +08:00
- By default, the pipe passes the object on its left hand side to the first argument of the function on the right-hand side.
2022-04-27 21:01:00 +08:00
`%>%` allows you change the placement with a `.` placeholder.
2022-02-21 05:15:13 +08:00
For example, `x %>% f(1)` is equivalent to `f(x, 1)` but `x %>% f(1, .)` is equivalent to `f(1, x)`.
2022-04-27 21:01:00 +08:00
R 4.2.0 added a `_` placeholder to the base pipe, with one additional restriction: the argument has to be named.
2022-02-24 03:33:21 +08:00
For example, `x |> f(1, y = _)` is equivalent to `f(1, y = x)`.
2022-04-27 21:01:00 +08:00
- The `|>` placeholder is deliberately simple and can't replicate many features of the `%>%` placeholder: you can't pass it to multiple arguments, and it doesn't have any special behavior when the placeholder is used inside another function.
For example, `df %>% split(.$var)` is equivalent to `split(df, df$var)` and `df %>% {split(.$x, .$y)}` is equivalent to `split(df$x, df$y)`.
2022-02-16 04:49:05 +08:00
2022-11-04 23:44:59 +08:00
With `%>%` you can use `.` on the left-hand side of operators like `$`, `[[`, `[` (which you'll learn about in @sec-subset-many), so you can extract a single column from a data frame with (e.g.) `mtcars %>% .$cyl`.
2022-04-27 21:01:00 +08:00
A future version of R may add similar support for `|>` and `_`.
2022-09-01 19:43:57 +08:00
For the special case of extracting a column out of a data frame, you can also use `dplyr::pull()`:
2022-02-16 04:49:05 +08:00
2022-02-17 05:51:27 +08:00
```{r}
mtcars |> pull(cyl)
```
2022-02-16 04:49:05 +08:00
2022-04-27 21:01:00 +08:00
- `%>%` allows you to drop the parentheses when calling a function with no other arguments; `|>` always requires the parentheses.
2022-02-16 04:49:05 +08:00
2022-04-27 21:01:00 +08:00
- `%>%` allows you to start a pipe with `.` to create a function rather than immediately executing the pipe; this is not supported by the base pipe.
2022-04-27 22:05:56 +08:00
Luckily there's no need to commit entirely to one pipe or the other --- you can use the base pipe for the majority of cases where it's sufficient, and use the magrittr pipe when you really need its special features.
2022-09-29 23:36:22 +08:00
2022-12-05 02:05:38 +08:00
## `|>` vs `+`
Sometimes we'll turn the end of a pipeline of data transformation into a plot.
Watch for the transition from `|>` to `+`.
We wish this transition wasn't necessary but unfortunately ggplot2 was created before the pipe was discovered.
```{r}
#| eval: false
diamonds |>
count(cut, clarity) |>
2022-12-13 02:35:15 +08:00
ggplot(aes(x = clarity, y = cut, fill = n)) +
2022-12-05 02:05:38 +08:00
geom_tile()
```
2022-09-29 23:36:22 +08:00
## Summary
2022-12-05 02:05:38 +08:00
In this chapter, you've learned more about the pipe: why we recommend it and some of the history that lead to `|>`.
2022-09-29 23:36:22 +08:00
The pipe is important because you'll use it again and again throughout your analysis, but hopefully it will quickly become invisible and your fingers will type it (or use the keyboard shortcut) without your brain having to think too much about it.
In the next chapter, we switch back to data science tools, learning about tidy data.
Tidy data is a consistent way of organizing your data frames that is used throughout the tidyverse.
2022-11-12 22:13:31 +08:00
This consistency makes your life easier because once you have tidy data, it just works with the vast majority of tidyverse functions.
2022-09-29 23:36:22 +08:00
Of course, life is never easy and most datasets that you encounter in the wild will not already be tidy.
So we'll also teach you how to use the tidyr package to tidy your untidy data.