I have a function that is supposed to be used in a grouped dplyr::summarise(). I want to access both the data of each group as well as the full data. The former can easily be achieved with dplyr::across(). For the latter, I only see the option to rely on an internal function mask <- dplyr:::peek_mask() and then do mask$full_data().
My question: Is there any way to access the full data without relying on an internal function?
Below is a simple example, the actual function is rather complex, but in its essence it compares the grouped data to the full data.
Added for clarification: Ideally, the user can call this function foo and it produces the result inside dplyr::summarise without the need of specifying further arguments or adding calls.
Any help appreciated.
library(dplyr)
# function to be used inside summarize
foo <- function() {
mask <- dplyr:::peek_mask() # replace this function without relying on dplyr internals
full_data <- mask$full_data()
tibble(group_rows = nrow(across()),
full_data = nrow(full_data))
}
mtcars %>%
group_by(gear) %>%
summarise(foo())
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 3
#> gear group_rows full_data
#> <dbl> <int> <int>
#> 1 3 15 32
#> 2 4 12 32
#> 3 5 5 32
Created on 2020-12-09 by the reprex package (v0.3.0)