-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
group_by and grouped_df for classes extending tibble #5480
Comments
I would probably use library(dplyr)
new_special_df <- function(x, ..., class = character()) {
x <- tibble::as_tibble(x)
tibble::new_tibble(x = x, ..., nrow = nrow(x), class = c(class, "special_df"))
}
new_grouped_special_df <- function(x, groups, ..., class = character()) {
class <- c(class, c("grouped_special_df", "grouped_df"))
new_special_df(x, groups = groups, ..., class = class)
}
group_by.special_df <- function(.data,
...,
.add = FALSE,
.drop = group_by_drop_default(.data)) {
out <- NextMethod()
groups <- group_data(out)
new_grouped_special_df(out, groups)
}
df <- new_special_df(list(x = 1, y = 2))
class(df)
#> [1] "special_df" "tbl_df" "tbl" "data.frame"
gdf <- group_by(df, x)
class(gdf)
#> [1] "grouped_special_df" "grouped_df" "special_df"
#> [4] "tbl_df" "tbl" "data.frame" Created on 2020-08-18 by the reprex package (v0.3.0.9001) Two pain points I ran into here (for us to review internally):
|
Proposed solution works indeed for group_by, thanks. Unfortunately we face the same issue when trying to implement summarise, since the constructor for grouped_df is directly called in the body of the function: classes are completely dropped and only |
@GiuliaPais you'll need to supply your own |
As per title, we're developing a package where we use a structure that inherits from tibble, defined as follows:
Unfortunately when performing grouping on this structure, class isn't preserved and we've found no proper way to implement group_by generic, we've tried this:
But as you can see
compute_groups
is not an exported function from dplyr. Is there any proper way to do this or could this function be exported so we can call it in our implementation?The text was updated successfully, but these errors were encountered: