-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_magrittr.Rmd
193 lines (148 loc) · 5.66 KB
/
03_magrittr.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
---
title: "03_magrittr"
output: html_document
---
class: middle, center, inverse
layout: false
# 3 `palmerpenguins`:<br><br>Palmer Archipelago (Antarctica) Penguin Data
---
background-image: url(https://raw.githubusercontent.com/allisonhorst/palmerpenguins/master/man/figures/logo.png)
background-position: 97.5% 2.5%
background-size: 7.5%
layout: true
---
## 3 `palmerpenguins`: Palmer Archipelago<br>(Antarctica) Penguin Data
.pull-left[
From here on, to illustrate the features of the `tidyverse` core packages we use data from the `palmerpenguins` package by [Allison Horst](https://allisonhorst.github.io/palmerpenguins/).
The package comes with data about penguins observed on islands in the Palmer Archipelago near Palmer Station, Antarctica.
]
.pull-right[
```{r, echo=F, fig.align='center', out.width='60%'}
knitr::include_graphics("https://tenor.com/view/penguin-fat-the-struggle-is-real-lazy-gif-4242854.gif")
```
]
---
## 3 `palmerpenguins`: Palmer Archipelago<br>(Antarctica) Penguin Data
.pull-left[
```{r, eval=F}
library(palmerpenguins)
penguins
```
```{r, echo=F}
print(penguins, width = 50)
```
]
.pull-right[
```{r, echo=F, fig.align='center', out.width='65%', out.height='65%'}
knitr::include_graphics("https://raw.githubusercontent.com/allisonhorst/palmerpenguins/master/man/figures/lter_penguins.png")
knitr::include_graphics("https://raw.githubusercontent.com/allisonhorst/palmerpenguins/master/man/figures/culmen_depth.png")
```
]
---
class: middle, center, inverse
layout: false
# 4.1 `magrittr`:<br><br>A Forward-Pipe Operator for `R`
---
background-image: url(https://raw.githubusercontent.com/tidyverse/magrittr/master/man/figures/logo.png)
background-position: 95% 5%
background-size: 7.5%
layout: true
---
## 4.1 `magrittr`: The Forward-Pipe Operator
`magrittr` comes with a set of operators:
- **Pipe Operator:** `%>%`<br><br>
- **Assignment Operator:** `%<>%`<br><br>
- **"Tee" Operator:** `%T>%`<br><br>
- **Exposition Operator:** `%$%`
--
<br>
Essentially, these operators aim to improve the readability of your code in multiple ways:
- arrange operations into an easily readable pipeline of chained commands (left-to-right),
- avoid nested function calls (inside-out),
- minimize the use of local variable assignments (`<-`) and function definitions, and
- easily add and/or delete steps in your pipeline without breaking the code.
???
The exposition operator: %$% (explodes out variables in a data frame, no need to use pull())
---
## 4.1 `magrittr`: The Forward-Pipe Operator
**Basic Piping:** forward a value or object (LHS) into the next function call (RHS) as **first** argument
```{r, eval=F}
x %>% f # equivalent to: f(x)
x %>% f(y) # equivalent to: f(x, y)
x %>% f %>% g %>% h # equivalent to: h(g(f(x)))
```
--
**Piping with placeholders:** forward a value or object (LHS) into the next function call (RHS) as **any** argument
```{r, eval=F}
x %>% f(.) # equivalent to: x %>% f
x %>% f(y, .) # equivalent to: f(y, x)
x %>% f(y, z = .) # equivalent to: f(y, z = x)
x %>% f(y = nrow(.), z = ncol(.)) # equivalent to: f(x, y = nrow(x), z = ncol(x))
```
--
**Building functions and pipelines:** a sequence of code starting with the placeholder (`.`) returns a function which can be used to later apply the pipeline to concrete values
```{r, eval=F}
f <- . %>% cos %>% sin # equivalent to: f <- function(.) sin(cos(.))
f(20) # equivalent to: the pipeline 20 %>% cos %>% sin
```
.footnote[
*Note: Find out more about `%>%` by running `vignette("magrittr")`. Type `%>%` using the shortcut: Ctrl + Shift + M.*
]
---
## 4.1 `magrittr`: The Forward-Pipe Operator
**Question:** What is the average body mass in grams for all penguins observed in the year 2007 (after excluding missing values)?
**In a pipeless world:**
```{r, eval=F}
mean(subset(penguins, year == 2007)$body_mass_g, na.rm = T)
# alternatively:
peng_bm_2007 <- subset(penguins, year == 2007)$body_mass_g
mean(peng_bm_2007, na.rm = T)
```
--
.pull-left[
**In a world full of pipes:**
```{r, eval=F}
penguins %>%
subset(year == 2007) %>%
.$body_mass_g %>%
mean(na.rm = T)
```
]
.pull-right[
<br>
- Sequential style improves readability!
- Less deciphering of nested function calls!
- No need to store intermediate results!
- Modular modification of pipeline steps!
]
.footnote[
*Note: As of version `4.1.0`, base `R` comes with a native pipe operator as well (`|>`).*
]
???
- Add or remove individual steps easily in your pipeline
- The `magrittr` forward pipe is imported by the `tidyverse`, no need to load it separately
---
## 4.1 `magrittr`: The Forward-Pipe Operator
**Advanced piping:** Use the more advanced pipe operators to further streamline your workflow.
.panelset[
.panel[.panel-name[Tee Pipe]
`%T>%` can be used to trigger the side-effect of a function, e.g., for plotting or printing results, and let the original data bypass the respective step.
```{r}
penguins[1:5, c("island", "bill_length_mm")] %T>% print %>% .$bill_length_mm %>% mean(na.rm=T)
```
]
.panel[.panel-name[Exposition Pipe]
`%$%` exposes the names in LHS object to the RHS expression. This is useful if the RHS expression does not allow for a separate `data` argument.
```{r, fig.width=6, fig.asp=0.618, fig.retina=3}
penguins %$% plot(species, bill_length_mm) # equivalent to: plot(penguins$species, penguins$bill_length_mm)
```
]
.panel[.panel-name[Assignment Pipe]
`%<>%` can be used equivalently to the base `R` assignment operator (`<-`). It reassign the result of the of the pipeline to the starting variable.
```{r}
var <- penguins$bill_length_mm
var %<>% mean(na.rm=T)
var
```
]
]