Skip to content

Commit

Permalink
Use lifecycle infrastructure for filter() deprecation warnings (#6468)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan authored Sep 20, 2022
1 parent 138f9fe commit f2c7dd5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
3 changes: 3 additions & 0 deletions R/conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ err_locs <- function(x) {
dplyr_internal_error <- function(class = NULL, data = list()) {
abort(class = c(class, "dplyr:::internal_error"), dplyr_error_data = data)
}
dplyr_internal_signal <- function(class) {
signal(message = "Internal dplyr signal", class = c(class, "dplyr:::internal_signal"))
}

skip_internal_condition <- function(cnd) {
if (inherits(cnd, "dplyr:::internal_error")) {
Expand Down
50 changes: 38 additions & 12 deletions R/filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,25 @@ filter_expand <- function(dots, mask, error_call = caller_env()) {
filter_eval <- function(dots, mask, error_call = caller_env()) {
env_filter <- env()

withCallingHandlers({
mask$eval_all_filter(dots, env_filter)
}, error = function(e) {
local_error_context(dots = dots, .index = env_filter$current_expression, mask = mask)

bullets <- c(
cnd_bullet_header("computing"),
filter_bullets(e)
)
abort(bullets, call = error_call, parent = skip_internal_condition(e))

})
withCallingHandlers(
expr = mask$eval_all_filter(dots, env_filter),
error = function(e) {
local_error_context(dots = dots, .index = env_filter$current_expression, mask = mask)

bullets <- c(
cnd_bullet_header("computing"),
filter_bullets(e)
)
abort(bullets, call = error_call, parent = skip_internal_condition(e))

},
`dplyr:::signal_filter_across` = function(e) {
warn_filter_across(call = error_call)
},
`dplyr:::signal_filter_data_frame` = function(e) {
warn_filter_data_frame(call = error_call)
}
)
}

filter_bullets <- function(cnd, ...) {
Expand Down Expand Up @@ -222,3 +229,22 @@ filter_bullets.default <- function(cnd, ...) {
)
}

warn_filter_across <- function(call) {
lifecycle::deprecate_warn(
when = "1.0.8",
what = I("Using `across()` in `filter()`"),
with = I("`if_any()` or `if_all()`"),
always = TRUE,
env = call
)
}

warn_filter_data_frame <- function(call) {
lifecycle::deprecate_warn(
when = "1.0.8",
what = I("Returning data frames from `filter()` expressions"),
with = I("`if_any()` or `if_all()`"),
always = TRUE,
env = call
)
}
1 change: 1 addition & 0 deletions src/dplyr.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct symbols {
static SEXP current_data;
static SEXP dot_drop;
static SEXP dplyr_internal_error;
static SEXP dplyr_internal_signal;
static SEXP dot_indices;
static SEXP chops;
static SEXP mask;
Expand Down
18 changes: 16 additions & 2 deletions src/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ void stop_filter_incompatible_type(R_xlen_t i, SEXP quos, SEXP column_name, SEXP
DPLYR_ERROR_THROW("dplyr:::filter_incompatible_type");
}

void signal_filter_across() {
SEXP cls = PROTECT(Rf_mkString("dplyr:::signal_filter_across"));
SEXP call = PROTECT(Rf_lang2(dplyr::symbols::dplyr_internal_signal, cls));
Rf_eval(call, dplyr::envs::ns_dplyr);
UNPROTECT(2);
}

void signal_filter_data_frame() {
SEXP cls = PROTECT(Rf_mkString("dplyr:::signal_filter_data_frame"));
SEXP call = PROTECT(Rf_lang2(dplyr::symbols::dplyr_internal_signal, cls));
Rf_eval(call, dplyr::envs::ns_dplyr);
UNPROTECT(2);
}

}

bool all_lgl_columns(SEXP data) {
Expand Down Expand Up @@ -118,9 +132,9 @@ SEXP eval_filter_one(SEXP quos, SEXP mask, SEXP caller, R_xlen_t n, SEXP env_fil
SEXP expr = rlang::quo_get_expr(VECTOR_ELT(quos, i));
bool across = TYPEOF(expr) == LANGSXP && CAR(expr) == dplyr::symbols::across;
if (across) {
Rf_warningcall(R_NilValue, "Using `across()` in `filter()` is deprecated, use `if_any()` or `if_all()`.");
dplyr::signal_filter_across();
} else {
Rf_warningcall(R_NilValue, "data frame results in `filter()` are deprecated, use `if_any()` or `if_all()`.");
dplyr::signal_filter_data_frame();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ SEXP symbols::caller = Rf_install("caller");
SEXP symbols::current_data = Rf_install("current_data");
SEXP symbols::dot_drop = Rf_install(".drop");
SEXP symbols::dplyr_internal_error = Rf_install("dplyr_internal_error");
SEXP symbols::dplyr_internal_signal = Rf_install("dplyr_internal_signal");
SEXP symbols::dot_indices = Rf_install(".indices");
SEXP symbols::chops = Rf_install("chops");
SEXP symbols::mask = Rf_install("mask");
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/_snaps/filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,17 @@
data.frame(x = 1, y = 1) %>% filter(across(everything(), ~ .x > 0))
Condition
Warning:
Using `across()` in `filter()` is deprecated, use `if_any()` or `if_all()`.
Using `across()` in `filter()` was deprecated in dplyr 1.0.8.
Please use `if_any()` or `if_all()` instead.
Output
x y
1 1 1
Code
data.frame(x = 1, y = 1) %>% filter(data.frame(x > 0, y > 0))
Condition
Warning:
data frame results in `filter()` are deprecated, use `if_any()` or `if_all()`.
Returning data frames from `filter()` expressions was deprecated in dplyr 1.0.8.
Please use `if_any()` or `if_all()` instead.
Output
x y
1 1 1
Expand Down

0 comments on commit f2c7dd5

Please sign in to comment.