Skip to content

Commit c97093c

Browse files
authored
Merge pull request #219 from cmu-delphi/ndefries/hosp-state-take_as_of
`pub_covid_hosp_state_timeseries` to use `as_of` parameter
2 parents 73ea679 + a4ca176 commit c97093c

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

R/endpoints.R

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,30 +459,55 @@ pub_covid_hosp_facility <- function(
459459
#'
460460
#' @param states character. Two letter state abbreviations.
461461
#' @param dates [`timeset`]. Dates to fetch.
462-
#' @param issues [`timeset`]. Optionally, the issues to fetch. If not set, the
463-
#' most recent issue is returned.
464462
#' @param ... not used for values, forces later arguments to bind by name
463+
#' @param as_of Date. Optionally, the as of date for the issues to fetch. If not
464+
#' specified, the most recent data is returned. Mutually exclusive with
465+
#' `issues`.
466+
#' @param issues [`timeset`]. Optionally, the issue of the data to fetch. If not
467+
#' specified, the most recent issue is returned. Mutually exclusive with
468+
#' `as_of` or `lag`.
465469
#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`.
466470
#' @return [`tibble::tibble`]
467471
#'
468472
#' @keywords endpoint
469473
#' @export
470474
#
471-
pub_covid_hosp_state_timeseries <- function(states, dates, ..., issues = NULL, fetch_args = fetch_args_list()) {
475+
pub_covid_hosp_state_timeseries <- function(
476+
states,
477+
dates,
478+
...,
479+
as_of = NULL,
480+
issues = NULL,
481+
fetch_args = fetch_args_list()) {
482+
# Check parameters
472483
rlang::check_dots_empty()
473484

485+
if (missing(states) || missing(dates)) {
486+
stop(
487+
"`states` and `dates` are both required"
488+
)
489+
}
490+
491+
if (sum(!is.null(issues), !is.null(as_of)) > 1) {
492+
stop("`issues`and `as_of` are mutually exclusive")
493+
}
494+
474495
assert_character_param("states", states)
475496
assert_timeset_param("dates", dates)
497+
assert_date_param("as_of", as_of, len = 1, required = FALSE)
476498
assert_timeset_param("issues", issues, required = FALSE)
499+
477500
dates <- parse_timeset_input(dates)
478501
issues <- parse_timeset_input(issues)
502+
as_of <- parse_timeset_input(as_of)
479503

480504
create_epidata_call(
481505
"covid_hosp_state_timeseries/",
482506
list(
483507
states = states,
484508
dates = dates,
485-
issues = issues
509+
issues = issues,
510+
as_of = as_of
486511
),
487512
list(
488513
create_epidata_field_info("state", "text"),

man/pub_covid_hosp_state_timeseries.Rd

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

tests/testthat/test-endpoints.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,40 @@ test_that("endpoints fail when given args via dots", {
248248
regexp = dots_error
249249
)
250250
})
251+
252+
test_that("pub_covid_hosp_state_timeseries supports versioned queries", {
253+
epidata_call <- pub_covid_hosp_state_timeseries(
254+
"ut", epirange(12340101, 34560101),
255+
issues = 20220101,
256+
fetch_args = fetch_args_list(
257+
fields = c(
258+
"state", "geocoded_state", "date", "issue",
259+
"previous_day_admission_influenza_confirmed",
260+
"previous_day_admission_influenza_confirmed_coverage"
261+
),
262+
disable_date_parsing = TRUE,
263+
dry_run = TRUE
264+
)
265+
)
266+
expect_identical(epidata_call$params$issues, 20220101)
267+
expect_identical(epidata_call$params$as_of, NULL)
268+
# COVID hosp state timeseries server code doesn't support `lag`
269+
expect_identical(epidata_call$params$lag, NULL)
270+
271+
epidata_call <- pub_covid_hosp_state_timeseries(
272+
"ut", epirange(12340101, 34560101),
273+
as_of = 20220101,
274+
fetch_args = fetch_args_list(
275+
fields = c(
276+
"state", "geocoded_state", "date", "issue",
277+
"previous_day_admission_influenza_confirmed",
278+
"previous_day_admission_influenza_confirmed_coverage"
279+
),
280+
disable_date_parsing = TRUE,
281+
dry_run = TRUE
282+
)
283+
)
284+
expect_identical(epidata_call$params$issues, NULL)
285+
expect_identical(epidata_call$params$as_of, 20220101)
286+
expect_identical(epidata_call$params$lag, NULL)
287+
})

0 commit comments

Comments
 (0)