Skip to content

Commit

Permalink
n_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Mar 7, 2024
1 parent 64dd8b7 commit c11b900
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ S3method(inner_join,duckplyr_df)
S3method(intersect,duckplyr_df)
S3method(left_join,duckplyr_df)
S3method(mutate,duckplyr_df)
S3method(n_groups,duckplyr_df)
S3method(nest_by,duckplyr_df)
S3method(nest_join,duckplyr_df)
S3method(print,relational_relexpr)
Expand Down Expand Up @@ -283,6 +284,7 @@ importFrom(dplyr,mutate)
importFrom(dplyr,mutate_all)
importFrom(dplyr,n)
importFrom(dplyr,n_distinct)
importFrom(dplyr,n_groups)
importFrom(dplyr,nest_by)
importFrom(dplyr,nest_join)
importFrom(dplyr,nth)
Expand Down
1 change: 1 addition & 0 deletions R/duckplyr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#' @importFrom dplyr group_trim
#' @importFrom dplyr grouped_df
#' @importFrom dplyr if_else
#' @importFrom dplyr n_groups
#' @importFrom glue glue
#' @importFrom lifecycle deprecated
#' @importFrom tibble as_tibble
Expand Down
32 changes: 32 additions & 0 deletions R/n_groups.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by 02-duckplyr_df-methods.R
#' @export
n_groups.duckplyr_df <- function(x) {
# Our implementation
rel_try(
# Always fall back to dplyr
"No relational implementation for n_groups()" = TRUE,
{
return(out)
}
)

# dplyr forward
n_groups <- dplyr$n_groups.data.frame
out <- n_groups(x)
return(out)

# dplyr implementation
nrow(group_data(x))
}

duckplyr_n_groups <- function(x, ...) {
try_fetch(
x <- as_duckplyr_df(x),
error = function(e) {
testthat::skip(conditionMessage(e))
}
)
out <- n_groups(x, ...)
class(out) <- setdiff(class(out), "duckplyr_df")
out
}
1 change: 1 addition & 0 deletions R/overwrite.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ methods_overwrite <- function() {
vctrs::s3_register("dplyr::intersect", "data.frame", intersect.duckplyr_df)
vctrs::s3_register("dplyr::left_join", "data.frame", left_join.duckplyr_df)
vctrs::s3_register("dplyr::mutate", "data.frame", mutate.duckplyr_df)
vctrs::s3_register("dplyr::n_groups", "data.frame", n_groups.duckplyr_df)
vctrs::s3_register("dplyr::nest_by", "data.frame", nest_by.duckplyr_df)
vctrs::s3_register("dplyr::nest_join", "data.frame", nest_join.duckplyr_df)
vctrs::s3_register("dplyr::pull", "data.frame", pull.duckplyr_df)
Expand Down
1 change: 1 addition & 0 deletions R/restore.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ methods_restore <- function() {
vctrs::s3_register("dplyr::intersect", "data.frame", dplyr$intersect.data.frame)
vctrs::s3_register("dplyr::left_join", "data.frame", dplyr$left_join.data.frame)
vctrs::s3_register("dplyr::mutate", "data.frame", dplyr$mutate.data.frame)
vctrs::s3_register("dplyr::n_groups", "data.frame", dplyr$n_groups.data.frame)
vctrs::s3_register("dplyr::nest_by", "data.frame", dplyr$nest_by.data.frame)
vctrs::s3_register("dplyr::nest_join", "data.frame", dplyr$nest_join.data.frame)
vctrs::s3_register("dplyr::pull", "data.frame", dplyr$pull.data.frame)
Expand Down
3 changes: 3 additions & 0 deletions dplyr-methods/n_groups.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
n_groups.data.frame <- function(x) {
nrow(group_data(x))
}
16 changes: 16 additions & 0 deletions tests/testthat/test-as_duckplyr_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,22 @@ test_that("as_duckplyr_df() and mutate(c = .data$b)", {
expect_equal(pre, post)
})

test_that("as_duckplyr_df() and n_groups()", {
withr::local_envvar(DUCKPLYR_FORCE = "FALSE")

skip("Special")

# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))

# Run
pre <- test_df %>% as_duckplyr_df() %>% n_groups()
post <- test_df %>% n_groups() %>% as_duckplyr_df()

# Compare
expect_equal(pre, post)
})

test_that("as_duckplyr_df() and nest_by()", {
withr::local_envvar(DUCKPLYR_FORCE = "FALSE")

Expand Down
3 changes: 2 additions & 1 deletion tools/00-funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ df_methods <-
filter(!grepl("_$|^as[.]tbl$", name)) %>%
# special dplyr methods, won't implement
filter(!(name %in% c(
"n_groups",
"same_src", # data frames can be copied into duck-frames with zero cost
NULL
))) %>%
Expand All @@ -35,6 +34,7 @@ df_methods <-
"group_split",
"group_trim",
"groups",
"n_groups",
"rowwise",
NULL
))) %>%
Expand Down Expand Up @@ -711,6 +711,7 @@ test_skip_map <- c(
group_split = "WAT",
group_trim = "Grouped",
groups = "Special",
n_groups = "Special",
nest_by = "WAT",
# FIXME: Fail with rowwise()
rowwise = "Stack overflow",
Expand Down

0 comments on commit c11b900

Please sign in to comment.