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

Drop caller_env from DataMask #6444

Merged
merged 5 commits into from
Sep 13, 2022
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
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)

* `group_by_prepare()` loses the `caller_env` argument. It was rarely used
and it is no longer needed (#6444).

* `nth()`, `first()`, `last()`, and `with_order()` now sort character `order_by`
vectors in the C locale. Using character vectors for `order_by` is rare, so we
expect this to have little practical impact (#6451).
Expand Down
2 changes: 1 addition & 1 deletion R/across.R
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ expand_across <- function(quo) {
!!cols,
fns = eval_tidy(expr$.fns, mask, env = env),
names = eval_tidy(expr$.names, mask, env = env),
.caller_env = dplyr_mask$get_caller_env(),
.caller_env = env,
mask = dplyr_mask,
inline = TRUE
)
Expand Down
10 changes: 1 addition & 9 deletions R/data-mask.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DataMask <- R6Class("DataMask",
public = list(
initialize = function(data, caller, verb, error_call) {
initialize = function(data, verb, error_call) {
rows <- group_rows(data)
if (length(rows) == 0) {
# Specially handle case of zero groups
Expand All @@ -17,7 +17,6 @@ DataMask <- R6Class("DataMask",
}
names(data) <- names_bindings
private$size <- nrow(data)
private$caller <- caller
private$current_data <- unclass(data)

private$chops <- .Call(dplyr_lazy_vec_chop_impl, data, rows)
Expand Down Expand Up @@ -185,10 +184,6 @@ DataMask <- R6Class("DataMask",

get_rlang_mask = function() {
private$mask
},

get_caller_env = function() {
private$caller
}

),
Expand Down Expand Up @@ -226,9 +221,6 @@ DataMask <- R6Class("DataMask",
grouped_df = NULL,
rowwise_df = NULL,

# caller environment of the verb (summarise(), ...)
caller = NULL,

verb = character()
)
)
5 changes: 1 addition & 4 deletions R/distinct.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ distinct_prepare <- function(.data,
}

# If any calls, use mutate to add new columns, then distinct on those
computed_columns <- add_computed_columns(.data, vars,
caller_env = caller_env,
error_call = error_call
)
computed_columns <- add_computed_columns(.data, vars, error_call = error_call)
.data <- computed_columns$data
distinct_vars <- computed_columns$added_names

Expand Down
6 changes: 3 additions & 3 deletions R/filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ filter <- function(.data, ..., .preserve = FALSE) {

#' @export
filter.data.frame <- function(.data, ..., .preserve = FALSE) {
loc <- filter_rows(.data, ..., caller_env = caller_env())
loc <- filter_rows(.data, ...)
dplyr_row_slice(.data, loc, preserve = .preserve)
}

filter_rows <- function(.data, ..., caller_env, error_call = caller_env()) {
filter_rows <- function(.data, ..., error_call = caller_env()) {
error_call <- dplyr_error_call(error_call)

dots <- dplyr_quosures(...)
DavisVaughan marked this conversation as resolved.
Show resolved Hide resolved
check_filter(dots, error_call = error_call)

mask <- DataMask$new(.data, caller_env, "filter", error_call = error_call)
mask <- DataMask$new(.data, "filter", error_call = error_call)
on.exit(mask$forget(), add = TRUE)

dots <- filter_expand(dots, mask = mask, error_call = error_call)
Expand Down
11 changes: 2 additions & 9 deletions R/group-by.r
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ group_by.data.frame <- function(.data, ..., .add = FALSE, .drop = group_by_drop_
.data,
...,
.add = .add,
caller_env = caller_env(),
error_call = current_env()
)
grouped_df(groups$data, groups$group_names, .drop)
Expand Down Expand Up @@ -192,7 +191,6 @@ ungroup.data.frame <- function(x, ...) {
#' @keywords internal
group_by_prepare <- function(.data,
...,
caller_env = caller_env(2),
.add = FALSE,
.dots = deprecated(),
add = deprecated(),
Expand All @@ -208,14 +206,11 @@ group_by_prepare <- function(.data,
if (!missing(.dots)) {
# Used by dbplyr 1.4.2 so can't aggressively deprecate
lifecycle::deprecate_warn("1.0.0", "group_by(.dots = )", always = TRUE)
new_groups <- c(new_groups, compat_lazy_dots(.dots, env = caller_env))
new_groups <- c(new_groups, compat_lazy_dots(.dots, env = caller_env(2)))
Comment on lines -211 to +209
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess it still technically had meaning here, but we are ok with removing caller_env as an input arg because this is deprecated behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the argument was particularly troublesome because (a) it lacked a leading . and (b) it had the same name as a function we called.

}

# If any calls, use mutate to add new columns, then group by those
computed_columns <- add_computed_columns(.data, new_groups,
caller_env = caller_env,
error_call = error_call
)
computed_columns <- add_computed_columns(.data, new_groups, error_call = error_call)

out <- computed_columns$data
group_names <- computed_columns$added_names
Expand All @@ -242,7 +237,6 @@ group_by_prepare <- function(.data,

add_computed_columns <- function(.data,
vars,
caller_env,
error_call = caller_env()) {
is_symbol <- map_lgl(vars, quo_is_variable_reference)
needs_mutate <- have_name(vars) | !is_symbol
Expand All @@ -253,7 +247,6 @@ add_computed_columns <- function(.data,
cols <- mutate_cols(
ungroup(.data),
dplyr_quosures(!!!vars),
caller_env = caller_env,
error_call = error_call
)

Expand Down
6 changes: 3 additions & 3 deletions R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mutate.data.frame <- function(.data,
.after = NULL) {
keep <- arg_match(.keep)

cols <- mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
cols <- mutate_cols(.data, dplyr_quosures(...))
used <- attr(cols, "used")

out <- dplyr_col_modify(.data, cols)
Expand Down Expand Up @@ -218,13 +218,13 @@ mutate.data.frame <- function(.data,

# Helpers -----------------------------------------------------------------

mutate_cols <- function(.data, dots, caller_env, error_call = caller_env()) {
mutate_cols <- function(.data, dots, error_call = caller_env()) {
# Collect dots before setting up error handlers (#6178)
force(dots)

DavisVaughan marked this conversation as resolved.
Show resolved Hide resolved
error_call <- dplyr_error_call(error_call)

mask <- DataMask$new(.data, caller_env, "mutate", error_call = error_call)
mask <- DataMask$new(.data, "mutate", error_call = error_call)
old_current_column <- context_peek_bare("column")

on.exit(context_poke("column", old_current_column), add = TRUE)
Expand Down
6 changes: 3 additions & 3 deletions R/slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ slice <- function(.data, ..., .preserve = FALSE) {

#' @export
slice.data.frame <- function(.data, ..., .preserve = FALSE) {
loc <- slice_rows(.data, ..., caller_env = caller_env(), error_call = current_env())
loc <- slice_rows(.data, ..., error_call = current_env())
dplyr_row_slice(.data, loc, preserve = .preserve)
}

Expand Down Expand Up @@ -270,14 +270,14 @@ slice_sample.data.frame <- function(.data, ..., n, prop, weight_by = NULL, repla

# helpers -----------------------------------------------------------------

slice_rows <- function(.data, ..., caller_env, error_call = caller_env()) {
slice_rows <- function(.data, ..., error_call = caller_env()) {
error_call <- dplyr_error_call(error_call)

dots <- enquos(...)
if (is_empty(dots)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can caller_env be removed from slice_rows()?

return(TRUE)
}
mask <- DataMask$new(.data, caller_env, "slice", error_call = error_call)
mask <- DataMask$new(.data, "slice", error_call = error_call)
on.exit(mask$forget(), add = TRUE)

chunks <- slice_eval(mask, dots, error_call = error_call)
Expand Down
10 changes: 5 additions & 5 deletions R/summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ summarize <- summarise

#' @export
summarise.data.frame <- function(.data, ..., .groups = NULL) {
cols <- summarise_cols(.data, dplyr_quosures(...), caller_env = caller_env())
cols <- summarise_cols(.data, dplyr_quosures(...))
out <- summarise_build(.data, cols)
if (identical(.groups, "rowwise")) {
out <- rowwise_df(out, character())
Expand All @@ -135,7 +135,7 @@ summarise.data.frame <- function(.data, ..., .groups = NULL) {

#' @export
summarise.grouped_df <- function(.data, ..., .groups = NULL) {
cols <- summarise_cols(.data, dplyr_quosures(...), caller_env = caller_env())
cols <- summarise_cols(.data, dplyr_quosures(...))
out <- summarise_build(.data, cols)
verbose <- summarise_verbose(.groups, caller_env())

Expand Down Expand Up @@ -178,7 +178,7 @@ summarise.grouped_df <- function(.data, ..., .groups = NULL) {

#' @export
summarise.rowwise_df <- function(.data, ..., .groups = NULL) {
cols <- summarise_cols(.data, dplyr_quosures(...), caller_env = caller_env())
cols <- summarise_cols(.data, dplyr_quosures(...))
out <- summarise_build(.data, cols)
verbose <- summarise_verbose(.groups, caller_env())

Expand All @@ -202,10 +202,10 @@ summarise.rowwise_df <- function(.data, ..., .groups = NULL) {
out
}

summarise_cols <- function(.data, dots, caller_env, error_call = caller_env()) {
summarise_cols <- function(.data, dots, error_call = caller_env()) {
error_call <- dplyr_error_call(error_call)

mask <- DataMask$new(.data, caller_env, "summarise", error_call = error_call)
mask <- DataMask$new(.data, "summarise", error_call = error_call)
old_current_column <- context_peek_bare("column")

on.exit(context_poke("column", old_current_column), add = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion R/transmute.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ transmute.data.frame <- function(.data, ...) {
dots <- check_transmute_args(...)
dots <- dplyr_quosures(!!!dots)

cols <- mutate_cols(.data, dots, caller_env = caller_env())
cols <- mutate_cols(.data, dots)

out <- dplyr_col_modify(.data, cols)

Expand Down
1 change: 0 additions & 1 deletion man/group_by_prepare.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions tests/testthat/helper-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,3 @@ expect_no_error <- function(object, ...) {
expect_no_warning <- function(object, ...) {
expect_warning({{ object }}, NA, ...)
}

sig_caller_env <- function() {
signal(
"",
"dplyr:::test_caller_env",
out = peek_mask()$get_caller_env()
)
}
expect_caller_env <- function(expr) {
env <- catch_cnd(expr, "dplyr:::test_caller_env")$out
expect_equal(env, caller_env())
}
13 changes: 9 additions & 4 deletions tests/testthat/test-across.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@ test_that("across(.unpack =) allows a glue specification for `.unpack`", {
}

df <- tibble(x = 1)

out <- mutate(df, across(x, fn, .unpack = "{outer}.{inner}"))

expect_named(out, c("x", "x.a", "x.b"))

# Can use variables from caller env
out <- local({
name <- "name"
mutate(df, across(x, fn, .unpack = "{name}.{inner}"))
})
expect_named(out, c("x", "name.a", "name.b"))
})

test_that("across(.unpack =) skips unpacking non-df-cols", {
Expand Down Expand Up @@ -908,7 +913,7 @@ test_that("expand_across() expands lambdas", {
index = 1
)

DataMask$new(mtcars, current_env(), "mutate", call("caller"))
DataMask$new(mtcars, "mutate", call("caller"))

expect_equal(
map(expand_across(quo), quo_get_expr),
Expand All @@ -929,7 +934,7 @@ test_that("expand_if_across() expands lambdas", {
index = 1
)

DataMask$new(mtcars, current_env(), "mutate", call("caller"))
DataMask$new(mtcars, "mutate", call("caller"))

expect_equal(
map(expand_if_across(quo), quo_squash),
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-distinct.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ test_that("distinct preserves grouping", {
expect_equal(group_vars(out), "x")
})

test_that("distinct() propagates caller env", {
expect_caller_env(distinct(mtcars, sig_caller_env()))
})

test_that("distinct() preserves attributes on bare data frames (#6318)", {
df <- vctrs::data_frame(x = c(1, 1))
attr(df, "foo") <- "bar"
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-group-by.r
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,6 @@ test_that("group_by() works with quosures (tidyverse/lubridate#959)", {
expect_equal(g(), tibble(x = 1, g = NA) %>% group_by(g))
})

test_that("group_by() propagates caller env", {
expect_caller_env(group_by(mtcars, sig_caller_env()))
})


# Errors ------------------------------------------------------------------

Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/test-mutate.r
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,6 @@ test_that("mutate() supports empty list columns in rowwise data frames (#5804",
expect_equal(res$n, integer())
})

test_that("mutate() propagates caller env", {
expect_caller_env(mutate(mtcars, sig_caller_env()))
})

test_that("mutate() fails on named empty arguments (#5925)", {
expect_error(
mutate(tibble(), bogus = )
Expand Down
6 changes: 0 additions & 6 deletions tests/testthat/test-summarise.r
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,6 @@ test_that("summarise() silently skips when all results are NULL (#5708)", {
expect_error(summarise(df, x = if(g == 1) 42))
})

test_that("summarise() propagates caller env", {
expect_caller_env(summarise(mtcars, sig_caller_env()))
expect_caller_env(summarise(group_by(mtcars, cyl), sig_caller_env()))
})


# errors -------------------------------------------------------------------

test_that("summarise() preserves the call stack on error (#5308)", {
Expand Down