Debian Codegraph @

inst/doc/syntax.R

raw
1 ## ----include = FALSE----------------------------------------------------------
2 knitr::opts_chunk$set(
3 collapse = TRUE,
4 comment = "#>"
5 )
6 options(
7 tibble.print_min = 4,
8 tibble.print_max = 4
9 )
10 options(
11 crayon.enabled = FALSE
12 )
13
14 ## ----setup--------------------------------------------------------------------
15 library(tidyselect)
16 library(magrittr)
17
18 # For better printing
19 mtcars <- tibble::as_tibble(mtcars)
20 iris <- tibble::as_tibble(iris)
21
22 ## -----------------------------------------------------------------------------
23 select_loc <- function(data, ...) {
24 eval_select(rlang::expr(c(...)), data)
25 }
26
27 rename_loc <- function(data, ...) {
28 eval_rename(rlang::expr(c(...)), data)
29 }
30
31 ## -----------------------------------------------------------------------------
32 mtcars %>% select_loc(mpg:hp, !cyl, vs)
33
34 mtcars %>% select_loc(1:4, !2, 8)
35
36 ## -----------------------------------------------------------------------------
37 mtcars %>% select_loc(2:4)
38
39 ## -----------------------------------------------------------------------------
40 mtcars %>% select_loc(cyl:hp)
41
42 ## -----------------------------------------------------------------------------
43 iris %>% select_loc(starts_with("Sepal") | ends_with("Width"))
44
45 ## -----------------------------------------------------------------------------
46 iris %>% select_loc(starts_with("Sepal") & ends_with("Width"))
47
48 ## -----------------------------------------------------------------------------
49 iris %>% select_loc(!ends_with("Width"))
50
51 ## -----------------------------------------------------------------------------
52 iris %>% select_loc(starts_with("Sepal") & !ends_with("Width"))
53
54 ## -----------------------------------------------------------------------------
55 mtcars %>% select_loc(mpg, disp:hp)
56
57 mtcars %>% select_loc(c(mpg, disp:hp))
58
59 ## -----------------------------------------------------------------------------
60 iris %>% select_loc(starts_with("Sepal"), ends_with("Width"), Species)
61
62 iris %>% select_loc(starts_with("Sepal") | ends_with("Width") | Species)
63
64 ## -----------------------------------------------------------------------------
65 mtcars %>% select_loc(foo = c(bar = mpg, baz = cyl))
66
67 ## -----------------------------------------------------------------------------
68 mtcars %>% select_loc(foo = c(mpg, cyl))
69
70 ## -----------------------------------------------------------------------------
71 as.list(mtcars) %>% select_loc(foo = c(mpg, cyl))
72
73 ## -----------------------------------------------------------------------------
74 mtcars %>% select_loc(foo = c(bar = c(mpg, cyl)))
75
76 ## -----------------------------------------------------------------------------
77 iris %>% select_loc(!Species, foo = Sepal.Width)
78
79 ## -----------------------------------------------------------------------------
80 iris %>% select_loc(where(is.numeric))
81
82 iris %>% select_loc(where(is.factor))
83
84 iris %>% select_loc(where(is.numeric) | where(is.factor))
85
86 iris %>% select_loc(where(is.numeric) & where(is.factor))
87
88 ## -----------------------------------------------------------------------------
89 iris %>% select_loc(force(c(1, 3)))
90
91 ## -----------------------------------------------------------------------------
92 iris %>% select_loc(force(c("Sepal.Length", "Petal.Length")))
93
94 ## -----------------------------------------------------------------------------
95 iris %>% select_loc(force(is.numeric))
96
97 ## -----------------------------------------------------------------------------
98 mask <- function(data, expr) {
99 rlang::eval_tidy(rlang::enquo(expr), data)
100 }
101
102 foo <- 10
103 cyl <- 200
104
105 # `cyl` represents the data frame column here:
106 mtcars %>% mask(cyl * foo)
107
108 ## -----------------------------------------------------------------------------
109 mtcars %>% mask(!!cyl * foo)
110 mtcars %>% mask(.env$cyl * foo)
111
112 ## ----error = TRUE-------------------------------------------------------------
113 cyl_pos <- 2
114 mtcars %>% select_loc(mpg | cyl_pos)
115
116 ## ----error = TRUE-------------------------------------------------------------
117 mtcars %>% select_loc(all_of(mpg))
118
119 ## -----------------------------------------------------------------------------
120 x <- data.frame(x = 1:3, y = 4:6, z = 7:9)
121
122 # `ncol(x)` is an env-expression, so `x` represents the data frame in
123 # the environment rather than the column in the data frame
124 x %>% select_loc(2:ncol(x))
125
126 ## -----------------------------------------------------------------------------
127 y <- c("y", "z")
128 x %>% select_loc(all_of(y))
129
130 ## -----------------------------------------------------------------------------
131 mtcars %>% select_loc(cyl_pos)
132
133 ## ----error = TRUE-------------------------------------------------------------
134 mtcars %>% select_loc(cyl^2)
135
136 mtcars %>% select_loc(mpg * wt)
137
138 ## ----error = TRUE-------------------------------------------------------------
139 mtcars %>% select_loc(mpg)
140
141 mtcars %>% rename_loc(mpg)
142
143 ## ----error = TRUE-------------------------------------------------------------
144 # Lists can have duplicates
145 as.list(mtcars) %>% select_loc(foo = mpg, foo = cyl)
146
147 # Data frames cannot
148 mtcars %>% select_loc(foo = mpg, foo = cyl)
149
150 ## ----error = TRUE-------------------------------------------------------------
151 mtcars %>% select_loc(cyl, cyl = mpg)
152
153 mtcars %>% select_loc(disp, cyl = mpg)
154
155 ## ----error = TRUE-------------------------------------------------------------
156 mtcars %>% rename_loc(cyl, cyl = mpg)
157
158 mtcars %>% rename_loc(disp, cyl = mpg)
159
160 ## -----------------------------------------------------------------------------
161 mtcars %>% select_loc(foo = cyl, cyl = mpg)
162
163 mtcars %>% rename_loc(foo = cyl, cyl = mpg)
164
165 ## -----------------------------------------------------------------------------
166 dups <- vctrs::new_data_frame(list(x = 1, y = 2, x = 3))
167
168 ## -----------------------------------------------------------------------------
169 dups %>% select_loc(y)
170
171 ## ----error = TRUE-------------------------------------------------------------
172 dups %>% select_loc(x)
173
174 ## -----------------------------------------------------------------------------
175 dups %>% select_loc(x, foo = 3)
176
177 dups %>% rename_loc(foo = 3)
178
179 ## -----------------------------------------------------------------------------
180 mtcars %>% subset(select = c(cyl, hp:wt))
181