Skip to content

Commit 0919992

Browse files
committed
refactor reformat_epirange; add date_to_epiweek helper fn
1 parent d491a20 commit 0919992

File tree

5 files changed

+58
-42
lines changed

5 files changed

+58
-42
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export(save_api_key)
4848
export(set_cache)
4949
import(cachem)
5050
import(glue)
51+
importFrom(MMWRweek,MMWRweek)
5152
importFrom(MMWRweek,MMWRweek2Date)
53+
importFrom(MMWRweek,MMWRyear)
5254
importFrom(checkmate,assert)
5355
importFrom(checkmate,assert_character)
5456
importFrom(checkmate,assert_integerish)
@@ -65,6 +67,7 @@ importFrom(checkmate,test_class)
6567
importFrom(checkmate,test_date)
6668
importFrom(checkmate,test_integerish)
6769
importFrom(checkmate,test_list)
70+
importFrom(glue,glue)
6871
importFrom(httr,RETRY)
6972
importFrom(httr,content)
7073
importFrom(httr,http_error)

R/endpoints.R

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ pub_covid_hosp_facility_lookup <- function(
176176
#' @return [`tibble::tibble`]
177177
#'
178178
#' @importFrom checkmate test_class test_integerish test_character
179+
#' @importFrom glue glue
179180
#'
180181
#' @seealso [`pub_covid_hosp_facility()`], [`epirange()`]
181182
#' @keywords endpoint
@@ -200,27 +201,20 @@ pub_covid_hosp_facility <- function(
200201
# Confusingly, the endpoint expects `collection_weeks` to be in day format,
201202
# but correspond to epiweeks. Allow `collection_weeks` to be provided in
202203
# either day or week format.
203-
if (test_class(collection_weeks, "EpiRange") && nchar(collection_weeks$to) == 6) {
204-
cli::cli_warn(
205-
c(
206-
"`collection_weeks` {collection_weeks} is in week format but
207-
`pub_covid_hosp_facility` expects day format; dates will be automatically
208-
converted to day format"
209-
),
210-
class = "epidatr__epirange_week_coercion"
204+
coercion_msg <- glue::glue(
205+
c(
206+
"`collection_weeks` {collection_weeks} is in week format but
207+
`pub_covid_hosp_facility` expects day format; dates will be automatically
208+
converted to day format"
211209
)
212-
collection_weeks <- convert_epirange_format(collection_weeks, to_type = "day")
210+
)
211+
if (test_class(collection_weeks, "EpiRange") && nchar(collection_weeks$from) == 6) {
212+
cli::cli_warn(coercion_msg, class = "epidatr__epirange_week_coercion")
213+
collection_weeks <- reformat_epirange(collection_weeks, to_type = "day")
213214
# Single week date.
214215
} else if ((test_integerish(collection_weeks) || test_character(collection_weeks)) &&
215216
nchar(collection_weeks) == 6) {
216-
cli::cli_warn(
217-
c(
218-
"`collection_weeks` {collection_weeks} is in week format but
219-
`pub_covid_hosp_facility` expects day format; dates will be automatically
220-
converted to day format"
221-
),
222-
class = "epidatr__single_week_coercion"
223-
)
217+
cli::cli_warn(coercion_msg, class = "epidatr__single_week_coercion")
224218
collection_weeks <- parse_api_week(collection_weeks)
225219
}
226220

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 MMWRyear 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/convert_epirange_format.Rd renamed to man/reformat_epirange.Rd

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

0 commit comments

Comments
 (0)