Skip to content

Commit 8f36d44

Browse files
committed
Fix warnings&errors in examples&tests, keep archive_cases_dv in sync
- Fix unmuffled warnings and errors in the tests and examples - Introduce the S3 class `"epiprocess__snapshot_as_of_last_update_version"` to selectively muffle this type of warning. - Fix some things that only arise when running checks or examples from a fresh R session / one that does not have epiprocess, dplyr, and tibble loaded. - When re-building `archive_cases_dv`, use `compactify=FALSE` to not mess up the current tests. - Don't store `archive_cases_dv` directly, as its R6 implementation will be stored with it; instead, just store its DT and reconstruct the archive object when requested (use a promise and mark it to @export).
1 parent c042e1d commit 8f36d44

11 files changed

+69
-22
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ S3method(tail,epi_df)
2020
S3method(ungroup,epi_df)
2121
S3method(unnest,epi_df)
2222
export("%>%")
23+
export(archive_cases_dv)
2324
export(arrange)
2425
export(as_epi_archive)
2526
export(as_epi_df)

R/archive.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ epi_archive =
196196
DT = rm_locf(DT)
197197
} else {
198198
# Create empty data frame for nrow(elim) to be 0
199-
elim = tibble()
199+
elim = tibble::tibble()
200200
}
201201

202202
# Warns about redundant rows
@@ -280,7 +280,7 @@ epi_archive =
280280
Abort("`max_version` must be at most `max(DT$max_version)`.")
281281
}
282282
if (max_version == self_max) {
283-
Warn("Getting data as of the latest version possible. For a variety of reasons, it is possible that we only have a preliminary picture of this version (e.g., the upstream source has updated it but we have not seen it due to latency in synchronization). Thus, the snapshot that we produce here might not be reproducible at a later time (e.g., when the archive has caught up in terms of synchronization).")
283+
Warn("Getting data as of the latest version possible. For a variety of reasons, it is possible that we only have a preliminary picture of this version (e.g., the upstream source has updated it but we have not seen it due to latency in synchronization). Thus, the snapshot that we produce here might not be reproducible at a later time (e.g., when the archive has caught up in terms of synchronization).", class="epiprocess__snapshot_as_of_last_update_version")
284284
}
285285

286286
# Filter by version and return
@@ -525,7 +525,7 @@ epi_archive =
525525
#'
526526
#' @export
527527
#' @examples
528-
#' df <- data.frame (geo_value = c(replicate(2, "ca"), replicate(2, "fl")),
528+
#' df <- data.frame (geo_value = c(rep("ca", 2), rep("fl", 2)),
529529
#' county = c(1, 3, 2, 5),
530530
#' time_value = c("2020-06-01",
531531
#' "2020-06-02",

R/data.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@
4141
#'
4242
#' COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University.
4343
#' \url{https://github.com/CSSEGISandData/COVID-19}
44+
#'
45+
#' @export
4446
"archive_cases_dv"
4547

48+
# Like normal data objects, set archive_cases_dv up as a promise, so it doesn't
49+
# take unnecessary space. This also avoids a need for @include tags.
50+
delayedAssign("archive_cases_dv", as_epi_archive(archive_cases_dv_dt, compactify=FALSE))

