Debian Codegraph @

inst/doc/tidyselect.R

raw
1 ## ----include = FALSE----------------------------------------------------------
2 knitr::opts_chunk$set(
3 collapse = TRUE,
4 comment = "#>"
5 )
6
7 ## ----setup--------------------------------------------------------------------
8 library(tidyselect)
9 library(magrittr)
10
11 ## ----include = FALSE----------------------------------------------------------
12 # For better printing
13 mtcars <- tibble::as_tibble(mtcars)
14 iris <- tibble::as_tibble(iris)
15
16 options(
17 tibble.print_min = 4,
18 tibble.print_max = 4
19 )
20
21 ## ----eval = FALSE-------------------------------------------------------------
22 # mtcars %>% dplyr::select(mpg, cyl)
23
24 ## ----eval = FALSE-------------------------------------------------------------
25 # mtcars %>% pivot_longer(c(mpg, cyl))
26 # mtcars %>% pivot_longer(mpg | cyl)
27
28 ## ----eval = FALSE-------------------------------------------------------------
29 # # Passing dots
30 # toupper_dots <- function(data, ...) {
31 # sel <- dplyr::select(data, ...)
32 # rlang::set_names(sel, toupper)
33 # }
34 # # Interpolating a named argument with {{ }}
35 # toupper_arg <- function(data, arg) {
36 # sel <- dplyr::select(data, {{ arg }})
37 # rlang::set_names(sel, toupper)
38 # }
39 #
40 # mtcars %>% toupper_dots(mpg:disp, vs)
41 # #> # A tibble: 32 x 4
42 # #> MPG CYL DISP VS
43 # #> <dbl> <dbl> <dbl> <dbl>
44 # #> 1 21 6 160 0
45 # #> 2 21 6 160 0
46 # #> 3 22.8 4 108 1
47 # #> 4 21.4 6 258 1
48 # #> # … with 28 more rows
49 #
50 # mtcars %>% toupper_arg(c(mpg:disp, vs))
51 # #> # A tibble: 32 x 4
52 # #> MPG CYL DISP VS
53 # #> <dbl> <dbl> <dbl> <dbl>
54 # #> 1 21 6 160 0
55 # #> 2 21 6 160 0
56 # #> 3 22.8 4 108 1
57 # #> 4 21.4 6 258 1
58 # #> # … with 28 more rows
59
60 ## -----------------------------------------------------------------------------
61 own <- rlang::expr(1 + 2)
62 own
63
64 ## -----------------------------------------------------------------------------
65 fn <- function(arg) {
66 expr <- rlang::enquo(arg)
67 expr
68 }
69 user <- fn(1 + 2)
70 user
71
72 ## -----------------------------------------------------------------------------
73 rlang::eval_tidy(own)
74
75 rlang::eval_tidy(user)
76
77 ## -----------------------------------------------------------------------------
78 with_data <- function(data, x) {
79 expr <- rlang::enquo(x)
80 rlang::eval_tidy(expr, data = data)
81 }
82
83 ## ----error = TRUE-------------------------------------------------------------
84 NULL %>% with_data(mean(cyl) * 10)
85
86 mtcars %>% with_data(mean(cyl) * 10)
87
88 ## -----------------------------------------------------------------------------
89 eval_select(rlang::expr(mpg), mtcars)
90
91 eval_select(rlang::expr(c(mpg:disp, vs)), mtcars)
92
93 ## -----------------------------------------------------------------------------
94 eval_select(rlang::expr(c(foo = mpg, bar = disp)), mtcars)
95
96 eval_rename(rlang::expr(c(foo = mpg, bar = disp)), mtcars)
97
98 ## -----------------------------------------------------------------------------
99 select <- function(.data, ...) {
100 expr <- rlang::expr(c(...))
101 pos <- eval_select(expr, data = .data)
102 rlang::set_names(.data[pos], names(pos))
103 }
104
105 mtcars %>%
106 select(mpg, cyl)
107
108 ## -----------------------------------------------------------------------------
109 select <- function(.data, cols) {
110 expr <- rlang::enquo(cols)
111 pos <- eval_select(expr, data = .data)
112 rlang::set_names(.data[pos], names(pos))
113 }
114
115 mtcars %>%
116 select(c(mpg, cyl))
117
118 ## -----------------------------------------------------------------------------
119 eval_select(rlang::expr(c(foo = mpg)), mtcars)
120
121 eval_rename(rlang::expr(c(foo = mpg)), mtcars)
122
123 ## ----error = TRUE-------------------------------------------------------------
124 eval_rename(rlang::expr(mpg), mtcars)
125
126 eval_rename(rlang::expr(c(mpg)), mtcars)
127
128 eval_rename(rlang::expr(c(foo = mpg)), mtcars)
129
130 ## -----------------------------------------------------------------------------
131 wrapper <- function(data, ...) {
132 eval_rename(rlang::expr(c(...)), data)
133 }
134
135 mtcars %>% wrapper(foo = mpg, bar = hp:wt)
136
137 ## -----------------------------------------------------------------------------
138 rename <- function(.data, ...) {
139 pos <- eval_rename(rlang::expr(c(...)), .data)
140 names(.data)[pos] <- names(pos)
141 .data
142 }
143
144 mtcars %>%
145 rename(foo = mpg, bar = hp:wt)
146
147 ## -----------------------------------------------------------------------------
148 x <- rlang::expr(print(peek_vars()))
149
150 invisible(eval_select(x, data = mtcars))
151
152 ## -----------------------------------------------------------------------------
153 my_selector <- function(prefix, suffix) {
154 intersect(
155 starts_with(prefix),
156 ends_with(suffix)
157 )
158 }
159
160 iris %>% select(my_selector("Sepal", "Length"))
161
162 ## -----------------------------------------------------------------------------
163 if_width <- function(n, vars = peek_vars(fn = "if_width")) {
164 vars[nchar(vars) == n]
165 }
166
167 mtcars %>% select(if_width(2))
168
169 ## ----error = TRUE-------------------------------------------------------------
170 mtcars[if_width(2)]
171
172 ## -----------------------------------------------------------------------------
173 if_width(2, vars = names(mtcars))
174
175 ## -----------------------------------------------------------------------------
176 dups <- vctrs::new_data_frame(list(foo = 1, quux = 2, foo = 3))
177
178 dups %>% select(if_width(3))
179
180 ## -----------------------------------------------------------------------------
181 if_width <- function(n, vars = peek_vars(fn = "if_width")) {
182 which(nchar(vars) == n)
183 }
184
185 ## ----error = TRUE-------------------------------------------------------------
186 dups %>% select(if_width(3))
187
188 ## -----------------------------------------------------------------------------
189 as.list(dups) %>% select(if_width(3))
190