File: tidyeval-across.md

package info (click to toggle)
r-cran-dbplyr 2.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,376 kB
  • sloc: sh: 13; makefile: 2
file content (295 lines) | stat: -rw-r--r-- 7,004 bytes parent folder | download
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
# across() captures anonymous functions

    Code
      (expect_error(capture_across(lf, across(a, function(x) {
        x <- x + 2
        log(x)
      }))))
    Output
      <error/rlang_error>
      Error in `across()`:
      ! Cannot translate functions consisting of more than one statement.

# across() gives informative errors

    Code
      capture_across(lf, across(a, 1))
    Condition
      Error in `across()`:
      ! `.fns` must be a function, a formula, or list of functions/formulas.
    Code
      capture_across(lf, across(a, list(1)))
    Condition
      Error in `across()`:
      ! `.fns` must contain a function or a formula.
      x Problem with 1
    Code
      capture_across(lf, across(a:b, "log"))
    Condition
      Error in `across()`:
      ! `.fns` must be a function, a formula, or list of functions/formulas.
    Code
      capture_across(lf, across(c, mean))
    Condition
      Error in `across()`:
      ! Can't subset columns that don't exist.
      x Column `c` doesn't exist.

# across() defaults to everything()

    Code
      lazy_frame(x = 1, y = 1) %>% summarise(across(.fns = ~ . + 1))
    Output
      <SQL>
      SELECT `x` + 1.0 AS `x`, `y` + 1.0 AS `y`
      FROM `df`

# untranslatable functions are preserved

    Code
      lf %>% summarise(across(a:b, SQL_LOG))
    Output
      <SQL>
      SELECT SQL_LOG(`a`) AS `a`, SQL_LOG(`b`) AS `b`
      FROM `df`

# old _at functions continue to work

    Code
      lf %>% dplyr::summarise_at(dplyr::vars(a:b), "sum")
    Condition
      Warning:
      Missing values are always removed in SQL aggregation functions.
      Use `na.rm = TRUE` to silence this warning
      This warning is displayed once every 8 hours.
    Output
      <SQL>
      SELECT SUM(`a`) AS `a`, SUM(`b`) AS `b`
      FROM `df`

---

    Code
      lf %>% dplyr::summarise_at(dplyr::vars(a:b), sum)
    Output
      <SQL>
      SELECT SUM(`a`) AS `a`, SUM(`b`) AS `b`
      FROM `df`

---

    Code
      lf %>% dplyr::summarise_at(dplyr::vars(a:b), ~ sum(.))
    Output
      <SQL>
      SELECT SUM(`a`) AS `a`, SUM(`b`) AS `b`
      FROM `df`

# across() errors if named

    Code
      (expect_error(mutate(lf, x = across())))
    Output
      <error/rlang_error>
      Error in `mutate()`:
      ! In dbplyr, the result of `across()` must be unnamed.
      i `x = across()` is named.
    Code
      (expect_error(group_by(lf, x = across())))
    Output
      <error/rlang_error>
      Error in `group_by()`:
      ! In dbplyr, the result of `across()` must be unnamed.
      i `x = across()` is named.

# across() throws error if unpack = TRUE

    Code
      (expect_error(lf %>% mutate(across(x, .unpack = TRUE))))
    Output
      <error/rlang_error>
      Error in `mutate()`:
      i In argument: `across(x, .unpack = TRUE)`
      Caused by error in `mutate()`:
      ! `.unpack = TRUE` is not supported in SQL translations.

# if_all() gives informative errors

    Code
      capture_if_all(lf, if_all(a, 1))
    Condition
      Error in `if_all()`:
      ! `.fns` must be a function, a formula, or list of functions/formulas.
    Code
      capture_if_all(lf, if_all(a, list(1)))
    Condition
      Error in `if_all()`:
      ! `.fns` must contain a function or a formula.
      x Problem with 1

# if_all/any works in filter()

    Code
      lf %>% filter(if_all(a:b, ~ . > 0))
    Output
      <SQL>
      SELECT *
      FROM `df`
      WHERE (`a` > 0.0 AND `b` > 0.0)

