Would you know a way to construct an empty grouped data.frame
or tibble
in dplyr
?
This is the best that I could come up with for a constructor of a custom grouped_df
class, but I don't like it as the result in fact isn't a true grouped_df
.
library(dplyr)
new_df_custom_grouped <- function(x = group_by(tibble())) {
# stopifnot(is_grouped_df(x))
structure(x,
class = c("tbl_df_custom_grouped", "tbl_df_custom", class(x)))
}
Motivation
In the pursuit of making my code compliant with tidy code principles, I looked into the section about constructors for S3 classes of the Advanced R book.
To define a custom method for dplyr::group_by()
while using cast methods as layed out in the article on S3 vectors of the vctrs
package, I need a constructor for a custom grouped data frame (see vctrs
GitHub issue #155 if you're interested in the details).