Skip to content

Commit

Permalink
Preserve list-cols in rowwise-across()
Browse files Browse the repository at this point in the history
Fixes #6264
  • Loading branch information
hadley committed Jul 30, 2022
1 parent 864fdc3 commit f65350f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dplyr (development version)

* `across()` used without functions inside an rowwise-data frame no longer
generates an invalid data frame (#6264).

* `arrange()` now correctly ignores `NULL` inputs (#6193).

* `*_join()` now error if you supply them with additional arguments that
Expand Down
6 changes: 4 additions & 2 deletions R/across.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ across <- function(.cols = everything(), .fns = NULL, ..., .names = NULL) {
names <- setup$names

mask <- peek_mask()
data <- mask$current_cols(vars)

if (is.null(fns)) {
cols <- mask$current_cols(vars, restore_rowwise = TRUE)
nrow <- length(mask$current_rows())
data <- new_data_frame(data, n = nrow, class = c("tbl_df", "tbl"))
data <- new_data_frame(cols, n = nrow, class = c("tbl_df", "tbl"))

if (is.null(names)) {
return(data)
Expand All @@ -158,6 +158,8 @@ across <- function(.cols = everything(), .fns = NULL, ..., .names = NULL) {
}
}

data <- mask$current_cols(vars)

n_cols <- length(data)
n_fns <- length(fns)

Expand Down
17 changes: 9 additions & 8 deletions R/data-mask.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,22 @@ DataMask <- R6Class("DataMask",
},

pick = function(vars) {
cols <- self$current_cols(vars)
if (inherits(private$data, "rowwise_df")) {
cols <- self$current_cols(vars, restore_rowwise = TRUE)
nrow <- length(self$current_rows())
new_tibble(cols, nrow = nrow)
},

current_cols = function(vars, restore_rowwise = FALSE) {
cols <- env_get_list(parent.env(private$mask), vars)
if (restore_rowwise && inherits(private$data, "rowwise_df")) {
cols <- map2(cols, names(cols), function(col, name) {
if (vec_is_list(private$current_data[[name]])) {
col <- list(col)
}
col
})
}
nrow <- length(self$current_rows())
new_tibble(cols, nrow = nrow)
},

current_cols = function(vars) {
env_get_list(parent.env(private$mask), vars)
cols
},

current_rows = function() {
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-across.R
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,20 @@ test_that("expand_if_across() expands lambdas", {
)
})

test_that("rowwise() preserves list-cols iff no fns (#5951, #6264)", {
rf <- rowwise(tibble(x = list(1:2, 3:5)))

# Need to unchop so works like mutate(x = length(x))
out <- mutate(rf, across(everything(), length))
expect_equal(out$x, c(2, 3))

# Need to preserve to create valid data frame
out <- mutate(rf, across = list(across(everything())))
expect_equal(out$across, list(
tibble(x = list(1:2)),
tibble(x = list(3:5))
))
})

# c_across ----------------------------------------------------------------

Expand Down
8 changes: 0 additions & 8 deletions tests/testthat/test-mutate.r
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,6 @@ test_that("mutate() propagates caller env", {
expect_caller_env(mutate(mtcars, sig_caller_env()))
})

test_that("rowwise() + mutate(across()) correctly handles list columns (#5951)", {
tib <- tibble(a=list(1:2,3:4),c=list(NULL,NULL)) %>% rowwise()
expect_identical(
mutate(tib, sum = across(everything(),sum)),
mutate(tib, sum = across(where(is.list),sum))
)
})

test_that("mutate() fails on named empty arguments (#5925)", {
expect_error(
mutate(tibble(), bogus = )
Expand Down

0 comments on commit f65350f

Please sign in to comment.