Skip to content

Commit

Permalink
groups
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Mar 7, 2024
1 parent 674690c commit f700612
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ S3method(group_map,duckplyr_df)
S3method(group_modify,duckplyr_df)
S3method(group_size,duckplyr_df)
S3method(group_vars,duckplyr_df)
S3method(groups,duckplyr_df)
S3method(head,duckplyr_df)
S3method(inner_join,duckplyr_df)
S3method(intersect,duckplyr_df)
Expand Down
32 changes: 32 additions & 0 deletions R/groups.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by 02-duckplyr_df-methods.R
#' @export
groups.duckplyr_df <- function(x) {
# Our implementation
rel_try(
# Always fall back to dplyr
"No relational implementation for groups()" = TRUE,
{
return(out)
}
)

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

# dplyr implementation
syms(group_vars(x))
}

duckplyr_groups <- function(x, ...) {
try_fetch(
x <- as_duckplyr_df(x),
error = function(e) {
testthat::skip(conditionMessage(e))
}
)
out <- 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 @@ -21,6 +21,7 @@ methods_overwrite <- function() {
vctrs::s3_register("dplyr::group_modify", "data.frame", group_modify.duckplyr_df)
vctrs::s3_register("dplyr::group_size", "data.frame", group_size.duckplyr_df)
vctrs::s3_register("dplyr::group_vars", "data.frame", group_vars.duckplyr_df)
vctrs::s3_register("dplyr::groups", "data.frame", groups.duckplyr_df)
vctrs::s3_register("dplyr::inner_join", "data.frame", inner_join.duckplyr_df)
vctrs::s3_register("dplyr::intersect", "data.frame", intersect.duckplyr_df)
vctrs::s3_register("dplyr::left_join", "data.frame", left_join.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 @@ -21,6 +21,7 @@ methods_restore <- function() {
vctrs::s3_register("dplyr::group_modify", "data.frame", dplyr$group_modify.data.frame)
vctrs::s3_register("dplyr::group_size", "data.frame", dplyr$group_size.data.frame)
vctrs::s3_register("dplyr::group_vars", "data.frame", dplyr$group_vars.data.frame)
vctrs::s3_register("dplyr::groups", "data.frame", dplyr$groups.data.frame)
vctrs::s3_register("dplyr::inner_join", "data.frame", dplyr$inner_join.data.frame)
vctrs::s3_register("dplyr::intersect", "data.frame", dplyr$intersect.data.frame)
vctrs::s3_register("dplyr::left_join", "data.frame", dplyr$left_join.data.frame)
Expand Down
3 changes: 3 additions & 0 deletions dplyr-methods/groups.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
groups.data.frame <- function(x) {
syms(group_vars(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 @@ -863,6 +863,22 @@ test_that("as_duckplyr_df() and group_vars()", {
expect_equal(pre, post)
})

test_that("as_duckplyr_df() and 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() %>% groups()
post <- test_df %>% groups() %>% as_duckplyr_df()

# Compare
expect_equal(pre, post)
})

test_that("as_duckplyr_df() and inner_join(join_by(a))", {
withr::local_envvar(DUCKPLYR_FALLBACK_FORCE = "TRUE")

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

0 comments on commit f700612

Please sign in to comment.