r4ds/pipes.Rmd

259 lines
11 KiB
Plaintext
Raw Normal View History

2016-03-01 22:29:58 +08:00
# Pipes
Pipes let you transform the way you call deeply nested functions. Using a pipe doesn't affect what the code does; behind the scenes it is run in (almost) the exact same way. What the pipe does is change how _you_ write, and read, code.
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
You've been using the pipe for a while now, so you already understand the basics. The point of this chapter is to explore the pipe in more detail. You'll learn the alternatives that the pipe replaces, and the pros and cons of the pipe. Importantly, you'll also learn situations in which you should avoid the pipe.
2016-03-01 22:29:58 +08:00
The pipe, `%>%`, comes from the __magrittr__ package by Stefan Milton Bache. This package provides a handful of other helpful tools if you explicitly load it. We'll explore some of those tools to close out the chapter.
2016-03-10 22:13:13 +08:00
2016-07-19 21:01:50 +08:00
### Prerequisites
This chapter focusses on `%>%` which is normally loaded for you by packages in the tidyverse. Here we'll focus on it alone, so we'll make it available directly from magrittr. We'll also extract the `diamonds` dataset out of ggplot2 to use in some examples.
```{r setup}
library(magrittr)
diamonds <- ggplot2::diamonds
```
2016-03-10 22:13:13 +08:00
## Piping alternatives
The point of the pipe is to help you write code in a way that easier to read and understand. To see why the pipe is so useful, we're going to explore a number of ways of writing the same code. Let's use code to tell a story about a little bunny named Foo Foo:
2016-03-01 22:29:58 +08:00
> Little bunny Foo Foo
> Went hopping through the forest
> Scooping up the field mice
> And bopping them on the head
We'll start by defining an object to represent little bunny Foo Foo:
```{r, eval = FALSE}
foo_foo <- little_bunny()
```
2016-03-10 22:13:13 +08:00
And we'll use a function for each key verb: `hop()`, `scoop()`, and `bop()`. Using this object and these verbs, there are (at least) four ways we could retell the story in code:
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
1. Save each intermediate step as a new object.
1. Overwrite the original object many times.
1. Compose functions.
1. Use the pipe.
2016-03-01 22:29:58 +08:00
We'll work through each approach, showing you the code and talking about the advantages and disadvantages. Note that these are made up functions; please don't expect this code to do something.
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
### Intermediate steps
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
The simplest approach is to save each step as a new object:
2016-03-01 22:29:58 +08:00
```{r, eval = FALSE}
foo_foo_1 <- hop(foo_foo, through = forest)
foo_foo_2 <- scoop(foo_foo_1, up = field_mice)
foo_foo_3 <- bop(foo_foo_2, on = head)
```
The main downside of this form is that it forces you to name each intermediate element. If there are natural names, this form feels natural, and you should use it. But in this example, there aren't natural names, and we're adding numeric suffixes just to make the names unique. That leads to two problems: the code is cluttered with unimportant names, and you have to carefully increment the suffix on each line. Whenever I write code like this, I invariably use the wrong number on one line and then spend 10 minutes scratching my head and trying to figure out what went wrong with my code.
2016-03-01 22:29:58 +08:00
You may worry that this form creates many intermediate copies of your data and takes up a lot of memory, but that's not necessary. First, worrying about memory is not a useful way to spend your time: worry about it when it becomes a problem (i.e. you run out of memory), not before. Second, R isn't stupid: if you're working with data frames, R will share columns where possible. Let's take a look at an actual data manipulation pipeline where we add a new column to the `diamonds` dataset from ggplot2:
2016-03-01 22:29:58 +08:00
```{r}
2016-07-19 21:01:50 +08:00
diamonds2 <- dplyr::mutate(diamonds, price_per_carat = price / carat)
2016-03-01 22:29:58 +08:00
2016-07-19 21:01:50 +08:00
pryr::object_size(diamonds)
pryr::object_size(diamonds2)
pryr::object_size(diamonds, diamonds2)
2016-03-01 22:29:58 +08:00
```
`pryr::object_size()` gives the memory occupied by all of its arguments. The results seem counterintuitive at first:
* `diamonds` takes up 3.46 MB,
* `diamonds2` takes up 3.89 MB,
* `diamonds` and `diamonds2` together take up 3.89 MB!
2016-03-10 22:13:13 +08:00
How can that work? Well, `diamonds2` has 10 columns in common with `diamonds`: there's no need to duplicate all that data, so the two data frames have variables in common. These variables will only get copied if you modify one of them.
In the following example, we modify a single value in `diamonds$carat`. That means the `carat` variable can no longer be shared between the two data frames, and a copy must be made. The individual size of each data frame will be unchanged, but the collective size increases:
2016-03-01 22:29:58 +08:00
```{r}
diamonds$carat[1] <- NA
2016-07-19 21:01:50 +08:00
pryr::object_size(diamonds)
pryr::object_size(diamonds2)
pryr::object_size(diamonds, diamonds2)
2016-03-01 22:29:58 +08:00
```
2016-03-10 22:13:13 +08:00
(Note that we use `pryr::object_size()` here, not the built-in `object.size()`. `object.size()` isn't quite smart enough to recognise that the columns are shared across multiple data frames.)
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
### Overwrite the original
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
Instead of creating intermediate objects at each step, we could overwrite the original object:
2016-03-01 22:29:58 +08:00
```{r, eval = FALSE}
foo_foo <- hop(foo_foo, through = forest)
foo_foo <- scoop(foo_foo, up = field_mice)
foo_foo <- bop(foo_foo, on = head)
```
This is less typing (and less thinking), so you're less likely to make mistakes. However, there are two problems:
2016-03-10 22:13:13 +08:00
1. Debugging is painful: if you make a mistake you'll need to re-run the
complete pipeline from the beginning.
2016-03-01 22:29:58 +08:00
1. The repetition of the object being transformed (we've written `foo_foo` six
times!) obscures what's changing on each line.
2016-03-10 22:13:13 +08:00
### Function composition
Another approach is to abandon assignment and just string the function calls together:
2016-03-01 22:29:58 +08:00
```{r, eval = FALSE}
bop(
scoop(
hop(foo_foo, through = forest),
up = field_mice
),
on = head
)
```
2016-03-10 22:13:13 +08:00
Here the disadvantage is that you have to read from inside-out, from right-to-left, and that the arguments end up spread far apart (evocatively called the
[dagwood sandwhich](https://en.wikipedia.org/wiki/Dagwood_sandwich) problem). In short, this code is hard for a human to consume.
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
### Use the pipe
2016-03-01 22:29:58 +08:00
Finally, we can use the pipe:
```{r, eval = FALSE}
foo_foo %>%
hop(through = forest) %>%
scoop(up = field_mouse) %>%
bop(on = head)
```
2016-03-10 22:13:13 +08:00
This is my favourite form, because it focusses on verbs, not nouns. You can read this series of function compositions like it's a set of imperative actions. Foo foo hops, then scoops, then bops. The downside, of course, is that you need to be familiar with the pipe. If you've never seen `%>%` before, you'll have no idea what this code does. Fortunately, however, most people pick up the idea very quickly, so when you share you code with others who aren't familiar with the pipe, you can easily teach them.
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
The pipe works by doing "lexical transformation". Behind the scenes, magrittr reassemble the code in the pipe to a form that works by overwriting an intermediate object. When you run a pipe like the one above, magrittr does something like this:
2016-03-01 22:29:58 +08:00
```{r, eval = FALSE}
2016-03-10 22:13:13 +08:00
my_pipe <- function(.) {
. <- hop(., through = forest)
. <- scoop(., up = field_mice)
bop(., on = head)
}
my_pipe(foo_foo)
2016-03-01 22:29:58 +08:00
```
2016-03-10 22:13:13 +08:00
This means that the pipe won't work for two classes of functions:
1. Functions that use the current environment. For example, `assign()`
will create a new variable with the given name in the current environment:
```{r}
assign("x", 10)
x
"x" %>% assign(100)
x
```
The use of assign with the pipe does not work because it assigns it to
a temporary environment used by `%>%`. If you do want to use assign with the
pipe, you must be explicit about the environment:
```{r}
env <- environment()
"x" %>% assign(100, envir = env)
x
```
Other functions with this problem include `get()` and `load()`
1. Functions that use lazy evaluation. In R, function arguments
2016-03-10 22:13:13 +08:00
are only computed when the function uses them, not prior to calling the
function. The pipe computes each element in turn, so you can't
2016-03-10 22:13:13 +08:00
rely on this behaviour.
One place that this is a problem is `tryCatch()`, which lets you capture
and handle errors:
```{r, error = TRUE}
tryCatch(stop("!"), error = function(e) "An error")
stop("!") %>%
tryCatch(error = function(e) "An error")
```
There are a relatively wide class of functions with this behaviour.
This includes `try()`, `supressMessages()`, and `suppressWarnings()`
in base R.
## When not to use the pipe
The pipe is a powerful tool, but it's not the only tool at your disposal, and it doesn't solve every problem! Pipes are most useful for rewriting a fairly short linear sequence of operations. I think you should reach for another tool when:
* Your pipes get longer than five or six lines. In that case, create
intermediate objects with meaningful names. That will make debugging easier,
because you can more easily check the intermediate results, and it makes
it easier to understand your code, because the variable names can help
communicate intent.
* You have multiple inputs or outputs. If there isn't one primary object
being transformed, but two or more objects being combined together,
don't use the pipe.
* You are starting to think about a directed graph with a complex
dependency structure. Pipes are fundamentally linear and expressing
complex relationships with them will typically yield confusing code.
2016-03-01 22:29:58 +08:00
2016-03-10 22:13:13 +08:00
## Other tools from magrittr
2016-03-01 22:29:58 +08:00
The pipe is provided by the magrittr package, by Stefan Milton Bache. Most of the packages you work with in this book will automatically provide `%>%` for you. You might want to load magrittr yourself if you're using another package, or you want to access some of the other pipe variants that magrittr provides.
2016-03-01 22:29:58 +08:00
* When working with more complex pipes, it's sometimes useful to call a
2016-03-01 22:29:58 +08:00
function for its side-effects. Maybe you want to print out the current
object, or plot it, or save it to disk. Many times, such functions don't
return anything, effectively terminating the pipe.
To work around this problem, you can use the "tee" pipe. `%T>%` works like
2016-03-10 22:13:13 +08:00
`%>%` except that it returns the LHS instead of the RHS. It's called
2016-03-01 22:29:58 +08:00
"tee" because it's like a literal T-shaped pipe.
```{r}
rnorm(100) %>%
matrix(ncol = 2) %>%
plot() %>%
str()
rnorm(100) %>%
matrix(ncol = 2) %T>%
plot() %>%
str()
```
2016-03-10 22:13:13 +08:00
* If you're working with functions that don't have a data frame based API
2016-03-01 22:29:58 +08:00
(i.e. you pass them individual vectors, not a data frame and expressions
to be evaluated in the context of that data frame), you might find `%$%`
useful. It "explodes" out the variables in a data frame so that you can
refer to them explicitly. This is useful when working with many functions
in base R:
```{r}
mtcars %$%
cor(disp, mpg)
```
* For assignment magrittr provides the `%<>%` operator which allows you to
replace code like:
```R
mtcars <- mtcars %>% transform(cyl = cyl * 2)
```
with
```R
mtcars %<>% transform(cyl = cyl * 2)
```
I'm not a fan of this operator because I think assignment is such a
special operation that it should always be clear when it's occurring.
In my opinion, a little bit of duplication (i.e. repeating the
name of the object twice), is fine in return for making assignment
more explicit.