Skip to content

Commit 49bfb4d

Browse files
committed
Make use of new support for multiple geo_values in one request
Sending multiple geo_values in comma delimited string Request is returned as successful if any data retrieved, so treat day/geo_value combos with non-retreived data as warnings Also, update error message to use human readable dates
1 parent 4e462ed commit 49bfb4d

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

R-packages/covidcast/R/covidcast.R

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,8 @@ covidcast_signal <- function(data_source, signal,
220220
issues <- as.Date(issues)
221221
}
222222

223-
df <- purrr::map_dfr(geo_values, function(geo_val) {
224-
single_geo(data_source, signal, start_day, end_day, geo_type, geo_val,
225-
as_of, issues, lag)
226-
})
223+
df <- covidcast_days(data_source, signal, start_day, end_day, geo_type,
224+
geo_values, as_of, issues, lag)
227225

228226
# Drop direction column (if it still exists)
229227
df$direction <- NULL
@@ -473,35 +471,51 @@ summary.covidcast_meta = function(object, ...) {
473471

474472
##########
475473

476-
# Helper function, not user-facing, to fetch a single geo-value.
477-
# covidcast_signal can then loop over multiple geos to produce its result.
478-
single_geo <- function(data_source, signal, start_day, end_day, geo_type,
474+
# Helper function, not user-facing, to loop through a sequence of days, call
475+
# covidcast for each one and combine the results
476+
covidcast_days <- function(data_source, signal, start_day, end_day, geo_type,
479477
geo_value, as_of, issues, lag) {
480478
ndays <- as.numeric(end_day - start_day)
481479
dat <- list()
482480

483481
# The API limits the number of rows that can be returned at once, so we query
484482
# each day separately.
485483
for (i in seq(ndays + 1)) {
486-
day <- date_to_string(start_day + i - 1)
484+
query_day <- start_day + i - 1
485+
day_str <- date_to_string(query_day)
487486
dat[[i]] <- covidcast(data_source = data_source,
488487
signal = signal,
489488
time_type = "day",
490489
geo_type = geo_type,
491-
time_values = day,
490+
time_values = day_str,
492491
geo_value = geo_value,
493492
as_of = as_of,
494493
issues = issues,
495494
lag = lag)
496495
message(sprintf("Fetched day %s: %s, %s, num_entries = %s",
497-
day, dat[[i]]$result, dat[[i]]$message,
496+
query_day,
497+
dat[[i]]$result,
498+
dat[[i]]$message,
498499
nrow(dat[[i]]$epidata)))
499-
500-
if (dat[[i]]$message != "success") {
501-
warn(paste0("Fetching ", signal, " from ", data_source, " for ", day,
502-
" in geography '", geo_value, "': ", dat[[i]]$message, "."),
503-
data_source = data_source, signal = signal, day = day,
504-
geo_value = geo_value, msg = dat[[i]]$message,
500+
if (dat[[i]]$message == "success") {
501+
returned_geo_values <- dat[[i]]$epidata$geo_value
502+
missed_geos <- setdiff(geo_value, returned_geo_values)
503+
if (length(missed_geos) > 0) {
504+
missed_geos_str <- paste0(missed_geos, collapse = ", ")
505+
warn(message =
506+
sprintf("Data not fetched for some geographies on %s: %s",
507+
query_day, missed_geos_str)
508+
)
509+
}
510+
} else {
511+
warn(paste0("Fetching ", signal, " from ", data_source, " for ",
512+
query_day, " in geography '", geo_value, "': ",
513+
dat[[i]]$message),
514+
data_source = data_source,
515+
signal = signal,
516+
day = query_day,
517+
geo_value = geo_value,
518+
msg = dat[[i]]$message,
505519
class = "covidcast_fetch_failed")
506520
}
507521
}
@@ -548,7 +562,10 @@ covidcast <- function(data_source, signal, time_type, geo_type, time_values,
548562
time_values = .list(time_values),
549563
geo_value = geo_value
550564
)
551-
565+
if (length(params$geo_value) > 1) {
566+
params$geo_values <- paste0(params$geo_value, collapse = ",") #convert to string
567+
params$geo_value <- NULL
568+
}
552569
if (!is.null(as_of)) {
553570
params$as_of <- date_to_string(as_of)
554571
}

0 commit comments

Comments
 (0)