-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023-NHSR-Conf-Presentation.qmd
511 lines (352 loc) · 10.4 KB
/
2023-NHSR-Conf-Presentation.qmd
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
---
title: "Old dog and new tricks<br>Learning to {purrr}"
author: "Tom Smith<br>Insight Manager<br>Nottingham University Hospitals NHS Trust"
format:
revealjs:
slide-number: true
incremental: true
output-file: index.html
theme: [default, css/custom_styles.scss]
---
## purrr for functional programming
![](img/purrr.jpg){fig-align="center"}
Ahhh, a friendly cat...
## Scary functional programming
![](img/ugly-cat.jpg){fig-align="center"}
Run away now!
## Aims
* Don't be scary!
. . .
:::{.nonincremental}
* Practical intro to purrr
* A "mental model" which helped me
* A helpful chart-making example
:::
. . .
Functional programming is for another day (and another speaker!)
## I wouldn't start from here
From the tidyverse documentation:
> "the best place to start is the family of map() functions which allow you to replace many for loops with code that is both more succinct and easier to read."
and:
> "The best place to learn about the map() functions is the [iteration chapter in R for data science.](https://r4ds.had.co.nz/iteration.html)"
::: {.notes}
Purrr is part of the tidyverse, and both of these comments are true.
The first hints at the fact that once you grasp the map() function, you'll already be able to use it's close relations in helpful ways.
The second says the best place to learn is Hadley's book R for Data Science. In the meantime I hope this presentation helps, and at the end there are links to these and other resources.
:::
## Loops vs. Map
Why should I learn how to use map()?
## Loop example
```{r}
#| echo: true
#| output-location: fragment
# we need a vector to iterate over
food <- c("croissant", "baked potato")
# we need to create a place to put the results (the right size)
result <- vector("character", length(food))
# here's the loop
for(i in seq_along(food)){
result[[i]] <- paste("Hot", food[[i]])
}
result
```
## Map example
```{r}
#| echo: true
#| output-location: fragment
# we still need a vector to iterate over
food <- c("croissant", "baked potato")
# we create a function to do the "work"
heat_the_food <- function(food){
paste("Hot", food)
}
# here's the loop
result <- purrr::map(food, heat_the_food)
result
```
::: {.notes}
Ignore for a moment the shape of the result. In the loop example we got a vector back. In the map example we have a list. We'll come back to that shortly.
But even with that difference you're sceptical. Tom, you're short-changing us. We're happy with loops. 14 lines of code. You're talking about map() being simpler, and you still took 14 lines.
Lets go back and look in more detail...
:::
## Comparison {auto-animate=true}
loop
```{.r}
# we need a vector to iterate over
food <- c("croissant", "baked potato")
# we need to create a place to put the results (the right size)
result <- vector("character", length(food))
# here's the loop
for(i in seq_along(food)){
result[[i]] <- paste("Hot", food[[i]])
}
result
```
## Comparison {auto-animate=true}
map
```{.r}
# we need a vector to iterate over
food <- c("croissant", "baked potato")
# we create a function to do the "work"
heat_the_food <- function(food){
paste("Hot", food)
}
# here's the loop
result <- purrr::map(food, heat_the_food)
result
```
## Comparison {auto-animate=true}
loop
```{.r code-line-numbers="|6|9|11"}
# we need a vector to iterate over
food <- c("croissant", "baked potato")
# we need to create a place to put the results (the right size)
result <- vector("character", length(food))
# here's the loop
for(i in seq_along(food)){
result[[i]] <- paste("Hot", food[[i]])
}
result
```
There is more "boilerplate" code in a loop, and code explanations end up in comments (which might not exist)
## Comparison {auto-animate=true}
map
```{.r code-line-numbers="|6|8|13"}
# we need a vector to iterate over
food <- c("croissant", "baked potato")
# we create a function to do the "work"
heat_the_food <- function(food){
paste("Hot", food)
}
# here's the loop
result <- purrr::map(food, heat_the_food)
result
```
The code is easier to read, and we can use function names not comments to describe what it's doing
## Arguments
```{.r}
purrr::map(.x, .f)
# .x A list or atomic vector
# .f A function
```
### Mental model
```{.r}
purrr::map(food, heat_the_food)
purrr::map(subjects, action)
purrr::map(noun, verb)
purrr::map(raw_material, instructions)
purrr::map(ingredients, recipe)
```
## Cakes!
purrr::map(.x, .f)
:::: columns
::: {.column width="30%" .fragment .centre}
.x
![](img/cake-x.jpg){width=300}
:::
::: {.column width="30%" .fragment .centre}
.f
![](img/cake-f.jpg){width=300}
:::
::: {.column width="10%" .fragment .centre}
=
:::
::: {.column width="30%" .fragment .centre}
result
![](img/cake-res.jpg){width=300}
:::
::::
## Cars!
purrr::map(.x, .f)
:::: columns
::: {.column width="30%" .fragment .centre}
.x
![](img/car-x.jpg){width=300}
:::
::: {.column width="30%" .fragment .centre}
.f
![](img/car-f.png){width=300}
:::
::: {.column width="10%" .fragment .centre}
=
:::
::: {.column width="30%" .fragment .centre}
result
![](img/car-res.jpg){width=300}
:::
::::
## Ikea!
purrr::map(.x, .f)
:::: columns
::: {.column width="30%" .fragment .centre}
.x
![](img/ikea-x.jpg){width=300}
:::
::: {.column width="30%" .fragment .centre}
.f
![](img/ikea-f.jpg){width=300}
:::
::: {.column width="10%" .fragment .centre}
=
:::
::: {.column width="30%" .fragment .centre}
result
![](img/ikea-res.jpg){width=300}
:::
::::
## Real-world example
![](img/charts.png)
## Some data
```{r echo=TRUE}
set.seed(100)
# make some timeseries data for 5 metrics
data_long <- data.frame(
metric = paste0("Metric ", rep(c(1,2,3,4,5), each = 45)),
date = rep(seq.Date(from = as.Date("2020-01-01"), to = as.Date("2023-09-01"), by = "month"), times = 5),
values = rnorm(225)
)
head(data_long)
```
## A function to make a plot
```{r echo=TRUE}
make_chart <- function(name, data){
# filter the dataset down to metric of interest
filtered_data <- data |>
dplyr::filter(
metric == name
)
# make a plot
p <- ggplot(
filtered_data,
aes(date, values)
) +
geom_line() +
geom_point() +
labs(
title = name,
subtitle = "An important subtitle"
)
# save the plot
ggsave(paste0("img/", name, ".png"), p, units = "px", width = 600, height = 300, scale = 3)
}
```
## Map
```{r echo=TRUE}
library(ggplot2)
purrr::map(
c("Metric 1", "Metric 2", "Metric 3", "Metric 4", "Metric 5"),
make_chart,
data_long
)
```
## Charts
![](img/Metric 1.png){.absolute top=80 left=0 width="600" height="300" .fragment}
![](img/Metric 2.png){.absolute top=160 left=100 width="600" height="300" .fragment}
![](img/Metric 3.png){.absolute top=240 left=200 width="600" height="300" .fragment}
![](img/Metric 4.png){.absolute top=320 left=300 width="600" height="300" .fragment}
![](img/Metric 5.png){.absolute top=400 left=400 width="600" height="300" .fragment}
## What about SPC!?
```{.r code-line-numbers="|2,11,13,14,16,19,23,29"}
# we make minor alterations to the plotting function
make_spc <- function(name, data){
# filter the dataset down to metric of interest
filtered_data <- data |>
dplyr::filter(
metric == name
)
# make a plot using {NHSRplotthedots}
p <- NHSRplotthedots::ptd_spc(
filtered_data,
values,
date
) |>
NHSRplotthedots::ptd_create_ggplot() +
labs(
title = name,
subtitle = "A better, and more insightful subtitle"
)
# save the plot
ggsave(paste0("img/", name, "_spc.png"), p, units = "px", width = 600, height = 300, scale = 4)
}
purrr::map(
c("Metric 1", "Metric 2", "Metric 3", "Metric 4", "Metric 5"),
make_spc,
data_long
)
```
```{r include=FALSE}
make_spc <- function(name, data){
# filter the dataset down to metric of interest
filtered_data <- data |>
dplyr::filter(
metric == name
)
# make a plot using {NHSRplotthedots}
p <- NHSRplotthedots::ptd_spc(
filtered_data,
values,
date
) |>
NHSRplotthedots::ptd_create_ggplot() +
labs(
title = name,
subtitle = "A better, and more insightful subtitle"
)
# save the plot
ggsave(paste0("img/", name, "_spc.png"), p, units = "px", width = 600, height = 300, scale = 4)
}
purrr::map(
c("Metric 1", "Metric 2", "Metric 3", "Metric 4", "Metric 5"),
make_spc,
data_long
)
```
## SPC charts
![](img/Metric 1_spc.png){.absolute top=80 left=0 width="600" height="300" .fragment}
![](img/Metric 2_spc.png){.absolute top=160 left=100 width="600" height="300" .fragment}
![](img/Metric 3_spc.png){.absolute top=240 left=200 width="600" height="300" .fragment}
![](img/Metric 4_spc.png){.absolute top=320 left=300 width="600" height="300" .fragment}
![](img/Metric 5_spc.png){.absolute top=400 left=400 width="600" height="300" .fragment}
## In summary
![](img/sausage-machine.jpg){.absolute top=100 left=100 width="800"}
![](img/purrr.jpg){.absolute top=430 left=500 width="80"}
![](img/data.jpg){.absolute top=50 left=450 width="100" height="100" .fragment}
## Some things we haven't covered
:::{.nonincremental}
* Passing other arguments in
+ eg. the data in the charts example
* The shape of the output
+ Getting a vector back instead of a list
* map() vs. walk()
+ Returned values vs. side-effects
* Mapping over more than one variable
+ map2() for 2 variables
+ pmap() and a dataframe of variables for more
:::
## Further online material
::::: {.columns}
:::: {.column width="50%"}
Tom Jemmett's video
<iframe width="400" height="220" src="https://www.youtube.com/embed/GxvccD8K49M" allowfullscreen></iframe>
::: {.tiny-text}
<https://www.youtube.com/GxvccD8K49M>
:::
::::
:::: {.column width="50%"}
Hadley Wickham's video
<iframe width="400" height="220" src="https://www.youtube.com/embed/EGAs7zuRutY" allowfullscreen></iframe>
::: {.tiny-text}
<https://www.youtube.com/EGAs7zuRutY>
:::
::::
:::::
The iteration chapter of R for Data Science:
<https://r4ds.had.co.nz/iteration.html#iteration>
Docs: <https://purrr.tidyverse.org>
Cheatsheet: <https://rstudio.github.io/cheatsheets/purrr.pdf>
## Open-source
![](img/github-mark.svg){width=30} Online presentation: <https://thomuk.github.io/2023-NHSR-Conf-Presentation>
![](img/github-mark.svg){width=30} Presentation code: <https://github.com/ThomUK/2023-NHSR-Conf-Presentation>
![](img/github-mark.svg){width=30} Other repos: <https://github.com/ThomUK>
## Thank you
![](img/grass-question-mark.png){fig-align="center"}