forked from tidyverse/style
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipes.Rmd
executable file
·143 lines (106 loc) · 3.23 KB
/
pipes.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Pipes
## Introduction
Use `%>%` to emphasise a sequence of actions, rather than the object that the actions are being performed on.
Avoid using the pipe when:
* You need to manipulate more than one object at a time. Reserve pipes for a
sequence of steps applied to one primary object.
* There are meaningful intermediate objects that could be given
informative names.
## Whitespace
`%>%` should always have a space before it, and should usually be followed by a new line. After the first step, each line should be indented by two spaces. This structure makes it easier to add new steps (or rearrange existing steps) and harder to overlook a step.
```{r, eval = FALSE}
# Good
iris %>%
group_by(Species) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
gather(measure, value, -Species) %>%
arrange(value)
# Bad
iris %>% group_by(Species) %>% summarize_all(mean) %>%
ungroup %>% gather(measure, value, -Species) %>%
arrange(value)
```
## Long lines
If the arguments to a function don't all fit on one line, put each argument on
its own line and indent:
```{r, eval = FALSE}
iris %>%
group_by(Species) %>%
summarise(
Sepal.Length = mean(Sepal.Length),
Sepal.Width = mean(Sepal.Width),
Species = n_distinct(Species)
)
```
## Short pipes
A one-step pipe can stay on one line, but unless you plan to expand it later on, you should consider rewriting it to a regular function call.
```{r, eval = FALSE}
# Good
iris %>% arrange(Species)
iris %>%
arrange(Species)
arrange(iris, Species)
```
Sometimes it's useful to include a short pipe as an argument to a function in a
longer pipe. Carefully consider whether the code is more readable with a short
inline pipe (which doesn't require a lookup elsewhere) or if it's better to move
the code outside the pipe and give it an evocative name.
```{r, eval = FALSE}
# Good
x %>%
select(a, b, w) %>%
left_join(y %>% select(a, b, v), by = c("a", "b"))
# Better
x_join <- x %>% select(a, b, w)
y_join <- y %>% select(a, b, v)
left_join(x_join, y_join, by = c("a", "b"))
```
## No arguments
magrittr allows you to omit `()` on functions that don't have arguments. Avoid this feature.
```{r, eval = FALSE}
# Good
x %>%
unique() %>%
sort()
# Bad
x %>%
unique %>%
sort
```
## Assignment
There are three acceptable forms of assignment:
* Variable name and assignment on separate lines:
```{r, eval = FALSE}
iris_long <-
iris %>%
gather(measure, value, -Species) %>%
arrange(-value)
```
* Variable name and assignment on the same line:
```{r, eval = FALSE}
iris_long <- iris %>%
gather(measure, value, -Species) %>%
arrange(-value)
```
* Assignment at the end of the pipe with `->`:
```{r, eval = FALSE}
iris %>%
gather(measure, value, -Species) %>%
arrange(-value) ->
iris_long
```
I think this is the most natural to write, but makes reading a little
harder: when the name comes first, it can act as a heading to remind
you of the purpose of the pipe.
The magrittr package provides the `%<>%` operator as a shortcut for modifying an object in place. Avoid this operator.
```{r, eval = FALSE}
# Good
x <- x %>%
abs() %>%
sort()
# Bad
x %<>%
abs() %>%
sort()
```