From 0352d7b31e99045ce63f5cabadb1e836a5fefcc0 Mon Sep 17 00:00:00 2001 From: "Logan C. Brooks" Date: Tue, 26 Nov 2024 03:47:21 -0800 Subject: [PATCH] Make `as_epi_df` remove grouping --- NAMESPACE | 1 + R/epi_df.R | 17 +++++++++++++++-- man/epi_df.Rd | 24 ++++++++++++++++++------ tests/testthat/test-epi_df.R | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index e044739b..e214d8f3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/epi_df.R b/R/epi_df.R index 5cf379e2..83cca073 100644 --- a/R/epi_df.R +++ b/R/epi_df.R @@ -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())), @@ -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") @@ -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) } @@ -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 @@ -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") diff --git a/man/epi_df.Rd b/man/epi_df.Rd index 4c592ab7..a6782718 100644 --- a/man/epi_df.Rd +++ b/man/epi_df.Rd @@ -4,12 +4,13 @@ \alias{as_epi_df} \alias{as_epi_df.epi_df} \alias{as_epi_df.tbl_df} +\alias{as_epi_df.grouped_df} \alias{as_epi_df.data.frame} \alias{as_epi_df.tbl_ts} +\alias{is_epi_df} \alias{new_epi_df} \alias{epi_df} -\alias{is_epi_df} -\title{\code{epi_df} object} +\title{Test for \code{epi_df} format} \usage{ as_epi_df(x, ...) @@ -24,10 +25,14 @@ as_epi_df(x, ...) ... ) +\method{as_epi_df}{grouped_df}(x, ...) + \method{as_epi_df}{data.frame}(x, as_of, other_keys = character(), ...) \method{as_epi_df}{tbl_ts}(x, as_of, other_keys = character(), ...) +is_epi_df(x) + new_epi_df( x = tibble::tibble(geo_value = character(), time_value = as.Date(integer())), geo_type, @@ -36,8 +41,6 @@ new_epi_df( other_keys = character(), ... ) - -is_epi_df(x) } \arguments{ \item{x}{An object.} @@ -65,9 +68,18 @@ then the current day-time will be used.} as a character vector here (typical examples are "age" or sub-geographies).} } \value{ -An \code{epi_df} object. +\itemize{ +\item Of \code{as_epi_df()}: an (ungrouped) \code{epi_df} +} -\code{TRUE} if the object inherits from \code{epi_df}. +\itemize{ +\item Of \code{is_epi_df}: \code{TRUE} if the object inherits from \code{epi_df}, +otherwise \code{FALSE}. +} + +\itemize{ +\item Of \code{new_epi_df()}: an \code{epi_df} +} } \description{ One of the two main data structures for storing time series in \code{epiprocess}. diff --git a/tests/testthat/test-epi_df.R b/tests/testthat/test-epi_df.R index 297d68df..44bb62e2 100644 --- a/tests/testthat/test-epi_df.R +++ b/tests/testthat/test-epi_df.R @@ -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,