-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-additional-material.Rmd
More file actions
150 lines (105 loc) · 3.61 KB
/
Copy path07-additional-material.Rmd
File metadata and controls
150 lines (105 loc) · 3.61 KB
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
---
output: html_document
editor_options:
chunk_output_type: console
---
class: inverse, center, middle
# Additional Material
---
# Alternatives to `{{ · }}`?
* `{{ · }}` is a shortcut for `!!enquo(·)`
* Use the `!!` and `rlang::enquo(·)` combination when you need to pass `var1` and `var2` around before it's injected.
* `!!` is a part of `{rlang}`
```{r, eval = FALSE}
my_mean <- function(data, var1, var2) {
var1_quo <- rlang::enquo(var1)
var2_quo <- rlang::enquo(var2)
dplyr::summarise(data, mean(!!var1_quo + !!var2_quo))
}
my_mean(mtcars, cyl, am)
#> # A tibble: 1 x 1
#> `mean(cyl + am)`
#> <dbl>
#> 1 6.59
```
---
# Can I avoid `{{ · }}` and `!!enquo(·)`?
* YES! (mostly)
* Immediately convert inputs into column name **strings**, and utilize all the what you learned in the first portion of this presentation.
```{r, eval = FALSE}
my_mean <- function(data, var1, var2) {
var1 <- dplyr::select(data, {{ var1 }}) |> names()
var2 <- dplyr::select(data, {{ var2 }}) |> names()
dplyr::summarise(data, mean(.data[[var1]] + .data[[var2]]))
}
my_mean(mtcars, cyl, am)
#> # A tibble: 1 x 1
#> `mean(.data[["cyl"]] + .data[["am"]])`
#> <dbl>
#> 1 6.59
```
---
# Naming new variables created within your function
* Cue, the walrus operator `:=`
* Part of the `rlang` package
* `{glue}` syntax is automatically recognized on the left-hand side of the
walrus operator
```{r, eval=FALSE}
my_naming_function <- function(variable){
gtsummary::trial %>%
dplyr::mutate("mean_{variable}" := mean(.data[[variable]], na.rm = TRUE))
}
my_naming_function(variable = "age")
# A tibble: 200 x 9
# trt age marker stage grade response death ttdeath mean_age
# <chr> <dbl> <dbl> <fct> <fct> <int> <int> <dbl> <dbl>
# 1 Drug A 23 0.16 T1 II 0 0 24 47.2
# 2 Drug B 9 1.11 T2 I 1 0 24 47.2
```
---
# rlang
* Examples for when you can't use the walrus operator and have to use
`rlang::sym()` or `rlang::syms()`
* `rlang::sym()` is used when you have a string and you want it to be unquoted
* `rlang::syms()` is used when you have a vector and you want it to be unquoted
```{r, eval=FALSE}
# e.g., a vector of c("A", "B", "C")
vec <- c("mpg", "hp")
# Error: Can't subset columns that don't exist.
# x Column `vec` doesn't exist.
mtcars %>%
dplyr::select(vec)
# now works
# similar to `all_of()` but there are times when you need the below
mtcars %>%
dplyr::select(!!!rlang::syms(vec))
```
* Can also be used to name variables within functions
---
# rlang
* Can use `rlang::expr()` to see what `rlang::sym()` is evaluating to
```{r, eval=FALSE}
vec <- c("mpg", "hp")
rlang::expr(mtcars %>%
dplyr::select(!!!rlang::syms(vec)))
# mtcars %>% dplyr::select(mpg, hp)
```
---
# rlang: unquoted expresssions
* You can also write functions that accept unquoted expressions as arguments
```r
my_filter <- function(data, condition) {
condition <- rlang::enquo(condition)
dplyr::filter(data, !!condition)
}
my_filter(gtsummary::trial, age < 30) |> head()
#> # A tibble: 6 x 8
#> trt age marker stage grade response death ttdeath
#> <chr> <dbl> <dbl> <fct> <fct> <int> <int> <dbl>
#> 1 Drug A 23 0.16 T1 II 0 0 24
#> 2 Drug B 9 1.11 T2 I 1 0 24
#> 3 Drug B 21 0.258 T4 I 0 1 12.9
#> 4 Drug B 28 0.803 T4 II 0 1 18
#> 5 Drug B 25 2.45 T1 I 1 0 24
#> 6 Drug B 25 0.531 T4 III 0 1 23.2
```