R/methods-epi_archive.R

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,25 @@
2727
#' ```
2828
#'
2929
#' @export
30-
#' @examples
31-
#' epix_as_of(x = archive_cases_dv,
32-
#' max_version = max(archive_cases_dv$DT$version))
30+
#' @examples
31+
#'
32+
#' range(archive_cases_dv$DT$version) # 2020-06-02 -- 2020-06-15
33+
#'
34+
#' epix_as_of(x = archive_cases_dv,
35+
#' max_version = as.Date("2020-06-12"))
36+
#'
37+
#' # When fetching a snapshot as of the latest version with update data in the
38+
#' # archive, a warning is issued as this update data might not yet be finalized
39+
#' # (for example, if data versions are labeled with dates, these versions might be
40+
#' # overwritten throughout the day if the data can be updated multiple times per
41+
#' # day; when we build an archive based on special update-data queries all made at
42+
#' # the same time, the latest available update might still be subject to change,
43+
#' # but previous versions should be finalized). We can muffle such warnings with
44+
#' # the following pattern:
45+
#' withCallingHandlers({
46+
#' epix_as_of(x = archive_cases_dv,
47+
#' max_version = max(archive_cases_dv$DT$version))
48+
#' }, epiprocess__snapshot_as_of_last_update_version = function(wrn) invokeRestart("muffleWarning"))
3349
epix_as_of = function(x, max_version, min_time_value = -Inf) {
3450
if (!inherits(x, "epi_archive")) Abort("`x` must be of class `epi_archive`.")
3551
return(x$as_of(max_version, min_time_value))
@@ -74,10 +90,10 @@ epix_as_of = function(x, max_version, min_time_value = -Inf) {
7490
#' # create two example epi_archive datasets
7591
#' x <- archive_cases_dv$DT %>%
7692
#' dplyr::select(geo_value,time_value,version,case_rate) %>%
77-
#' as_epi_archive()
93+
#' as_epi_archive(compactify=TRUE)
7894
#' y <- archive_cases_dv$DT %>%
7995
#' dplyr::select(geo_value,time_value,version,percent_cli) %>%
80-
#' as_epi_archive()
96+
#' as_epi_archive(compactify=TRUE)
8197
#'
8298
#' # a full join stored in x
8399
#' epix_merge(x, y, all = TRUE)

R/sysdata.rda

2.14 KB
Binary file not shown.

data-raw/archive_cases_dv.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ archive_cases_dv <- covidcast(
88
signals = "smoothed_adj_cli",
99
time_type = "day",
1010
geo_type = "state",
11-
time_value = epirange(20200601, 20200615),
11+
time_values = epirange(20200601, 20200615),
1212
geo_values = "ca,fl",
1313
issues = epirange(20200601, 20200615)
1414
) %>%
1515
fetch_tbl() %>%
1616
select(geo_value, time_value, version = issue, percent_cli = value) %>%
17-
as_epi_archive(compactify=TRUE)
17+
as_epi_archive(compactify=FALSE)
1818

1919
case_rate <- covidcast(
2020
data_source = "jhu-csse",
2121
signals = "confirmed_7dav_incidence_prop",
2222
time_type = "day",
2323
geo_type = "state",
24-
time_value = epirange(20200601, 20200615),
24+
time_values = epirange(20200601, 20200615),
2525
geo_values = "ca,fl",
2626
issues = epirange(20200601, 20200615)
2727
) %>%
@@ -31,4 +31,11 @@ case_rate <- covidcast(
3131

3232
epix_merge(archive_cases_dv, case_rate, all = TRUE)
3333

34-
usethis::use_data(archive_cases_dv, overwrite = TRUE)
34+
# If we directly store an epi_archive R6 object as data, it will store its class
35+
# implementation there as well. To prevent mismatches between these stored
36+
# implementations and the latest class definition, don't store them as R6
37+
# objects; store the DT and construct the R6 object on request.
38+
39+
archive_cases_dv_dt = archive_cases_dv$DT
40+
41+
usethis::use_data(archive_cases_dv_dt, overwrite = TRUE, internal=TRUE)

data/archive_cases_dv.rda

-39.5 KB
Binary file not shown.

man/as_epi_archive.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/epix_as_of.Rd

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/epix_merge.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-compactify.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dt <- row_replace(74,73,74) # Not LOCF
4848

4949
dt_true <- as_tibble(as_epi_archive(dt,compactify=TRUE)$DT)
5050
dt_false <- as_tibble(as_epi_archive(dt,compactify=FALSE)$DT)
51-
dt_null <- as_tibble(as_epi_archive(dt,compactify=NULL)$DT)
51+
dt_null <- suppressWarnings(as_tibble(as_epi_archive(dt,compactify=NULL)$DT))
5252

5353
test_that("Warning for LOCF with compactify as NULL", {
5454
expect_warning(as_epi_archive(dt,compactify=NULL))
@@ -73,12 +73,14 @@ test_that("as_of utilizes LOCF even after removal of LOCF values",{
7373
ea_true <- as_epi_archive(dt,compactify=TRUE)
7474
ea_false <- as_epi_archive(dt,compactify=FALSE)
7575

76-
epix_as_of(ea_true,max(ea_true$DT$version))
77-
7876
# Row 22, an LOCF row corresponding to the latest version, but for the
7977
# date 2020-06-02, is omitted in ea_true
80-
as_of_true <- ea_true$as_of(max(ea_true$DT$version))
81-
as_of_false <- ea_false$as_of(max(ea_false$DT$version))
78+
expect_warning({
79+
as_of_true <- ea_true$as_of(max(ea_true$DT$version))
80+
}, class = "epiprocess__snapshot_as_of_last_update_version")
81+
expect_warning({
82+
as_of_false <- ea_false$as_of(max(ea_false$DT$version))
83+
}, class = "epiprocess__snapshot_as_of_last_update_version")
8284

8385
expect_identical(as_of_true,as_of_false)
8486
})

0 commit comments

Comments
 (0)