Skip to content

Commit

Permalink
Add .negate to filter_on (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
asardaes committed Jun 23, 2019
1 parent 03cb7c9 commit d01879a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- `transmute` is no longer an alias for `select`. This is to make the latter more flexible in some
bare selection cases (like combining several `tidyselect` calls), and leave the former more simple
and preferable for actual transmutation cases.
- `filter_on` gains a `.negate` parameter.

# table.express 0.1.1

Expand Down
12 changes: 10 additions & 2 deletions R/VERBS-filter_on.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#'
#' @export
#' @importFrom rlang abort
#' @importFrom rlang expr
#'
#' @template data-arg
#' @param ... Key-value pairs, see details.
#' @param nomatch See [data.table::data.table].
#' @param mult See [data.table::data.table].
#' @param .negate Whether to negate the expression and search only for rows that don't contain the
#' given values.
#' @template chain-arg
#'
#' @details
Expand All @@ -30,7 +33,7 @@
#' start_expr %>%
#' filter_on(cyl = 4, gear = 5)
#'
filter_on <- function(.data, ..., nomatch = getOption("datatable.nomatch"), mult = "all",
filter_on <- function(.data, ..., nomatch = getOption("datatable.nomatch"), mult = "all", .negate = FALSE,
.chain = getOption("table.express.chain", TRUE))
{
key_value <- parse_dots(FALSE, ...)
Expand All @@ -41,7 +44,12 @@ filter_on <- function(.data, ..., nomatch = getOption("datatable.nomatch"), mult
rlang::abort("All arguments in '...' must be named.")
}

ans <- where(.data, list(!!!values), .parse = FALSE, .chain = .chain) %>%
clause <- rlang::expr(list(!!!values))
if (.negate) {
clause <- rlang::expr(`!`(`!!`(clause)))
}

ans <- .data$set_where(clause, .chain) %>%
frame_append(on = !!keys, .parse = FALSE)

if (!missing(nomatch)) {
Expand Down
1 change: 1 addition & 0 deletions inst/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- `transmute` is no longer an alias for `select`. This is to make the latter more flexible in some
bare selection cases (like combining several `tidyselect` calls), and leave the former more simple
and preferable for actual transmutation cases.
- `filter_on` gains a `.negate` parameter.
6 changes: 6 additions & 0 deletions tests/testthat/unit/test-filter_on.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ test_that("The filter_on verb works as expected.", {
expect_identical(nrow(ans), 0L)
})

test_that("The filter_on semantics can be negated.", {
expected <- DT[!list(0), on = "am"]
ans <- DT %>% start_expr %>% filter_on(am = 0, .negate = TRUE) %>% end_expr
expect_identical(ans, expected)
})

test_that("The filter_on verb works for several values per key.", {
expected <- state[.(c("South", "West"), c("South Atlantic", "Pacific"))]

Expand Down

0 comments on commit d01879a

Please sign in to comment.