Skip to content

Commit 0352d7b

Browse files
committed
Make as_epi_df remove grouping
1 parent 53505d3 commit 0352d7b

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ S3method(arrange_row_canonical,default)
1111
S3method(arrange_row_canonical,epi_df)
1212
S3method(as_epi_df,data.frame)
1313
S3method(as_epi_df,epi_df)
14+
S3method(as_epi_df,grouped_df)
1415
S3method(as_epi_df,tbl_df)
1516
S3method(as_epi_df,tbl_ts)
1617
S3method(as_tibble,epi_df)

R/epi_df.R

+15-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ NULL
174174
#' @param other_keys If your tibble has additional keys, be sure to specify them
175175
#' as a character vector here (typical examples are "age" or sub-geographies).
176176
#' @param ... Additional arguments passed to methods.
177-
#' @return An `epi_df` object.
177+
#' @return * Of `new_epi_df()`: an `epi_df`
178178
#'
179179
#' @export
180180
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 =
205205
#' to be converted
206206
#' @param ... used for specifying column names, as in [`dplyr::rename`]. For
207207
#' example, `geo_value = STATEFP, time_value = end_date`.
208+
#' @return * Of `as_epi_df()`: an (ungrouped) `epi_df`
209+
#'
208210
#' @export
209211
as_epi_df <- function(x, ...) {
210212
UseMethod("as_epi_df")
@@ -215,6 +217,7 @@ as_epi_df <- function(x, ...) {
215217
#' @method as_epi_df epi_df
216218
#' @export
217219
as_epi_df.epi_df <- function(x, ...) {
220+
x <- ungroup(x)
218221
return(x)
219222
}
220223

@@ -296,6 +299,14 @@ as_epi_df.tbl_df <- function(
296299
new_epi_df(x, geo_type, time_type, as_of, other_keys)
297300
}
298301

302+
#' @rdname epi_df
303+
#' @order 1
304+
#' @method as_epi_df grouped_df
305+
#' @export
306+
as_epi_df.grouped_df <- function(x, ...) {
307+
as_epi_df(ungroup(x), ...)
308+
}
309+
299310
#' @rdname epi_df
300311
#' @order 1
301312
#' @method as_epi_df data.frame
@@ -319,9 +330,11 @@ as_epi_df.tbl_ts <- function(x, as_of, other_keys = character(), ...) {
319330
#' Test for `epi_df` format
320331
#'
321332
#' @param x An object.
322-
#' @return `TRUE` if the object inherits from `epi_df`.
333+
#' @return * Of `is_epi_df`: `TRUE` if the object inherits from `epi_df`,
334+
#' otherwise `FALSE`.
323335
#'
324336
#' @rdname epi_df
337+
#' @order 1
325338
#' @export
326339
is_epi_df <- function(x) {
327340
inherits(x, "epi_df")

man/epi_df.Rd

+18-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-epi_df.R

+16
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ test_that("as_epi_df works for nonstandard input", {
7575
)
7676
})
7777

78+
test_that("as_epi_df ungroups", {
79+
expect_false(
80+
tibble::tibble(geo_value = 1, time_value = 1) %>%
81+
dplyr::group_by(geo_value) %>%
82+
as_epi_df(as_of = 2) %>%
83+
dplyr::is_grouped_df()
84+
)
85+
expect_false(
86+
tibble::tibble(geo_value = 1, time_value = 1) %>%
87+
as_epi_df(as_of = 2) %>%
88+
dplyr::group_by(geo_value) %>%
89+
as_epi_df(as_of = 2) %>%
90+
dplyr::is_grouped_df()
91+
)
92+
})
93+
7894
# select fixes
7995
tib <- tibble::tibble(
8096
x = 1:10, y = 1:10,

0 commit comments

Comments
 (0)