-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_masking.qmd
493 lines (287 loc) · 8.47 KB
/
data_masking.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
---
title: "data_masking"
---
## Resources
[https://brad-cannell.github.io/r_notes/confidence-intervals.html]
[https://rlang.r-lib.org/reference/topic-quosure.html]
[https://dplyr.tidyverse.org/articles/programming.html]
[https://rlang.r-lib.org/reference/topic-data-mask-programming.html]
[https://rlang.r-lib.org/reference/topic-multiple-columns.html]
[https://mastering-shiny.org/action-tidy.html]
[https://adv-r.hadley.nz/expressions.html]
## basics
### enquo() and !! / `{{}}`
enquo() is called quosures -- it lets R know which environment an environment should be called. It is used for varaiables that data masked
When an environment is quosured it then needs to be defused which you do with !! or xx.
Alternatively you can just use `{{}}`
For multiple arguments in ..., pass them on to functions that also take ... like group_by(), or pass them within c() for functions taking tidy selection in a single argument like pivot_longer():
```{r}
# Pass dots through
my_group_by <- function(.data, ...) {
.data %>% dplyr::group_by(...)
}
my_pivot_longer <- function(.data, ...) {
.data %>% tidyr::pivot_longer(c(...))
}
```
for enquo() var you can use quo_get_expr() and quo_get_env() to validate the expression and environment they are are declared in
#### expr and !! or eval_tidy()
expr is used to capture an unquoted argument and you defuse with !! or eval_tidy()
```{r}
library(rlang)
my_mean <- function(data, var) {
# `expr()` is sufficient, no need for `quo()`
var_quo <- enquo(var)
expr <- expr(mean({{ var }}))
quo_get_expr(var_quo)
# dplyr::summarise(data, !!expr)
}
my_mean(mtcars,vs)
```
expr() returns an expression, an object that captures the structure of the code without evaluating it (i.e. running it). If you have an expression, you can evaluate it with base::eval():
```{r}
install.packages("lobstr")
lobstr::ast(mean(c(1:10),5,na.rm=TRUE) |> min())
```
## symbol
Symbo is expression (eg captured code) that references an object (x,mtcars, mean) or a string directly covered to an expression sym()
turn a symbo back to a string with rlang::as_string() or as.character()
## Calls
A call object represents a captured function call. Call objects are a special type of list90 where the first component specifies the function to call (usually a symbol), and the remaining elements are the arguments for that call. Call objects create branches in the AST, because calls can be nested inside other calls.
### Constructing
You can construct a call object from its components using rlang::call2(). The first argument is the name of the function to call (either as a string, a symbol, or another call). The remaining arguments will be passed along to the call:
```{r}
call2("mean", x = expr(x), na.rm = TRUE)
#> mean(x = x, na.rm = TRUE)
call2(expr(base::mean), x = expr(x), na.rm = TRUE)
#> base::mean(x = x, na.rm = TRUE)
```
Using call2() to create complex expressions is a bit clunky. You’ll learn another technique in Chapter 19.
#### using this in practice
### Alternative to call2
In short, use enexpr() and enexprs() to capture the expressions supplied as arguments by the user. Use expr() and exprs() to capture expressions that you supply.
### Data masking
```{r}
base::deparse()
```
#### Enquoes
- quotes functions eg. turns mpg into "mpg" AND declares the environment eg. global or local for the quoted return to be put in.
- eg. ... or mpg needs to but into enquos(...) or enquo(mpg)
- you evaluate unquoted objects with !! or `base::deparse()`
- or combine both enquo() and !! with {}
- if you have a list of quoted objects you need !!! to unlist
#### alternative to sym
19.3.2 Capturing symbols
Sometimes you only want to allow the user to specify a variable name, not an arbitrary expression. In this case, you can use ensym() or ensyms(). These are variants of enexpr() and enexprs() that check the captured expression is either symbol or a string (which is converted to a symbol96). ensym() and ensyms() throw an error if given anything else.
## Other
- rlang::qq_show() around your evaluated expression to show
- if input is a quoted input put it around sym (enquo for unquoted)
```{r}
library(tidyverse)
my_mean <- function(data, var) {
var <- dplyr::sym(var)
rlang::qq_show(
data |> dplyr::summarise(mean(!!var))
)
}
mtcars |>
my_mean("cyl")
```
```{r}
custom <- function(.data,group,focus){
group <- enquos(group)
focus <- enquo(focus)
.data %>%
group_by(pick(!!!group)) %>%
summarize(
mean=mean(!!focus)
,.groups="drop"
)
}
diamonds %>% custom(group=c(color,cut),focus=x)
```
sym() is used to take a quoted input "x" and use it in a context without quotes. Need !!sym("x") to be used
or rlang::englue("var) within the .data[[var]] context
.data[[rlang::englue("var")]]
.env$var to refer to grid table
```{r}
library(tidyverse)
col_numbers <- 1:ncol(diamonds)
map(
.x=col_numbers
,.f=~diamonds[.x:10]
)
split(diamonds, ~ cut) %>%
map(
.x=.
,~.x %>% pull(price) %>% sum) %>%
stack()
map(
.x=col_numbers
,.f = ~mtcars[.x] %>% table()
)
```
```{r}
diamonds %>% select(cut,color) %>%
table()
```
```{r}
library(tidyverse)
cols <- c("mpg","cyl")
df <- expand.grid(cols) %>%
mutate(data=list(mtcars))
df
global_env_unquoted <- function(data,col){
data %>%
filter(eval({{col}}))
}
df %>%
rowwise() %>%
mutate(args=list(rlang::parse_expr(paste0(Var1,">18")))) %>%
mutate(
fun=list(global_env_unquoted(data,args))
)
```
## Select
```{r}
library(tidyverse)
basic <- function(){
mtcars %>%
select(mpg)
}
basic()
direct_quoted <- function(){
name <- "mpg"
mtcars %>%
select(name)
}
direct_quoted()
input_quoted <- function(name){
mtcars %>%
select(name)
}
input_quoted("mpg")
input_quoted_2 <- function(name){
mtcars %>%
select(any_of(name))
}
input_quoted_2("mpg")
input_unquoted <- function(name){
mtcars %>%
select({{name}})
}
input_unquoted(mpg)
derived_calculad <- function(name){
mtcars2 <- mtcars %>%
mutate("{{name}}_2":={{name}})
mtcars2 %>%
select(any_of(rlang::englue("{{name}}_2")))
}
derived_calculad(mpg)
quoted_in_body <- function(){
name <- paste0("m","p","g")
mtcars %>%
select(name)
}
quoted_in_body()
select_col_names <- function(name){
mtcars %>%
select(contains(name))
}
select_col_names("m")
numeric_args <- function(args){
args_input <- rlang::parse_expr(args)
mtcars %>% select(eval(args_input))
}
numeric_args("where(~sum(.x)<180)")
```
```{r}
mtcars %>%
select(where(~sum(.x)<180))
```
## unquoted in table
```{r}
library(tidyverse)
test <- function(data,col_names){
select(data,any_of(col_names))
}
tibble(
col_names=c("mpg","hp")
,data=list(mtcars)
) %>%
rowwise() %>%
mutate(
fun=list(function(data,col_names) select(data,any_of(col_names)))
,defined_fun=list(test(data,col_names))
) %>% unnest(defined_fun)
tibble(
col_names=list("mpg/hp"=c("mpg","hp"),"mpg"=c("mpg"))
,data=list(mtcars)
,names=names(col_names)
) %>%
rowwise() %>%
mutate(
defined_fun=list(test(data,col_names))
) %>% unnest(defined_fun)
```
### Mutate
```{r}
direct <- function(){
mtcars %>%
mutate(mtcars2=mtcars*2)
}
direct()
```
## quoted as input directly to function header
```{r}
quoted_input <- function(input_name,input_col){
input_col <- sym(input_col)
mtcars %>%
mutate(
"{input_name}":=!!input_col*2
)
}
quoted_input("test","mpg")
```
## Unquoted input
```{r}
unquoted_input <- function(name,input_col){
mtcars %>%
mutate(
"{{name}}":={{input_col}}
) %>%
relocate(last_col())
}
unquoted_input(name=test,input_col=mpg)
```
## Dirived as quote in body
```{r}
derived_from_body <- function(){
obj <- "mpg2"
obj2 <- sym("mpg")
mtcars %>%
mutate("{obj}":=!!obj2)
}
derived_from_body()
is(ymd("2022-01-01"),"Date")
```
```{r}
yr_mo <- function(.data){
fpaR::contoso_fact_sales %>%
mutate(
DateKey=mdy(DateKey)
,yr_mo_report_date=format.Date(DateKey,"%y%m")
) %>%
relocate(last_col())
}
yr_mo()
```
## why does this fail?
```{r}
lm3a <- function(formula, data) {
formula <- enexpr(formula)
lm_call <- expr(lm(!!formula, data = data))
eval(expr = lm_call,)
}
lm3a(mpg ~ disp+drat+hp*vs, data=mtcars)
```