Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

across() not operating sequentially #5773

Merged
merged 3 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

* Using testthat 3rd edition.

* Fixed bug introduced in `across()` in previous version (#5765).
* Fixed bugs introduced in `across()` in previous version (#5765).

* `group_by()` keeps attributes unrelated to the grouping (#5760).

Expand Down
29 changes: 24 additions & 5 deletions R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ mutate_cols <- function(.data, ...) {
for (i in seq_along(dots)) {
mask$across_cache_reset()

# get results from all the quosures that are expanded from ..i
# then ingest them after
quosures <- expand_quosure(dots[[i]])
quosures_results <- vector(mode = "list", length = length(quosures))

for (k in seq_along(quosures)) {
quo <- quosures[[k]]
Expand Down Expand Up @@ -283,11 +286,6 @@ mutate_cols <- function(.data, ...) {
}

if (is.null(chunks)) {
if (quo_data$is_named) {
name <- quo_data$name_given
new_columns[[name]] <- zap()
mask$remove(name)
}
next
}

Expand All @@ -305,6 +303,27 @@ mutate_cols <- function(.data, ...) {
}
}

quosures_results[[k]] <- list(result = result, chunks = chunks)
}


for (k in seq_along(quosures)) {
quo <- quosures[[k]]
quo_data <- attr(quo, "dplyr:::data")

quo_result <- quosures_results[[k]]
if (is.null(quo_result)) {
if (quo_data$is_named) {
name <- quo_data$name_given
new_columns[[name]] <- zap()
mask$remove(name)
}
next
}

result <- quo_result$result
chunks <- quo_result$chunks

if (!quo_data$is_named && is.data.frame(result)) {
new_columns[names(result)] <- result
mask$add_many(result, chunks)
Expand Down
15 changes: 15 additions & 0 deletions R/summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ summarise_cols <- function(.data, ...) {
mask$across_cache_reset()

quosures <- expand_quosure(dots[[i]])
quosures_results <- vector(mode = "list", length = length(quosures))

# with the previous part above, for each element of ... we can
# have either one or several quosures, each of them handled here:
Expand All @@ -246,6 +247,20 @@ summarise_cols <- function(.data, ...) {
)
chunks_k <- vec_cast_common(!!!chunks_k, .to = types_k)

quosures_results[[k]] <- list(chunks = chunks_k, types = types_k)
}

for (k in seq_along(quosures)) {
quo <- quosures[[k]]
quo_data <- attr(quo, "dplyr:::data")

quo_result <- quosures_results[[k]]
if (is.null(quo_result)) {
next
}
types_k <- quo_result$types
chunks_k <- quo_result$chunks

if (!quo_data$is_named && is.data.frame(types_k)) {
chunks_extracted <- .Call(dplyr_extract_chunks, chunks_k, types_k)

Expand Down
72 changes: 66 additions & 6 deletions tests/testthat/test-across.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,28 +254,88 @@ test_that("across() works with empty data frames (#5523)", {
)
})

test_that("lambdas in across() can use columns", {
test_that("lambdas in mutate() + across() can use columns", {
df <- tibble(x = 2, y = 4, z = 8)
expect_identical(
df %>% mutate_all(~ .x / y),
df %>% mutate(data.frame(x = x / y, y = y / y, z = z / y)),
df %>% mutate(across(everything(), ~ .x / y))
)
expect_identical(
df %>% mutate_all(~ .x / y),
df %>% mutate(data.frame(x = x / y, y = y / y, z = z / y)),
df %>% mutate(+across(everything(), ~ .x / y))
)

expect_identical(
df %>% mutate(data.frame(x = x / y, y = y / y, z = z / y)),
df %>% mutate(across(everything(), ~ .x / .data$y))
)
expect_identical(
df %>% mutate(data.frame(x = x / y, y = y / y, z = z / y)),
df %>% mutate(+across(everything(), ~ .x / .data$y))
)
})

test_that("lambdas in summarise() + across() can use columns", {
df <- tibble(x = 2, y = 4, z = 8)
expect_identical(
df %>% summarise(data.frame(x = x / y, y = y / y, z = z / y)),
df %>% summarise(across(everything(), ~ .x / y))
)
expect_identical(
df %>% summarise(data.frame(x = x / y, y = y / y, z = z / y)),
df %>% summarise(+across(everything(), ~ .x / y))
)

expect_identical(
df %>% summarise(data.frame(x = x / y, y = y / y, z = z / y)),
df %>% summarise(across(everything(), ~ .x / .data$y))
)
expect_identical(
df %>% summarise(data.frame(x = x / y, y = y / y, z = z / y)),
df %>% summarise(+across(everything(), ~ .x / .data$y))
)
})

test_that("lambdas in across() can use columns in follow up expressions (#5717)", {
test_that("lambdas in mutate() + across() can use columns in follow up expressions (#5717)", {
df <- tibble(x = 2, y = 4, z = 8)
expect_identical(
df %>% mutate(a = 2, x = x / y, y = y / y, z = z / y),
df %>% mutate(a = 2, data.frame(x = x / y, y = y / y, z = z / y)),
df %>% mutate(a = 2, across(c(x, y, z), ~ .x / y))
)
expect_identical(
df %>% mutate(a = 2, x = x / y, y = y / y, z = z / y),
df %>% mutate(a = 2, data.frame(x = x / y, y = y / y, z = z / y)),
df %>% mutate(a = 2, +across(c(x, y, z), ~ .x / y))
)

expect_identical(
df %>% mutate(a = 2, data.frame(x = x / y, y = y / y, z = z / y)),
df %>% mutate(a = 2, across(c(x, y, z), ~ .x / .data$y))
)
expect_identical(
df %>% mutate(a = 2, data.frame(x = x / y, y = y / y, z = z / y)),
df %>% mutate(a = 2, +across(c(x, y, z), ~ .x / .data$y))
)
})

test_that("lambdas in summarise() + across() can use columns in follow up expressions (#5717)", {
df <- tibble(x = 2, y = 4, z = 8)
expect_identical(
df %>% summarise(a = 2, data.frame(x = x / y, y = y / y, z = z / y)),
df %>% summarise(a = 2, across(c(x, y, z), ~ .x / y))
)
expect_identical(
df %>% summarise(a = 2, data.frame(x = x / y, y = y / y, z = z / y)),
df %>% summarise(a = 2, +across(c(x, y, z), ~ .x / y))
)

expect_identical(
df %>% summarise(a = 2, data.frame(x = x / y, y = y / y, z = z / y)),
df %>% summarise(a = 2, across(c(x, y, z), ~ .x / .data$y))
)
expect_identical(
df %>% summarise(a = 2, data.frame(x = x / y, y = y / y, z = z / y)),
df %>% summarise(a = 2, +across(c(x, y, z), ~ .x / .data$y))
)
})

test_that("functions defined inline can use columns (#5734)", {
Expand Down