Skip to content

Commit 6a85f42

Browse files
committed
warn about week conversion; handle single week date
refactor reformat_epirange; add date_to_epiweek helper fn better collection_weeks message
1 parent 3aee83b commit 6a85f42

File tree

5 files changed

+68
-25
lines changed

5 files changed

+68
-25
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export(save_api_key)
4848
export(set_cache)
4949
import(cachem)
5050
import(glue)
51+
importFrom(MMWRweek,MMWRweek)
5152
importFrom(MMWRweek,MMWRweek2Date)
5253
importFrom(checkmate,assert)
5354
importFrom(checkmate,assert_character)

R/endpoints.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub_covid_hosp_facility_lookup <- function(
175175
#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`.
176176
#' @return [`tibble::tibble`]
177177
#'
178-
#' @importFrom checkmate test_class
178+
#' @importFrom checkmate test_class test_integerish test_character
179179
#'
180180
#' @seealso [`pub_covid_hosp_facility()`], [`epirange()`]
181181
#' @keywords endpoint
@@ -200,8 +200,19 @@ pub_covid_hosp_facility <- function(
200200
# Confusingly, the endpoint expects `collection_weeks` to be in day format,
201201
# but correspond to epiweeks. Allow `collection_weeks` to be provided in
202202
# either day or week format.
203-
if (test_class(collection_weeks, "EpiRange")) {
204-
collection_weeks <- convert_epirange_format(collection_weeks, to_type = "day")
203+
coercion_msg <- c(
204+
"`collection_weeks` is in week format but `pub_covid_hosp_facility`
205+
expects day format; dates will be converted to day format but may not
206+
correspond exactly to desired time range"
207+
)
208+
if (test_class(collection_weeks, "EpiRange") && nchar(collection_weeks$from) == 6) {
209+
cli::cli_warn(coercion_msg, class = "epidatr__epirange_week_coercion")
210+
collection_weeks <- reformat_epirange(collection_weeks, to_type = "day")
211+
# Single week date.
212+
} else if ((test_integerish(collection_weeks) || test_character(collection_weeks)) &&
213+
nchar(collection_weeks) == 6) {
214+
cli::cli_warn(coercion_msg, class = "epidatr__single_week_coercion")
215+
collection_weeks <- parse_api_week(collection_weeks)
205216
}
206217

207218
create_epidata_call(

R/model.R

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,17 @@ epirange <- function(from, to) {
6464
#' helper to reformat an epirange from week to day or vice versa
6565
#'
6666
#' @keywords internal
67-
convert_epirange_format <- function(epirange, to_type = c("day", "week")) {
67+
reformat_epirange <- function(epirange, to_type = c("day", "week")) {
6868
to_type <- match.arg(to_type)
6969

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-
}
70+
# Day format -> week
71+
if (nchar(epirange$from) == 8 && to_type == "week") {
72+
epirange$from <- date_to_epiweek(epirange$from)
73+
epirange$to <- date_to_epiweek(epirange$to)
74+
# Week format -> day
75+
} else if (nchar(epirange$from) == 6 && to_type == "day") {
76+
epirange$from <- parse_api_week(epirange$from)
77+
epirange$to <- parse_api_week(epirange$to)
9178
}
9279

9380
return(epirange)
@@ -203,6 +190,20 @@ parse_data_frame <- function(epidata_call, df, disable_date_parsing = FALSE) {
203190
df
204191
}
205192

193+
#' Converts a date (integer or character) to an epiweek
194+
#' @param value date (integer or character, with format YYYYMMDD) to be converted to an epiweek
195+
#' @return an integer representing an epiweek, in the format YYYYWW
196+
#' @importFrom MMWRweek MMWRweek
197+
#' @keywords internal
198+
date_to_epiweek <- function(value) {
199+
date_components <- MMWRweek::MMWRweek(as.Date(as.character(value), "%Y%m%d"))
200+
as.numeric(paste0(
201+
date_components$MMWRyear,
202+
# Pad with zeroes up to 2 digits (x -> 0x)
203+
formatC(date_components$MMWRweek, width = 2, flag = 0)
204+
))
205+
}
206+
206207
#' @keywords internal
207208
parse_api_date <- function(value) {
208209
as.Date(as.character(value), tryFormats = c("%Y%m%d", "%Y-%m-%d"))

man/date_to_epiweek.Rd

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

man/reformat_epirange.Rd

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

0 commit comments

Comments
 (0)