Skip to content

Commit

Permalink
Make as_epi_df remove grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
brookslogan committed Nov 26, 2024
1 parent 53505d3 commit 0352d7b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ S3method(arrange_row_canonical,default)
S3method(arrange_row_canonical,epi_df)
S3method(as_epi_df,data.frame)
S3method(as_epi_df,epi_df)
S3method(as_epi_df,grouped_df)
S3method(as_epi_df,tbl_df)
S3method(as_epi_df,tbl_ts)
S3method(as_tibble,epi_df)
Expand Down
17 changes: 15 additions & 2 deletions R/epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ NULL
#' @param other_keys If your tibble has additional keys, be sure to specify them
#' as a character vector here (typical examples are "age" or sub-geographies).
#' @param ... Additional arguments passed to methods.
#' @return An `epi_df` object.
#' @return * Of `new_epi_df()`: an `epi_df`
#'
#' @export
new_epi_df <- function(x = tibble::tibble(geo_value = character(), time_value = as.Date(integer())),
Expand Down Expand Up @@ -205,6 +205,8 @@ new_epi_df <- function(x = tibble::tibble(geo_value = character(), time_value =
#' to be converted
#' @param ... used for specifying column names, as in [`dplyr::rename`]. For
#' example, `geo_value = STATEFP, time_value = end_date`.
#' @return * Of `as_epi_df()`: an (ungrouped) `epi_df`
#'
#' @export
as_epi_df <- function(x, ...) {
UseMethod("as_epi_df")
Expand All @@ -215,6 +217,7 @@ as_epi_df <- function(x, ...) {
#' @method as_epi_df epi_df
#' @export
as_epi_df.epi_df <- function(x, ...) {
x <- ungroup(x)
return(x)
}

Expand Down Expand Up @@ -296,6 +299,14 @@ as_epi_df.tbl_df <- function(
new_epi_df(x, geo_type, time_type, as_of, other_keys)
}

#' @rdname epi_df
#' @order 1
#' @method as_epi_df grouped_df
#' @export
as_epi_df.grouped_df <- function(x, ...) {
as_epi_df(ungroup(x), ...)
}

#' @rdname epi_df
#' @order 1
#' @method as_epi_df data.frame
Expand All @@ -319,9 +330,11 @@ as_epi_df.tbl_ts <- function(x, as_of, other_keys = character(), ...) {
#' Test for `epi_df` format
#'
#' @param x An object.
#' @return `TRUE` if the object inherits from `epi_df`.
#' @return * Of `is_epi_df`: `TRUE` if the object inherits from `epi_df`,
#' otherwise `FALSE`.
#'
#' @rdname epi_df
#' @order 1
#' @export
is_epi_df <- function(x) {
inherits(x, "epi_df")
Expand Down
24 changes: 18 additions & 6 deletions man/epi_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-epi_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ test_that("as_epi_df works for nonstandard input", {
)
})

test_that("as_epi_df ungroups", {
expect_false(
tibble::tibble(geo_value = 1, time_value = 1) %>%
dplyr::group_by(geo_value) %>%
as_epi_df(as_of = 2) %>%
dplyr::is_grouped_df()
)
expect_false(
tibble::tibble(geo_value = 1, time_value = 1) %>%
as_epi_df(as_of = 2) %>%
dplyr::group_by(geo_value) %>%
as_epi_df(as_of = 2) %>%
dplyr::is_grouped_df()
)
})

# select fixes
tib <- tibble::tibble(
x = 1:10, y = 1:10,
Expand Down

0 comments on commit 0352d7b

Please sign in to comment.