Skip to content

Commit 1e0bf61

Browse files
committed
Add complete.epi_df method so grouped completions don't drop epi_dfness
1 parent 69e9ca7 commit 1e0bf61

8 files changed

+124
-16
lines changed

NAMESPACE

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ S3method(as_tsibble,epi_df)
1111
S3method(autoplot,epi_df)
1212
S3method(clone,epi_archive)
1313
S3method(clone,grouped_epi_archive)
14+
S3method(complete,epi_df)
1415
S3method(dplyr_col_modify,col_modify_recorder_df)
1516
S3method(dplyr_col_modify,epi_df)
1617
S3method(dplyr_reconstruct,epi_df)
@@ -50,6 +51,7 @@ export(as_epi_df)
5051
export(as_tsibble)
5152
export(autoplot)
5253
export(clone)
54+
export(complete)
5355
export(detect_outlr)
5456
export(detect_outlr_rm)
5557
export(detect_outlr_stl)
@@ -64,6 +66,7 @@ export(epix_merge)
6466
export(epix_slide)
6567
export(epix_truncate_versions_after)
6668
export(filter)
69+
export(full_seq)
6770
export(geo_column_names)
6871
export(group_by)
6972
export(group_modify)
@@ -194,6 +197,8 @@ importFrom(stats,median)
194197
importFrom(tibble,as_tibble)
195198
importFrom(tibble,new_tibble)
196199
importFrom(tibble,validate_tibble)
200+
importFrom(tidyr,complete)
201+
importFrom(tidyr,full_seq)
197202
importFrom(tidyr,unnest)
198203
importFrom(tidyselect,any_of)
199204
importFrom(tidyselect,eval_select)

R/methods-epi_df.R

+16-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#' use `attr(your_epi_df, "decay_to_tibble") <- FALSE` beforehand.
99
#'
1010
#' @template x
11-
#' @param ... additional arguments to forward to `NextMethod()`
1211
#'
1312
#' @importFrom tibble as_tibble
1413
#' @export
@@ -48,9 +47,9 @@ as_tsibble.epi_df <- function(x, key, ...) {
4847
#' Print and summary functions for an `epi_df` object.
4948
#'
5049
#' @template x
51-
#' @param ... additional arguments to forward to `NextMethod()`
5250
#'
5351
#' @method print epi_df
52+
#' @param ... additional arguments to forward to `NextMethod()`, or unused
5453
#' @export
5554
print.epi_df <- function(x, ...) {
5655
cat(
@@ -76,7 +75,6 @@ print.epi_df <- function(x, ...) {
7675
#' Currently unused.
7776
#'
7877
#' @method summary epi_df
79-
#' @rdname print.epi_df
8078
#' @importFrom rlang .data
8179
#' @importFrom stats median
8280
#' @export
@@ -241,6 +239,21 @@ group_modify.epi_df <- function(.data, .f, ..., .keep = FALSE) {
241239
dplyr::dplyr_reconstruct(NextMethod(), .data)
242240
}
243241

242+
#' @importFrom tidyr complete
243+
#' @rdname print.epi_df
244+
#' @param data an `epi_df`
245+
#' @param fill see [`tidyr::complete`]
246+
#' @param explicit see [`tidyr::complete`]
247+
#' @method complete epi_df
248+
#' @export
249+
complete.epi_df <- function(data, ..., fill = list(), explicit = TRUE) {
250+
result <- dplyr::dplyr_reconstruct(NextMethod(), data)
251+
if ("time_value" %in% names(rlang::call_match(dots_expand = FALSE)[["..."]])) {
252+
attr(result, "metadata")$time_type <- guess_time_type(result$time_value)
253+
}
254+
result
255+
}
256+
244257
#' @method unnest epi_df
245258
#' @rdname print.epi_df
246259
#' @param data an `epi_df`

R/reexports.R

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ dplyr::slice
5757
tidyr::unnest
5858

5959

60+
#' @importFrom tidyr complete
61+
#' @export
62+
tidyr::complete
63+
64+
# We don't provide a method for full_seq, but complete-ing using
65+
# full_seq(time_value) is still needed to make some downstream things behave
66+
# nicely. So make that more ergonomic/discoverable with a re-export:
67+
68+
#' @importFrom tidyr full_seq
69+
#' @export
70+
tidyr::full_seq
71+
72+
6073
# ggplot2 -----------------------------------------------------------------
6174

6275
#' @importFrom ggplot2 autoplot

man/as_tibble.epi_df.Rd

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

man/print.epi_df.Rd

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

man/reexports.Rd

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

man/summary.epi_df.Rd

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

tests/testthat/test-methods-epi_df.R

+61
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,64 @@ test_that("Renaming columns while grouped gives appropriate colnames and metadat
177177
select(geo_value, time_value, age_group = age, value)
178178
expect_identical(renamed_gedf1, renamed_gedf2)
179179
})
180+
181+
test_that("complete.epi_df works", {
182+
start_date <- as.Date("2020-01-01")
183+
daily_edf <- tibble::tibble(
184+
geo_value = c(1, 2),
185+
time_value = start_date + c(1, 2),
186+
value = 1
187+
) %>%
188+
as_epi_df(as_of = start_date + 3)
189+
expect_identical(
190+
daily_edf %>%
191+
tidyr::complete(geo_value, time_value = full_seq(time_value, period = 1)),
192+
tibble::tibble(
193+
geo_value = c(1, 1, 2, 2),
194+
time_value = start_date + c(1, 2, 1, 2),
195+
value = c(1, NA, NA, 1)
196+
) %>%
197+
as_epi_df(as_of = start_date + 3)
198+
)
199+
expect_identical(
200+
daily_edf %>%
201+
group_by(geo_value) %>%
202+
tidyr::complete(time_value = full_seq(time_value, period = 1)),
203+
daily_edf %>%
204+
group_by(geo_value)
205+
)
206+
weekly_edf <- tibble::tibble(
207+
geo_value = c(1, 1, 1, 2),
208+
time_value = start_date + c(1, 15, 22, 1),
209+
value = 1
210+
) %>%
211+
as_epi_df(as_of = start_date + 3)
212+
expect_identical(
213+
weekly_edf %>%
214+
complete(geo_value,
215+
time_value = full_seq(time_value, period = 7),
216+
fill = list(value = 5)
217+
),
218+
tibble::tibble(
219+
geo_value = c(1, 1, 1, 1, 2, 2, 2, 2),
220+
time_value = start_date + c(1, 8, 15, 22, 1, 8, 15, 22),
221+
value = c(1, 5, 1, 1, 1, 5, 5, 5)
222+
) %>%
223+
as_epi_df(as_of = start_date + 3)
224+
)
225+
expect_identical(
226+
weekly_edf %>%
227+
group_by(geo_value) %>%
228+
complete(
229+
time_value = full_seq(time_value, period = 7),
230+
fill = list(value = 5)
231+
) %>%
232+
ungroup(),
233+
tibble::tibble(
234+
geo_value = c(1, 1, 1, 1, 2),
235+
time_value = start_date + c(1, 8, 15, 22, 1),
236+
value = c(1, 5, 1, 1, 1)
237+
) %>%
238+
as_epi_df(as_of = start_date + 3)
239+
)
240+
})

0 commit comments

Comments
 (0)