Skip to content

patch: get select working with grouping #390

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

Merged
merged 10 commits into from
Jan 16, 2024
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Collate:
'data.R'
'epi_df.R'
'epiprocess.R'
'group_by_epi_df_methods.R'
'methods-epi_archive.R'
'grouped_epi_archive.R'
'growth_rate.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ S3method(groups,grouped_epi_archive)
S3method(next_after,Date)
S3method(next_after,integer)
S3method(print,epi_df)
S3method(select,epi_df)
S3method(summary,epi_df)
S3method(ungroup,epi_df)
S3method(ungroup,grouped_epi_archive)
Expand Down
10 changes: 10 additions & 0 deletions R/group_by_epi_df_methods.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#' @importFrom dplyr select
#' @export
select.epi_df <- function(.data, ...) {
selected <- NextMethod(.data)
return(dplyr_reconstruct(selected, .data))
}

# others to consider:
# - arrange
# -
22 changes: 19 additions & 3 deletions tests/testthat/test-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ test_that("as_epi_df errors when additional_metadata is not a list", {
pol = rep(c("blue", "swing", "swing"), each = 2))

expect_error(
as_epi_df(ex_input, additional_metadata = c(other_keys = "state", "pol")),
"`additional_metadata` must be a list type.")
})
as_epi_df(ex_input, additional_metadata = c(other_keys = "state", "pol")),
"`additional_metadata` must be a list type."
)
})


test_that("grouped epi_df maintains type for select", {
tib <- tibble::tibble(
x = 1:10, y = 1:10,
time_value = rep(seq(as.Date("2020-01-01"), by = 1, length.out = 5), times = 2),
geo_value = rep(c("ca", "hi"), each = 5)
)

epi_tib <- epiprocess::new_epi_df(tib)
epi_tib
grouped_epi <- epi_tib %>% group_by(geo_value)
selected_df <- grouped_epi %>% select(-y)
expect_true("epi_df" %in% class(selected_df))
})