Skip to content

Commit c46917e

Browse files
authored
Merge pull request #151 from JedGrabman/dev_multi_geo_value
Make use of new support for multiple geo_values in one request
2 parents 4bd43f3 + 8008a89 commit c46917e

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

R-packages/covidcast/R/covidcast.R

Lines changed: 40 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,57 @@ 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(tolower(geo_value), tolower(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+
data_source = data_source,
509+
signal = signal,
510+
day = query_day,
511+
geo_value = geo_value,
512+
msg = dat[[i]]$message,
513+
class = "covidcast_missing_geo_values"
514+
)
515+
}
516+
} else {
517+
warn(paste0("Fetching ", signal, " from ", data_source, " for ",
518+
query_day, " in geography '", geo_value, "': ",
519+
dat[[i]]$message),
520+
data_source = data_source,
521+
signal = signal,
522+
day = query_day,
523+
geo_value = geo_value,
524+
msg = dat[[i]]$message,
505525
class = "covidcast_fetch_failed")
506526
}
507527
}
@@ -548,7 +568,10 @@ covidcast <- function(data_source, signal, time_type, geo_type, time_values,
548568
time_values = .list(time_values),
549569
geo_value = geo_value
550570
)
551-
571+
if (length(params$geo_value) > 1) {
572+
params$geo_values <- paste0(params$geo_value, collapse = ",") #convert to string
573+
params$geo_value <- NULL
574+
}
552575
if (!is.null(as_of)) {
553576
params$as_of <- date_to_string(as_of)
554577
}

0 commit comments

Comments
 (0)