-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtidyeval_common.R
More file actions
61 lines (52 loc) · 1.53 KB
/
Copy pathtidyeval_common.R
File metadata and controls
61 lines (52 loc) · 1.53 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
# Library ----------------------------------------------------
library(tidyverse)
# Tidy evaluation ------------------------------------------
# bare to quosure: quo
bare_to_quo <- function(x, var){
x %>% select(!!var) %>% head(1)
}
bare_to_quo(mtcars, quo(cyl))
# bare to quosure in function: enquo
bare_to_quo_in_func <- function(x, var) {
var_enq <- enquo(var)
x %>% select(!!var_enq) %>% head(1)
}
bare_to_quo_in_func(mtcars, mpg)
# quosure to a name: quo_name
bare_to_name <- function(x, nm) {
nm_name <- quo_name(nm)
x %>% mutate(!!nm_name := 42) %>% head(1) %>%
select(!!nm)
}
bare_to_name(mtcars, quo(this_is_42))
# quosure to text: quo_text
quo_to_text <- function(x, var) {
var_enq <- enquo(var)
ggplot(x, aes_string(rlang::quo_text(var_enq))) + geom_density()
}
plt <- quo_to_text(mtcars, cyl)
plt
# character to name: sym
char_to_quo <- function(x, var) {
var_enq <- rlang::sym(var)
x %>% select(!!var_enq) %>% head(1)
}
char_to_quo(mtcars, "vs")
# multiple bares to quosure: quos
bare_to_quo_mult <- function(x, ...) {
grouping <- quos(...)
x %>% group_by(!!!grouping) %>% summarise(nr = n())
}
bare_to_quo_mult(mtcars, vs, cyl)
# multiple characters to names: syms
bare_to_quo_mult_chars <- function(x, ...) {
grouping <- rlang::syms(...)
x %>% group_by(!!!grouping) %>% summarise(nr = n())
}
bare_to_quo_mult_chars(mtcars, list("vs", "cyl"))
# quoting full expressions
filter_func <- function(x, filter_exp) {
filter_exp_enq <- enquo(filter_exp)
x %>% filter(!!filter_exp_enq)
}
filter_func(mtcars, hp == 93)