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

Implements drop_na() #194

Merged
merged 9 commits into from
Feb 21, 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
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# dtplyr (development version)

* More tidyr verbs are in the process of being added:

* `drop_na()` (@markfairbanks, #194)

# dtplyr 1.1.0

## New features
Expand Down Expand Up @@ -29,7 +33,7 @@
(apart from predicate functions which can't easily work on lazily evaluated
data tables).

* We have begun the process of add translations for tidyr verbs beginning
* We have begun the process of adding translations for tidyr verbs beginning
with `pivot_wider()` (@markfairbanks, #189).

## Translation improvements
Expand Down
38 changes: 38 additions & 0 deletions R/step-call.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,41 @@ unique.dtplyr_step <- function(x, incomparables = FALSE, ...) {
}
distinct(x)
}

# tidyr verbs -------------------------------------------------------------
#' Drop rows containing missing values
#'
#' @description
#' This is a method for the tidyr `drop_na()` generic. It is translated to
#' `data.table::na.omit()`
#'
#' @param data A [lazy_dt()].
#' @inheritParams tidyr::drop_na
#' @examples
#' library(dplyr)
#' library(tidyr)
#'
#' dt <- lazy_dt(tibble(x = c(1, 2, NA), y = c("a", NA, "b")))
#' dt %>% drop_na()
#' dt %>% drop_na(x)
#'
#' vars <- "y"
#' dt %>% drop_na(x, any_of(vars))
# exported onLoad
drop_na.dtplyr_step <- function(data, ...) {
sim_data <- simulate_vars(data)
locs <- names(tidyselect::eval_select(expr(c(...)), sim_data))

args <- list()
if (length(locs) > 0) {
args$cols <- locs
}

step_call(data, "na.omit", args = args)
}

# exported onLoad
drop_na.data.table <- function(data, ...) {
data <- lazy_dt(data)
tidyr::drop_na(data, ...)
}
2 changes: 2 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
register_s3_method("dplyr", "intersect", "data.table")
register_s3_method("dplyr", "setdiff", "data.table")
register_s3_method("dplyr", "union", "data.table")
register_s3_method("tidyr", "drop_na", "data.table")
register_s3_method("tidyr", "pivot_wider", "data.table")

register_s3_method("dplyr", "filter", "dtplyr_step")
register_s3_method("dplyr", "intersect", "dtplyr_step")
register_s3_method("dplyr", "setdiff", "dtplyr_step")
register_s3_method("dplyr", "union", "dtplyr_step")
register_s3_method("tidyr", "drop_na", "dtplyr_step")
register_s3_method("tidyr", "pivot_wider", "dtplyr_step")
}

Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ reference:

- title: tidyr verbs
contents:
- drop_na.dtplyr_step
- pivot_wider.dtplyr_step

navbar:
Expand Down
29 changes: 29 additions & 0 deletions man/drop_na.dtplyr_step.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/step-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@
Output
setnames(copy(DT), c("a", "b", "c"), toupper)

# errors are raised

Code
collect(drop_na(dt, "z"))
Error <vctrs_error_subscript_oob>
Can't subset columns that don't exist.
x Column `z` doesn't exist.

33 changes: 33 additions & 0 deletions tests/testthat/test-step-call.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,36 @@ test_that("unique is an alias for distinct", {
dt <- lazy_dt(data.table(x = c(1, 1)))
expect_equal(unique(dt), distinct(dt))
})

# drop_na ------------------------------------------------------------------

test_that("empty call drops every row", {
tb <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
step <- drop_na(lazy_dt(tb, "DT"))
expect_equal(show_query(step), expr(na.omit(DT)))
expect_equal(as_tibble(step), tb[1, ])
})

test_that("uses specified variables", {
df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
dt <- lazy_dt(df, "DT")

step <- drop_na(dt, x)
expect_equal(show_query(step), expr(na.omit(DT, cols = "x")))
expect_equal(collect(step), df[1:2, ])

step <- drop_na(dt, x:y)
expect_equal(show_query(step), expr(na.omit(DT, cols = !!c("x", "y"))))
expect_equal(collect(step), df[1, ])
})

test_that("errors are raised", {
tb <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
dt <- lazy_dt(tb, "DT")
expect_snapshot(collect(drop_na(dt, "z")), error = TRUE)
})

test_that("converts data.table to dtplyr_step", {
df <- data.table(x = c(1, 2, NA), y = c("a", NA, "b"))
expect_s3_class(drop_na(df), "dtplyr_step_call")
})