Skip to content

Commit

Permalink
Implement count() method
Browse files Browse the repository at this point in the history
Fixes #159
  • Loading branch information
hadley committed Jan 26, 2021
1 parent 7664bd2 commit d1e03b3
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ S3method(as_tibble,dtplyr_step)
S3method(auto_copy,dtplyr_step)
S3method(collect,dtplyr_step)
S3method(compute,dtplyr_step)
S3method(count,dtplyr_step)
S3method(dim,dtplyr_step)
S3method(dim,dtplyr_step_first)
S3method(distinct,data.table)
Expand Down Expand Up @@ -87,6 +88,7 @@ importFrom(dplyr,arrange)
importFrom(dplyr,auto_copy)
importFrom(dplyr,collect)
importFrom(dplyr,compute)
importFrom(dplyr,count)
importFrom(dplyr,distinct)
importFrom(dplyr,do)
importFrom(dplyr,full_join)
Expand Down
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)

* Implement `count()` method for compatibility with dplyr 1.0.0 (#159).

* Anonymous functions work in `mutate()` and `summarise()` (@smingerson, #155)

* Environment variables used in the `i` argument of `[.data.table` are
Expand Down
2 changes: 1 addition & 1 deletion R/dtplyr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
#' @export
.datatable.aware <- TRUE

globalVariables(".N")
globalVariables(c(".N", "desc"))
31 changes: 31 additions & 0 deletions R/step-subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,37 @@ transmute.dtplyr_step <- function(.data, ...) {
step_subset_j(.data, vars = names(dots), j = j)
}

#' @importFrom dplyr count
#' @export
count.dtplyr_step <- function(.data, ..., wt = NULL, sort = FALSE, name = NULL) {
if (!missing(...)) {
out <- group_by(.data, ..., .add = TRUE)
} else {
out <- .data
}

wt <- enexpr(wt)
if (is.null(wt)) {
n <- expr(n())
} else {
n <- expr(sum(!!wt, na.rm = TRUE))
}

if (is.null(name)) {
name <- "n"
} else if (!is_string(name)) {
abort("`name` must be a string")
}

out <- summarise(out, !!name := !!n)

if (sort) {
out <- arrange(out, desc(!!sym(name)))
}

out
}

# exported onLoad
filter.dtplyr_step <- function(.data, ...) {
dots <- capture_dots(.data, ..., .j = FALSE)
Expand Down
1 change: 1 addition & 0 deletions R/step.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ as.data.frame.dtplyr_step <- function(x, ...) {
as_tibble.dtplyr_step <- function(x, ...) {
out <- as_tibble(dt_eval(x))
attr(out, ".internal.selfref") <- NULL
attr(out, "sorted") <- NULL
out
}

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/step-subset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# can control name

Code
dt %>% count(name = 10) %>% collect()
Error <rlang_error>
`name` must be a string

46 changes: 46 additions & 0 deletions tests/testthat/test-step-subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,52 @@ test_that("if for unsupported resummarise", {
expect_error(dt %>% summarise(x = mean(x), x2 = sd(x)), "mutate")
})


# count -------------------------------------------------------------------

test_that("can be used grouped or ungrouped", {
dt <- lazy_dt(data.table(x = c(1, 1, 1, 2)), "DT")

expect_equal(
dt %>% count(x) %>% collect(),
tibble(x = c(1, 2), n = c(3, 1))
)
expect_equal(
dt %>% group_by(x) %>% count() %>% collect(),
tibble(x = c(1, 2), n = c(3, 1))
)
})

test_that("can control name", {
dt <- lazy_dt(data.table(x = c(1, 1, 1, 2)), "DT")

expect_equal(
dt %>% count(x, name = "y") %>% collect(),
tibble(x = c(1, 2), y = c(3, 1))
)
expect_snapshot(
dt %>% count(name = 10) %>% collect(),
error = TRUE
)
})


test_that("can weight", {
dt <- lazy_dt(data.table(x = c(1, 1, 2), y = c(1, 2, 10)), "DT")
expect_equal(
dt %>% count(x, wt = y) %>% collect(),
tibble(x = c(1, 2), n = c(3, 10))
)
})

test_that("can sort", {
dt <- lazy_dt(data.table(x = c(1, 1, 2), y = c(1, 2, 10)), "DT")
expect_equal(
dt %>% count(x, wt = y, sort = TRUE) %>% collect(),
tibble(x = c(2, 1), n = c(10, 3))
)
})

# select/rename ------------------------------------------------------------------

test_that("renames grouping vars", {
Expand Down

0 comments on commit d1e03b3

Please sign in to comment.