Skip to content

Commit

Permalink
fix: port @examples, add archive_cases_dv_subset_2
Browse files Browse the repository at this point in the history
  • Loading branch information
dshemetov committed Mar 18, 2024
1 parent dfd0105 commit 03df26e
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 85 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ S3method(ungroup,grouped_epi_archive2)
S3method(unnest,epi_df)
export("%>%")
export(archive_cases_dv_subset)
export(archive_cases_dv_subset_2)
export(arrange)
export(as_epi_archive)
export(as_epi_archive2)
Expand Down
26 changes: 13 additions & 13 deletions R/archive_new.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ next_after.Date <- function(x) x + 1L
#' value = rnorm(10, mean = 2, sd = 1)
#' )
#'
#' toy_epi_archive <- tib %>% epi_archive$new(
#' toy_epi_archive <- tib %>% new_epi_archive2(
#' geo_type = "state",
#' time_type = "day"
#' )
Expand Down Expand Up @@ -743,16 +743,16 @@ merge_epi_archive2 <- function(
#'
#' @examples
#'
#' grouped_archive <- archive_cases_dv_subset %>% group_by(geo_value)
#' grouped_archive <- archive_cases_dv_subset_2 %>% group_by(geo_value)
#'
#' # `print` for metadata and method listing:
#' grouped_archive %>% print()
#'
#' # The primary use for grouping is to perform a grouped `epix_slide`:
#'
#' archive_cases_dv_subset %>%
#' archive_cases_dv_subset_2 %>%
#' group_by(geo_value) %>%
#' epix_slide(
#' epix_slide2(
#' f = ~ mean(.x$case_rate_7d_av),
#' before = 2,
#' ref_time_values = as.Date("2020-06-11") + 0:2,
Expand All @@ -778,7 +778,7 @@ merge_epi_archive2 <- function(
#' time_value = as.Date(time_value),
#' version = as.Date(version)
#' ) %>%
#' as_epi_archive(other_keys = "age_group")
#' as_epi_archive2(other_keys = "age_group")
#'
#' # The following are equivalent:
#' toy_archive %>% group_by(geo_value, age_group)
Expand All @@ -801,7 +801,7 @@ merge_epi_archive2 <- function(
#'
#' toy_archive %>%
#' group_by(geo_value, age_group, .drop = FALSE) %>%
#' epix_slide(f = ~ sum(.x$value), before = 20) %>%
#' epix_slide2(f = ~ sum(.x$value), before = 20) %>%
#' ungroup()
#'
#' @importFrom dplyr group_by
Expand Down Expand Up @@ -1030,7 +1030,7 @@ slide.epi_archive2 <- function(epi_archive, f, ..., before, ref_time_values,
#' value = rnorm(10, mean = 2, sd = 1)
#' )
#'
#' toy_epi_archive <- tib %>% as_epi_archive(
#' toy_epi_archive <- tib %>% as_epi_archive2(
#' geo_type = "state",
#' time_type = "day"
#' )
Expand All @@ -1056,7 +1056,7 @@ slide.epi_archive2 <- function(epi_archive, f, ..., before, ref_time_values,
#' cases_rate = c(0.01, 0.02, 0.01, 0.05)
#' )
#'
#' x <- df %>% as_epi_archive(
#' x <- df %>% as_epi_archive2(
#' geo_type = "state",
#' time_type = "day",
#' other_keys = "county"
Expand All @@ -1081,15 +1081,15 @@ as_epi_archive2 <- function(x, geo_type, time_type, other_keys,
#'
#' @export
#' @examples
#' is_epi_archive(jhu_csse_daily_subset) # FALSE (this is an epi_df, not epi_archive)
#' is_epi_archive(archive_cases_dv_subset) # TRUE
#' is_epi_archive2(jhu_csse_daily_subset) # FALSE (this is an epi_df, not epi_archive)
#' is_epi_archive2(archive_cases_dv_subset_2) # TRUE
#'
#' # By default, grouped_epi_archives don't count as epi_archives, as they may
#' # support a different set of operations from regular `epi_archives`. This
#' # behavior can be controlled by `grouped_okay`.
#' grouped_archive <- archive_cases_dv_subset$group_by(geo_value)
#' is_epi_archive(grouped_archive) # FALSE
#' is_epi_archive(grouped_archive, grouped_okay = TRUE) # TRUE
#' grouped_archive <- archive_cases_dv_subset_2 %>% group_by(geo_value)
#' is_epi_archive2(grouped_archive) # FALSE
#' is_epi_archive2(grouped_archive, grouped_okay = TRUE) # TRUE
#'
#' @seealso [`is_grouped_epi_archive`]
is_epi_archive2 <- function(x, grouped_okay = FALSE) {
Expand Down
8 changes: 8 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,11 @@ delayed_assign_with_unregister_awareness(
#' * Furthermore, the data has been limited to a very small number of rows, the
#' signal names slightly altered, and formatted into a tibble.
"jhu_csse_county_level_subset"

#' @export
"archive_cases_dv_subset_2"

delayed_assign_with_unregister_awareness(
"archive_cases_dv_subset_2",
as_epi_archive2(archive_cases_dv_subset_dt, compactify = FALSE)
)
44 changes: 20 additions & 24 deletions R/methods-epi_archive_new.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@
#'
#' @examples
#' # warning message of data latency shown
#' epix_as_of(
#' x = archive_cases_dv_subset,
#' max_version = max(archive_cases_dv_subset$DT$version)
#' epix_as_of2(
#' archive_cases_dv_subset_2,
#' max_version = max(archive_cases_dv_subset_2$DT$version)
#' )
#'
#' @examples
#'
#' range(archive_cases_dv_subset$DT$version) # 2020-06-02 -- 2021-12-01
#' range(archive_cases_dv_subset_2$DT$version) # 2020-06-02 -- 2021-12-01
#'
#' epix_as_of(
#' x = archive_cases_dv_subset,
#' epix_as_of2(
#' archive_cases_dv_subset_2,
#' max_version = as.Date("2020-06-12")
#' )
#'
Expand All @@ -65,9 +63,9 @@
#' # muffle such warnings with the following pattern:
#' withCallingHandlers(
#' {
#' epix_as_of(
#' x = archive_cases_dv_subset,
#' max_version = max(archive_cases_dv_subset$DT$version)
#' epix_as_of2(
#' archive_cases_dv_subset_2,
#' max_version = max(archive_cases_dv_subset_2$DT$version)
#' )
#' },
#' epiprocess__snapshot_as_of_clobberable_version = function(wrn) invokeRestart("muffleWarning")
Expand Down Expand Up @@ -164,16 +162,14 @@ epix_fill_through_version2 <- function(epi_archive, fill_versions_end,
#'
#' @examples
#' # create two example epi_archive datasets
#' x <- archive_cases_dv_subset$DT %>%
#' x <- archive_cases_dv_subset_2$DT %>%
#' dplyr::select(geo_value, time_value, version, case_rate_7d_av) %>%
#' as_epi_archive(compactify = TRUE)
#' y <- archive_cases_dv_subset$DT %>%
#' as_epi_archive2(compactify = TRUE)
#' y <- archive_cases_dv_subset_2$DT %>%
#' dplyr::select(geo_value, time_value, version, percent_cli) %>%
#' as_epi_archive(compactify = TRUE)
#' as_epi_archive2(compactify = TRUE)
#' # merge results stored in a third object:
#' xy <- epix_merge(x, y)
#' # vs. mutating x to hold the merge result:
#' x$merge(y)
#' xy <- epix_merge2(x, y)
#'
#' @importFrom data.table key set setkeyv
#' @export
Expand Down Expand Up @@ -703,9 +699,9 @@ epix_detailed_restricted_mutate2 <- function(.data, ...) {
#'
#' # A simple (but not very useful) example (see the archive vignette for a more
#' # realistic one):
#' archive_cases_dv_subset %>%
#' archive_cases_dv_subset_2 %>%
#' group_by(geo_value) %>%
#' epix_slide(
#' epix_slide2(
#' f = ~ mean(.x$case_rate_7d_av),
#' before = 2,
#' ref_time_values = ref_time_values,
Expand All @@ -727,9 +723,9 @@ epix_detailed_restricted_mutate2 <- function(.data, ...) {
#'
#' # Examining characteristics of the data passed to each computation with
#' # `all_versions=FALSE`.
#' archive_cases_dv_subset %>%
#' archive_cases_dv_subset_2 %>%
#' group_by(geo_value) %>%
#' epix_slide(
#' epix_slide2(
#' function(x, gk, rtv) {
#' tibble(
#' time_range = if (nrow(x) == 0L) {
Expand All @@ -756,9 +752,9 @@ epix_detailed_restricted_mutate2 <- function(.data, ...) {
#' # to each computation. In this case, each computation should expect an
#' # `epi_archive` containing the relevant version data:
#'
#' archive_cases_dv_subset %>%
#' archive_cases_dv_subset_2 %>%
#' group_by(geo_value) %>%
#' epix_slide(
#' epix_slide2(
#' function(x, gk, rtv) {
#' tibble(
#' versions_start = if (nrow(x$DT) == 0L) {
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
4 changes: 2 additions & 2 deletions man/as_epi_archive2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/epi_archive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions man/epix_as_of2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions man/epix_merge2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/epix_slide2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/group_by.epi_archive2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/is_epi_archive2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-archive_new.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("first input must be a data.frame", {
)
})

dt <- archive_cases_dv_subset$DT
dt <- archive_cases_dv_subset_2$DT

test_that("data.frame must contain geo_value, time_value and version columns", {
expect_error(as_epi_archive2(select(dt, -geo_value), compactify = FALSE),
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-compactify.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ library(epiprocess)
library(data.table)
library(dplyr)

dt <- archive_cases_dv_subset$DT
dt <- archive_cases_dv_subset_2$DT
dt <- filter(dt, geo_value == "ca") %>%
filter(version <= "2020-06-15") %>%
select(-case_rate_7d_av)
Expand Down
Loading

0 comments on commit 03df26e

Please sign in to comment.