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

filter() checks for names #268

Merged
merged 4 commits into from
Jul 5, 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: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dtplyr (development version)

* `filter()` now errors for named input, e.g. `filter(dt, x = 1)` (@mgirlich, #267).

* `pull()` now supports the `name` argument (@mgirlich, #263).

* @mgirlich is now a dtplyr author in recognition of his significant and
Expand Down
9 changes: 1 addition & 8 deletions R/step-nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ nest.dtplyr_step <- function(.data, ..., .names_sep = NULL, .key = deprecated())
j_exprs <- imap(
cols,
function(x, name) {
x <- simplify_names2(x)
x <- simplify_names(x)
expr(.(data.table(!!!syms(x))))
}
)
Expand Down Expand Up @@ -81,10 +81,3 @@ eval_nest_dots <- function(.data, ...) {
lapply(cols, function(.x) names(tidyselect::eval_select(.x, sim_data)))
}
}

simplify_names2 <- function(x) {
auto_names <- rlang::exprs_auto_name(x)
name_unnecessary <- names2(x) == auto_names
names(x)[name_unnecessary] <- ""
x
}
23 changes: 23 additions & 0 deletions R/step-subset-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#' @importFrom dplyr filter
# exported onLoad
filter.dtplyr_step <- function(.data, ..., .preserve = FALSE) {
check_filter(...)
dots <- capture_dots(.data, ..., .j = FALSE)

if (filter_by_lgl_col(dots)) {
Expand Down Expand Up @@ -51,3 +52,25 @@ filter.data.table <- function(.data, ...) {
.data <- lazy_dt(.data)
filter(.data, ...)
}

check_filter <- function(...) {
dots <- enquos(...)
named <- have_name(dots)

for (i in which(named)) {
quo <- dots[[i]]

# only allow named logical vectors, anything else
# is suspicious
expr <- quo_get_expr(quo)
if (!is.logical(expr)) {
abort(c(
glue::glue("Problem with `filter()` input `..{i}`."),
x = glue::glue("Input `..{i}` is named."),
i = glue::glue("This usually means that you've used `=` instead of `==`."),
i = glue::glue("Did you mean `{name} == {as_label(expr)}`?", name = names(dots)[i])
))
}

}
}
20 changes: 20 additions & 0 deletions tests/testthat/_snaps/step-subset-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# errors for named input

Code
filter(dt, x = 1)
Error <rlang_error>
Problem with `filter()` input `..1`.
x Input `..1` is named.
i This usually means that you've used `=` instead of `==`.
i Did you mean `x == 1`?

---

Code
filter(dt, y > 1, x = 1)
Error <rlang_error>
Problem with `filter()` input `..2`.
x Input `..2` is named.
i This usually means that you've used `=` instead of `==`.
i Did you mean `x == 1`?

21 changes: 21 additions & 0 deletions tests/testthat/test-step-subset-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,24 @@ test_that("only adds step if dots are not empty", {
expect_equal(dt %>% filter(), dt)
expect_equal(dt %>% filter(!!!list()), dt)
})

test_that("errors for named input", {
dt <- lazy_dt(data.table(x = 1, y = 2), "DT")

expect_snapshot(error = TRUE, filter(dt, x = 1))
expect_snapshot(error = TRUE, filter(dt, y > 1, x = 1))
})

test_that("allows named constants that resolve to logical vectors", {
dt <- lazy_dt(mtcars, "DT")
filters <- mtcars %>%
transmute(
cyl %in% 6:8,
hp / drat > 50
)

expect_equal(
filter(dt, !!!filters),
filter(dt, !!!unname(filters))
)
})