Skip to content

Commit 8f2fcb0

Browse files
authored
Fix across() in combination with summarise(.by) (tidyverse#1494)
1 parent f3baed5 commit 8f2fcb0

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# dbplyr (development version)
22

3+
* `across(everything())` doesn't select grouping columns created via `.by` in
4+
`summarise()` (@mgirlich, #1493).
5+
36
# dbplyr 2.5.0
47

58
## Improved tools for qualified table names

R/verb-summarise.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#' show_query()
3636
summarise.tbl_lazy <- function(.data, ..., .by = NULL, .groups = NULL) {
3737
check_groups(.groups)
38-
dots <- summarise_eval_dots(.data, ...)
3938

4039
by <- compute_by(
4140
{{ .by }},
@@ -49,6 +48,8 @@ summarise.tbl_lazy <- function(.data, ..., .by = NULL, .groups = NULL) {
4948
.data$lazy_query$group_vars <- by$names
5049
.groups <- "drop"
5150
}
51+
52+
dots <- summarise_eval_dots(.data, ...)
5253
.data$lazy_query <- add_summarise(
5354
.data, dots,
5455
.groups = .groups,

tests/testthat/_snaps/tbl-sql.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@
2828
# check_from is deprecated
2929

3030
Code
31-
tbl(con, "x", check_from = FALSE)
31+
out <- tbl(con, "x", check_from = FALSE)
3232
Condition
3333
Warning:
3434
The `check_from` argument of `tbl_sql()` is deprecated as of dbplyr 2.5.0.
35-
Output
36-
# Source: table<`x`> [0 x 1]
37-
# Database: sqlite 3.45.0 [:memory:]
38-
# i 1 variable: y <lgl>
3935

tests/testthat/_snaps/verb-summarise.md

+10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@
102102
FROM `df`
103103
GROUP BY `g`
104104

105+
# across doesn't select columns from `.by` #1493
106+
107+
Code
108+
out
109+
Output
110+
<SQL>
111+
SELECT `g`, SUM(`..x`) AS `x`
112+
FROM `df`
113+
GROUP BY `g`
114+
105115
# can't use `.by` with `.groups`
106116

107117
Code

tests/testthat/test-tbl-sql.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test_that("check_from is deprecated", {
6565
con <- local_sqlite_connection()
6666
DBI::dbExecute(con, "CREATE TABLE x (y)")
6767

68-
expect_snapshot(tbl(con, "x", check_from = FALSE))
68+
expect_snapshot(out <- tbl(con, "x", check_from = FALSE))
6969
})
7070

7171
# n_groups ----------------------------------------------------------------

tests/testthat/test-verb-summarise.R

+13
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ test_that("can group transiently using `.by`", {
107107
expect_equal(group_vars(out), character())
108108
})
109109

110+
test_that("across doesn't select columns from `.by` #1493", {
111+
lf <- lazy_frame(g = 1, x = 1)
112+
113+
out <- lf %>%
114+
summarise(
115+
across(everything(), ~ sum(..x, na.rm = TRUE)),
116+
.by = g
117+
)
118+
119+
expect_snapshot(out)
120+
expect_equal(sql_build(out)$select[1], "`g`")
121+
})
122+
110123
test_that("can't use `.by` with `.groups`", {
111124
df <- lazy_frame(x = 1)
112125

0 commit comments

Comments
 (0)