Skip to content

Commit 3aee83b

Browse files
committed
pub_covid_hosp_facility supports epiweeks and days
`pub_covid_hosp_facility` says and implies it takes weeks for the arg `collection_weeks`, but it actually only takes days (corresponding to weeks) and will error if given weeks. Given the misleading name and behavior, and differing behavior compared to other endpoints with weekly data, allow users to supply weeks -- these will be converted to dates.
1 parent eb685cf commit 3aee83b

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

R/endpoints.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,22 @@ pub_covid_hosp_facility_lookup <- function(
161161
#' hospital_pks = "100075",
162162
#' collection_weeks = epirange(20200101, 20200501)
163163
#' )
164+
#'
165+
#' pub_covid_hosp_facility(
166+
#' hospital_pks = "100075",
167+
#' collection_weeks = epirange(202001, 202005)
168+
#' )
164169
#' }
165170
#' @param hospital_pks character. Facility identifiers.
166-
#' @param collection_weeks [`timeset`]. Epiweeks to fetch. Defaults to all ("*") dates.
171+
#' @param collection_weeks [`timeset`]. Dates (corresponding to epiweeks) to
172+
#' fetch. Defaults to all ("*") dates.
167173
#' @param ... not used for values, forces later arguments to bind by name
168174
#' @param publication_dates [`timeset`]. Publication dates to fetch.
169175
#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`.
170176
#' @return [`tibble::tibble`]
171177
#'
178+
#' @importFrom checkmate test_class
179+
#'
172180
#' @seealso [`pub_covid_hosp_facility()`], [`epirange()`]
173181
#' @keywords endpoint
174182
#' @export
@@ -181,14 +189,21 @@ pub_covid_hosp_facility <- function(
181189
fetch_args = fetch_args_list()) {
182190
rlang::check_dots_empty()
183191

184-
collection_weeks <- get_wildcard_equivalent_dates(collection_weeks, "week")
192+
collection_weeks <- get_wildcard_equivalent_dates(collection_weeks, "day")
185193

186194
assert_character_param("hospital_pks", hospital_pks)
187195
assert_timeset_param("collection_weeks", collection_weeks)
188196
assert_timeset_param("publication_dates", publication_dates, required = FALSE)
189197
collection_weeks <- parse_timeset_input(collection_weeks)
190198
publication_dates <- parse_timeset_input(publication_dates)
191199

200+
# Confusingly, the endpoint expects `collection_weeks` to be in day format,
201+
# but correspond to epiweeks. Allow `collection_weeks` to be provided in
202+
# either day or week format.
203+
if (test_class(collection_weeks, "EpiRange")) {
204+
collection_weeks <- convert_epirange_format(collection_weeks, to_type = "day")
205+
}
206+
192207
create_epidata_call(
193208
"covid_hosp_facility/",
194209
list(
@@ -1047,6 +1062,7 @@ pub_covidcast <- function(
10471062
#' }
10481063
#' @param system character. System name to fetch.
10491064
#' @param epiweek [`timeset`]. Epiweek to fetch. Does not support multiple dates.
1065+
#' Make separate calls to fetch data for multiple epiweeks.
10501066
#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`.
10511067
#' @return [`list`]
10521068
#' @keywords endpoint

R/model.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,37 @@ epirange <- function(from, to) {
6161
structure(list(from = from, to = to), class = "EpiRange")
6262
}
6363

64+
#' helper to reformat an epirange from week to day or vice versa
65+
#'
66+
#' @keywords internal
67+
convert_epirange_format <- function(epirange, to_type = c("day", "week")) {
68+
to_type <- match.arg(to_type)
69+
70+
# Day format
71+
if (nchar(epirange$from) == 8) {
72+
if (to_type == "week") {
73+
from_components <- MMWRweek::MMWRweek(as.Date(as.character(epirange$from), "%Y%m%d"))
74+
to_components <- MMWRweek::MMWRweek(as.Date(as.character(epirange$to), "%Y%m%d"))
75+
76+
epirange$from <- as.numeric(paste0(
77+
from_components$MMWRyear,
78+
formatC(from_components$MMWRweek, width = 2, flag = 0)
79+
))
80+
epirange$to <- as.numeric(paste0(
81+
to_components$MMWRyear,
82+
formatC(to_components$MMWRweek, width = 2, flag = 0)
83+
))
84+
}
85+
# Week format
86+
} else {
87+
if (to_type == "day") {
88+
epirange$from <- parse_api_week(epirange$from)
89+
epirange$to <- parse_api_week(epirange$to)
90+
}
91+
}
92+
93+
return(epirange)
94+
}
6495

6596
#' Timeset formats for specifying dates
6697
#'

man/pub_covid_hosp_facility.Rd

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

man/pub_delphi.Rd

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

0 commit comments

Comments
 (0)