Skip to content

Commit

Permalink
Support .data$. in colwise filter_*()
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Feb 12, 2019
1 parent 7d41d7e commit a10f575
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
15 changes: 7 additions & 8 deletions R/colwise-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,19 @@ apply_filter_syms <- function(pred, syms, tbl) {
if (inherits(pred, "any_vars")) {
joiner <- any_exprs
}
pred <- new_function(exprs(. = ), quo_get_expr(pred), quo_get_env(pred))
} else if (is_bare_formula(pred)) {
pred <- map(syms, function(sym) expr_substitute(pred, quote(.), sym))
} else if (is_bare_formula(pred) || is_function(pred)) {
pred <- as_function(pred)
} else if (!is_function(pred)) {
pred <- map(syms, function(sym) call2(pred, sym))
} else {
bad_args(".vars_predicate", "must be a function or a call to `all_vars()` or `any_vars()`, ",
"not {friendly_type_of(pred)}"
)
}

pred <- map(syms, function(sym) call2(pred, sym))

if (length(pred)) {
joiner(!!!pred)
if (length(pred) == 1) {
pred[[1L]]
} else {
pred
joiner(!!!pred)
}
}
17 changes: 17 additions & 0 deletions tests/testthat/test-colwise-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,20 @@ test_that("can supply functions to scoped filters", {
out <- mtcars %>% filter_at(c("cyl", "am"), function(.x) .x == 4 | .x == 0)
expect_identical(as.list(out), exp)
})

test_that("colwise filter support .data$. in the quosure versions", {
expect_identical(
filter_if(iris, is.numeric, any_vars(.data$. > 4)),
filter_if(iris, is.numeric, any_vars(. > 4))
)

expect_identical(
filter_all(select(iris, -Species), any_vars(.data$. > 4)),
filter_all(select(iris, -Species), any_vars(. > 4))
)

expect_identical(
filter_at(iris, vars(contains(".")), any_vars(.data$. > 4)),
filter_at(iris, vars(contains(".")), any_vars(. > 4))
)
})

0 comments on commit a10f575

Please sign in to comment.