---

    Code
      lf %>% filter(if_any(a:b, ~ . > 0))
    Output
      <SQL>
      SELECT *
      FROM `df`
      WHERE (`a` > 0.0 OR `b` > 0.0)

# if_all/any works in mutate()

    Code
      lf %>% mutate(c = if_all(a:b, ~ . > 0))
    Output
      <SQL>
      SELECT *, `a` > 0.0 AND `b` > 0.0 AS `c`
      FROM `df`

---

    Code
      lf %>% mutate(c = if_any(a:b, ~ . > 0))
    Output
      <SQL>
      SELECT *, `a` > 0.0 OR `b` > 0.0 AS `c`
      FROM `df`

# if_all/any uses every colum as default

    Code
      lf %>% filter(if_all(.fns = ~ . > 0))
    Output
      <SQL>
      SELECT *
      FROM `df`
      WHERE (`a` > 0.0 AND `b` > 0.0)

---

    Code
      lf %>% filter(if_any(.fns = ~ . > 0))
    Output
      <SQL>
      SELECT *
      FROM `df`
      WHERE (`a` > 0.0 OR `b` > 0.0)

# if_all/any works without `.fns` argument

    Code
      lf %>% filter(if_all(a:b))
    Output
      <SQL>
      SELECT *
      FROM `df`
      WHERE (`a` AND `b`)

---

    Code
      lf %>% filter(if_any(a:b))
    Output
      <SQL>
      SELECT *
      FROM `df`
      WHERE (`a` OR `b`)

# if_all() cannot rename variables

    Code
      (expect_error(capture_if_all(lf, if_all(c(a = x, b = y)))))
    Output
      <error/tidyselect:::error_disallowed_rename>
      Error in `if_all()`:
      ! Can't rename variables in this context.

# across(...) is deprecated

    Code
      summarise(lf, across(everything(), mean, na.rm = TRUE))
    Condition
      Warning:
      The `...` argument of `across()` is deprecated as of dbplyr 2.3.0.
      i Supply arguments directly to `.fns` through a lambda instead.
      
      # Previously across(a:b, mean, na.rm = TRUE)
      
      # Now across(a:b, ~mean(.x, na.rm = TRUE))
    Output
      <SQL>
      SELECT AVG(`x`) AS `x`
      FROM `df`

# across() does not support formulas with dots

    Code
      (expect_error(capture_across(lf, across(a:b, ~ log(.x, base = .y), base = 2))))
    Output
      <error/rlang_error>
      Error in `across()`:
      ! Can't use `...` when a purrr-style lambda is used in `.fns`.
      i Use a lambda instead.
      i Or inline them via a purrr-style lambda.
    Code
      (expect_error(capture_across(lf, across(a:b, list(~ log(.x, base = .y)), base = 2)))
      )
    Output
      <error/rlang_error>
      Error in `across()`:
      ! Can't use `...` when a purrr-style lambda is used in `.fns`.
      i Use a lambda instead.
      i Or inline them via a purrr-style lambda.

# `pick()` errors in `arrange()` are useful

    Code
      arrange(df, pick(y))
    Condition
      Error in `arrange()`:
      i In argument: `pick(y)`
      Caused by error in `pick()`:
      ! Can't subset columns that don't exist.
      x Column `y` doesn't exist.

# doesn't allow renaming

    Code
      arrange(lazy_frame(x = 1), pick(y = x))
    Condition
      Error in `arrange()`:
      i In argument: `pick(y = x)`
      Caused by error in `pick()`:
      ! Can't rename variables in this context.

# requires at least one input

    Code
      arrange(lazy_frame(x = 1), pick())
    Condition
      Error in `arrange()`:
      i In argument: `pick()`
      Caused by error in `partial_eval_pick()`:
      ! Must supply at least one input to `pick()`.

# `filter()` with `pick()` that uses invalid tidy-selection errors

    Code
      filter(df, pick(x, a))
    Condition
      Error in `filter()`:
      i In argument: `pick(x, a)`
      Caused by error in `pick()`:
      ! Can't subset columns that don't exist.
      x Column `a` doesn't exist.