Skip to content

Commit 6995fd7

Browse files
committed
enh: add reference_week_day arg to fetch_args_list
1 parent 735be8c commit 6995fd7

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

R/epidatacall.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ print.epidata_call <- function(x, ...) {
164164
#' @param format_type the format to request from the API, one of classic, json,
165165
#' csv; this is only used by `fetch_debug`, and by default is `"json"`
166166
#' @param refresh_cache if `TRUE`, ignore the cache, fetch the data from the
167-
#' API, and update the cache, if it is enabled
167+
#' API, and update the cache, if it is enabled
168+
#' @param reference_week_day the day of the week to use as the reference day
169+
#' when parsing epiweeks to dates (happens if `disable_date_parsing` is `FALSE`)
170+
#' Defaults to 1 Sunday (the first day of the week).
168171
#' @return A `fetch_args` object containing all the specified options
169172
#' @export
170173
#' @aliases fetch_args
@@ -180,7 +183,8 @@ fetch_args_list <- function(
180183
dry_run = FALSE,
181184
debug = FALSE,
182185
format_type = c("json", "classic", "csv"),
183-
refresh_cache = FALSE
186+
refresh_cache = FALSE,
187+
reference_week_day = 1
184188
) {
185189
rlang::check_dots_empty()
186190

@@ -194,6 +198,7 @@ fetch_args_list <- function(
194198
assert_logical(debug, null.ok = FALSE, len = 1L, any.missing = FALSE)
195199
format_type <- match.arg(format_type)
196200
assert_logical(refresh_cache, null.ok = FALSE, len = 1L, any.missing = FALSE)
201+
assert_numeric(reference_week_day, null.ok = FALSE, len = 1L, any.missing = FALSE)
197202

198203
structure(
199204
list(
@@ -206,7 +211,8 @@ fetch_args_list <- function(
206211
dry_run = dry_run,
207212
debug = debug,
208213
format_type = format_type,
209-
refresh_cache = refresh_cache
214+
refresh_cache = refresh_cache,
215+
reference_week_day = reference_week_day
210216
),
211217
class = "fetch_args"
212218
)
@@ -279,7 +285,7 @@ fetch <- function(epidata_call, fetch_args = fetch_args_list()) {
279285
if (fetch_args$return_empty && length(response_content) == 0) {
280286
fetched <- tibble()
281287
} else {
282-
fetched <- parse_data_frame(epidata_call, response_content, fetch_args$disable_date_parsing) %>% as_tibble()
288+
fetched <- parse_data_frame(epidata_call, response_content, fetch_args$disable_date_parsing, fetch_args$reference_week_day) %>% as_tibble()
283289
}
284290
})
285291

R/model.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ print.EpidataFieldInfo <- function(x, ...) {
163163
}
164164

165165
#' @importFrom stats na.omit
166-
parse_value <- function(info, value, disable_date_parsing = FALSE) {
166+
parse_value <- function(info, value, disable_date_parsing = FALSE, reference_week_day = 1) {
167167
stopifnot(inherits(info, "EpidataFieldInfo"))
168168

169169
if (is.null(value)) {
170170
return(value)
171171
} else if (info$type == "date" && !disable_date_parsing && !inherits(value, "Date")) {
172172
return(parse_api_date(value))
173173
} else if (info$type == "epiweek" && !disable_date_parsing && !inherits(value, "Date")) {
174-
return(parse_api_week(value))
174+
return(parse_api_week(value, reference_week_day = reference_week_day))
175175
} else if (info$type == "bool") {
176176
return(as.logical(value))
177177
} else if (info$type == "int") {
@@ -197,7 +197,7 @@ parse_value <- function(info, value, disable_date_parsing = FALSE) {
197197
}
198198

199199
#' @importFrom purrr map_chr
200-
parse_data_frame <- function(epidata_call, df, disable_date_parsing = FALSE) {
200+
parse_data_frame <- function(epidata_call, df, disable_date_parsing = FALSE, reference_week_day = 1) {
201201
stopifnot(inherits(epidata_call, "epidata_call"))
202202
meta <- epidata_call$meta
203203
df <- as.data.frame(df)
@@ -224,7 +224,7 @@ parse_data_frame <- function(epidata_call, df, disable_date_parsing = FALSE) {
224224
for (i in seq_len(length(meta))) {
225225
info <- meta[[i]]
226226
if (info$name %in% columns) {
227-
df[[info$name]] <- parse_value(info, df[[info$name]], disable_date_parsing = disable_date_parsing)
227+
df[[info$name]] <- parse_value(info, df[[info$name]], disable_date_parsing = disable_date_parsing, reference_week_day = reference_week_day)
228228
}
229229
}
230230
df
@@ -251,14 +251,15 @@ parse_api_date <- function(value) {
251251

252252
#' parse_api_week converts an integer to a date
253253
#' @param value value to be converted to an epiweek
254+
#' @param reference_week_day the day of the week to use as the reference day. Defaults to Saturday.
254255
#' @return a date
255256
#' @importFrom MMWRweek MMWRweek2Date
256257
#' @keywords internal
257-
parse_api_week <- function(value) {
258+
parse_api_week <- function(value, reference_week_day = 1) {
258259
v <- as.integer(value)
259260
years <- floor(v / 100)
260261
weeks <- v - (years * 100)
261-
MMWRweek::MMWRweek2Date(years, weeks)
262+
MMWRweek::MMWRweek2Date(years, weeks, MMWRday = reference_week_day)
262263
}
263264

264265
#' @importFrom checkmate test_character test_class test_date test_integerish test_list

0 commit comments

Comments
 (0)