Skip to content

Commit 99d3e1c

Browse files
authored
Merge pull request #471 from cmu-delphi/djm/as_tibble-attr
Djm/as tibble attr
2 parents 9e9a4a1 + 1cf296e commit 99d3e1c

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: epiprocess
33
Title: Tools for basic signal processing in epidemiology
4-
Version: 0.7.11
4+
Version: 0.7.12
55
Authors@R: c(
66
person("Jacob", "Bien", role = "ctb"),
77
person("Logan", "Brooks", email = "[email protected]", role = c("aut", "cre")),

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.x.y will indicat
3939
## Cleanup
4040
- Resolved some linting messages in package checks (#468).
4141

42+
## Cleanup
43+
- Added optional `decay_to_tibble` attribute controlling `as_tibble()` behavior
44+
of `epi_df`s to let `{epipredict}` work more easily with other libraries (#471).
45+
4246
# epiprocess 0.7.0
4347

4448
## Breaking changes:

R/methods-epi_df.R

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#' Converts an `epi_df` object into a tibble, dropping metadata and any
44
#' grouping.
55
#'
6+
#' Advanced: if you are working with a third-party package that uses
7+
#' `as_tibble()` on `epi_df`s but you actually want them to remain `epi_df`s,
8+
#' use `attr(your_epi_df, "decay_to_tibble") <- FALSE` beforehand.
9+
#'
610
#' @template x
711
#' @param ... additional arguments to forward to `NextMethod()`
812
#'
@@ -12,7 +16,11 @@ as_tibble.epi_df <- function(x, ...) {
1216
# Decaying drops the class and metadata. `as_tibble.grouped_df` drops the
1317
# grouping and should be called by `NextMethod()` in the current design.
1418
# See #223 for discussion of alternatives.
15-
decay_epi_df(NextMethod())
19+
if (attr(x, "decay_to_tibble") %||% TRUE) {
20+
return(decay_epi_df(NextMethod()))
21+
}
22+
metadata <- attr(x, "metadata")
23+
reclass(NextMethod(), metadata)
1624
}
1725

1826
#' Convert to tsibble format
@@ -52,6 +60,8 @@ print.epi_df <- function(x, ...) {
5260
cat(sprintf("* %-9s = %s\n", "geo_type", attributes(x)$metadata$geo_type))
5361
cat(sprintf("* %-9s = %s\n", "time_type", attributes(x)$metadata$time_type))
5462
cat(sprintf("* %-9s = %s\n", "as_of", attributes(x)$metadata$as_of))
63+
# Conditional output (silent if attribute is NULL):
64+
cat(sprintf("* %-9s = %s\n", "decay_to_tibble", attr(x, "decay_to_tibble")))
5565
cat("\n")
5666
NextMethod()
5767
}

man/as_tibble.epi_df.Rd

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

tests/testthat/test-as_tibble-decay.R

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
test_that("as_tibble checks an attr to avoid decay to tibble", {
2+
edf <- jhu_csse_daily_subset
3+
expect_identical(class(as_tibble(edf)), c("tbl_df", "tbl", "data.frame"))
4+
attr(edf, "decay_to_tibble") <- TRUE
5+
expect_identical(class(as_tibble(edf)), c("tbl_df", "tbl", "data.frame"))
6+
attr(edf, "decay_to_tibble") <- FALSE
7+
expect_identical(class(as_tibble(edf)), c("epi_df", "tbl_df", "tbl", "data.frame"))
8+
})
9+
10+
test_that("as_tibble ungroups if needed", {
11+
edf <- jhu_csse_daily_subset %>% group_by(geo_value)
12+
# removes the grouped_df class
13+
expect_identical(class(as_tibble(edf)), c("tbl_df", "tbl", "data.frame"))
14+
attr(edf, "decay_to_tibble") <- TRUE
15+
expect_identical(class(as_tibble(edf)), c("tbl_df", "tbl", "data.frame"))
16+
attr(edf, "decay_to_tibble") <- FALSE
17+
# removes grouped_df but not `epi_df`
18+
expect_identical(class(as_tibble(edf)), c("epi_df", "tbl_df", "tbl", "data.frame"))
19+
})

0 commit comments

Comments
 (0)