Skip to content

Commit f8ba9f8

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

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-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")
@@ -232,6 +234,7 @@ as_epi_df.tbl_df <- function(
232234
as_of,
233235
other_keys = character(),
234236
...) {
237+
x <- ungroup(x)
235238
x <- rename(x, ...)
236239
x <- guess_column_name(x, "time_value", time_column_names())
237240
x <- guess_column_name(x, "geo_value", geo_column_names())
@@ -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

+18
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ 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+
# TODO revisit the following; this is not how dplyr:::as_tibble.grouped_df
86+
# works (without tsibble:::as_tibble.grouped_df overriding):
87+
expect_true(
88+
tibble::tibble(geo_value = 1, time_value = 1) %>%
89+
as_epi_df(as_of = 2) %>%
90+
dplyr::group_by(geo_value) %>%
91+
as_epi_df(as_of = 2) %>%
92+
dplyr::is_grouped_df()
93+
)
94+
})
95+
7896
# select fixes
7997
tib <- tibble::tibble(
8098
x = 1:10, y = 1:10,

0 commit comments

Comments
 (0)