From 65a37363041e02cc1a2d7572ad4e26f90065a8b3 Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Fri, 25 Aug 2023 16:07:17 -0700 Subject: [PATCH 01/15] feat+test+docs+build: fetch by default --- .gitignore | 1 + NAMESPACE | 4 +- R/covidcast.R | 27 ++-- R/endpoints.R | 208 +++++++++++++++++------------ R/epidatacall.R | 162 ++++++++++++++-------- epidatr.Rproj | 17 --- man/covid_hosp_facility.Rd | 11 +- man/covid_hosp_facility_lookup.Rd | 9 +- man/covid_hosp_state_timeseries.Rd | 11 +- man/covidcast.Rd | 9 +- man/covidcast_meta.Rd | 7 +- man/delphi.Rd | 6 +- man/dengue_nowcast.Rd | 6 +- man/ecdc_ili.Rd | 12 +- man/epidata_call.Rd | 57 ++++++-- man/fetch_classic.Rd | 21 +-- man/fetch_tbl.Rd | 19 +-- man/flusurv.Rd | 12 +- man/fluview.Rd | 13 +- man/fluview_clinical.Rd | 12 +- man/fluview_meta.Rd | 7 +- man/gft.Rd | 6 +- man/kcdc_ili.Rd | 12 +- man/meta.Rd | 5 +- man/nidss_dengue.Rd | 6 +- man/nidss_flu.Rd | 12 +- man/nowcast.Rd | 6 +- man/paho_dengue.Rd | 12 +- man/parse_signal.Rd | 8 +- man/pvt_cdc.Rd | 7 +- man/pvt_dengue_sensors.Rd | 15 ++- man/pvt_ght.Rd | 6 +- man/pvt_meta_norostat.Rd | 6 +- man/pvt_norostat.Rd | 6 +- man/pvt_quidel.Rd | 6 +- man/pvt_sensors.Rd | 6 +- man/pvt_twitter.Rd | 12 +- man/wiki.Rd | 13 +- man/with_base_url.Rd | 20 +++ tests/testthat/test-covidcast.R | 17 ++- tests/testthat/test-endpoints.R | 203 ++++++++++++++++++++-------- tests/testthat/test-epidatacall.R | 68 +++++++++- tests/testthat/test-model.R | 6 +- vignettes/epidatr.Rmd | 70 +++++----- 44 files changed, 775 insertions(+), 384 deletions(-) delete mode 100644 epidatr.Rproj create mode 100644 man/with_base_url.Rd diff --git a/.gitignore b/.gitignore index 5325ba8a..2ecf9b05 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ env /doc/ /Meta/ .secrets +epidatr.Rproj diff --git a/NAMESPACE b/NAMESPACE index b6041fb3..6a2d3570 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,7 @@ export(dengue_nowcast) export(ecdc_ili) export(epirange) export(fetch) +export(fetch_args_list) export(flusurv) export(fluview) export(fluview_clinical) @@ -38,11 +39,12 @@ export(pvt_quidel) export(pvt_sensors) export(pvt_twitter) export(wiki) -export(with_base_url) importFrom(MMWRweek,MMWRweek2Date) importFrom(checkmate,assert) importFrom(checkmate,assert_character) importFrom(checkmate,assert_integerish) +importFrom(checkmate,assert_logical) +importFrom(checkmate,assert_numeric) importFrom(checkmate,check_character) importFrom(checkmate,check_class) importFrom(checkmate,check_date) diff --git a/R/covidcast.R b/R/covidcast.R index 3ff1a912..6d9d860d 100644 --- a/R/covidcast.R +++ b/R/covidcast.R @@ -6,10 +6,10 @@ parse_signal <- function(signal, base_url) { #' #' @param data_source data source to fetch #' @param signals data source to fetch - #' @param time_type data source to fetch - #' @param time_values data source to fetch #' @param geo_type geo_type to fetch + #' @param time_type data source to fetch #' @param geo_values data source to fetch + #' @param time_values data source to fetch #' @param as_of data source to fetch #' @param issues data source to fetch #' @param lag data source to fetch @@ -18,13 +18,24 @@ parse_signal <- function(signal, base_url) { signal$call <- function(geo_type, geo_values, time_values, - ...) { - epicall <- covidcast( - signal$source, signal$signal, geo_type, signal$time_type, - geo_values, time_values, ... + as_of = NULL, + issues = NULL, + lag = NULL, + fetch_args = fetch_args_list()) { + stopifnot(is.character(geo_type) & length(geo_type) == 1) + + covidcast( + source = signal$source, + signals = signal$signal, + geo_type = geo_type, + time_type = signal$time_type, + geo_values = geo_values, + time_values = time_values, + as_of = as_of, + issues = issues, + lag = lag, + fetch_args = fetch_args ) - epicall$base_url <- base_url - epicall } r <- list() r[[signal$signal]] <- signal diff --git a/R/endpoints.R b/R/endpoints.R index 8309fd8c..d763fe15 100644 --- a/R/endpoints.R +++ b/R/endpoints.R @@ -8,15 +8,17 @@ #' auth = "yourkey", #' locations = "fl,ca", #' epirange(201501, 201601) -#' ) %>% fetch() +#' ) #' } #' @param auth string. Restricted access key (not the same as API key). #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. +#' See `fetch_args_list()` for details. #' @return [`epidata_call`] #' #' @export -pvt_cdc <- function(auth, locations, epiweeks) { +pvt_cdc <- function(auth, locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("auth", auth, len = 1) assert_character_param("locations", locations) assert_timeset_param("epiweeks", epiweeks) @@ -43,7 +45,7 @@ pvt_cdc <- function(auth, locations, epiweeks) { create_epidata_field_info("total", "int"), create_epidata_field_info("value", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch COVID hospitalization facility identifiers @@ -61,20 +63,21 @@ pvt_cdc <- function(auth, locations, epiweeks) { #' #' @examples #' \dontrun{ -#' covid_hosp_facility_lookup(state = "fl") %>% fetch() -#' covid_hosp_facility_lookup(city = "southlake") %>% fetch() +#' covid_hosp_facility_lookup(state = "fl") +#' covid_hosp_facility_lookup(city = "southlake") #' } #' @param state string. A two-letter character state abbreviation. #' @param ccn string. A facility CMS certification number. #' @param city string. A city name. #' @param zip string. A 5-digit zip code. #' @param fips_code string. A 5-digit fips county code, zero-padded. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @seealso [covid_hosp_facility()] #' #' @export -covid_hosp_facility_lookup <- function(state = NULL, ccn = NULL, city = NULL, zip = NULL, fips_code = NULL) { +covid_hosp_facility_lookup <- function(state = NULL, ccn = NULL, city = NULL, zip = NULL, fips_code = NULL, fetch_args = fetch_args_list()) { assert_character_param("state", state, len = 1, required = FALSE) assert_character_param("ccn", ccn, len = 1, required = FALSE) assert_character_param("city", city, len = 1, required = FALSE) @@ -116,7 +119,7 @@ covid_hosp_facility_lookup <- function(state = NULL, ccn = NULL, city = NULL, zi create_epidata_field_info("fips_code", "text"), create_epidata_field_info("is_metro_micro", "int") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch COVID hospitalization data for specific facilities @@ -137,11 +140,12 @@ covid_hosp_facility_lookup <- function(state = NULL, ccn = NULL, city = NULL, zi #' covid_hosp_facility( #' hospital_pks = "100075", #' collection_weeks = epirange(20200101, 20200501) -#' ) %>% fetch() +#' ) #' } #' @param hospital_pks character. Facility identifiers. #' @param collection_weeks [`timeset`]. Epiweeks to fetch. #' @param publication_dates [`timeset`]. Publication dates to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' See also the official description and data dictionary at for @@ -150,7 +154,7 @@ covid_hosp_facility_lookup <- function(state = NULL, ccn = NULL, city = NULL, zi #' @seealso [covid_hosp_facility_lookup()], [epirange()] #' @export # -covid_hosp_facility <- function(hospital_pks, collection_weeks, publication_dates = NULL) { +covid_hosp_facility <- function(hospital_pks, collection_weeks, publication_dates = NULL, fetch_args = fetch_args_list()) { assert_character_param("hospital_pks", hospital_pks) assert_timeset_param("collection_weeks", collection_weeks) assert_timeset_param("publication_dates", publication_dates, required = FALSE) @@ -410,7 +414,7 @@ covid_hosp_facility <- function(hospital_pks, collection_weeks, publication_date "float" ) ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch COVID Hospitalization Data by State @@ -429,12 +433,13 @@ covid_hosp_facility <- function(hospital_pks, collection_weeks, publication_date #' covid_hosp_state_timeseries( #' states = "fl", #' dates = epirange(20200101, 20200501) -#' ) %>% fetch() +#' ) #' } #' @param states character. Two letter state abbreviations. #' @param dates [`timeset`]. Dates to fetch. #' @param issues [`timeset`]. Optionally, the issues to fetch. If not set, the #' most recent issue is returned. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' See also the official description and data dictionary at for @@ -442,7 +447,7 @@ covid_hosp_facility <- function(hospital_pks, collection_weeks, publication_date #' #' @export # -covid_hosp_state_timeseries <- function(states, dates, issues = NULL) { +covid_hosp_state_timeseries <- function(states, dates, issues = NULL, fetch_args = fetch_args_list()) { assert_character_param("states", states) assert_timeset_param("dates", dates) assert_timeset_param("issues", issues, required = FALSE) @@ -568,7 +573,7 @@ covid_hosp_state_timeseries <- function(states, dates, issues = NULL) { create_epidata_field_info("adult_icu_bed_covid_utilization", "float"), create_epidata_field_info("adult_icu_bed_utilization", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch covidcast metadata @@ -580,16 +585,18 @@ covid_hosp_state_timeseries <- function(states, dates, issues = NULL) { #' API docs: #' . #' +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. +#' #' @return [`epidata_call`] #' #' @examples #' \dontrun{ -#' covidcast_meta() %>% fetch() +#' covidcast_meta() #' } #' #' @seealso [covidcast()],[covidcast_epidata()] #' @export -covidcast_meta <- function() { +covidcast_meta <- function(fetch_args = fetch_args_list()) { create_epidata_call( "covidcast_meta/", list(), @@ -612,7 +619,7 @@ covidcast_meta <- function() { create_epidata_field_info("min_lag", "int"), create_epidata_field_info("max_lag", "int") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch covidcast data @@ -630,7 +637,7 @@ covidcast_meta <- function() { #' time_type = "day", #' geo_values = "ca,fl", #' time_values = epirange(20200601, 20200801) -#' ) %>% fetch() +#' ) #' covidcast( #' source = "jhu-csse", #' signals = "confirmed_7dav_incidence_prop", @@ -638,7 +645,7 @@ covidcast_meta <- function() { #' time_type = "day", #' geo_values = "*", #' time_values = epirange(20200601, 20200801) -#' ) %>% fetch() +#' ) #' } #' @param source string. The data source to query (see: #' ). @@ -661,6 +668,7 @@ covidcast_meta <- function() { #' @param lag integer. Optionally, the lag of the issues to fetch. If not set, #' the most recent issue is returned. Mutually exclusive with `as_of` or #' `issues`. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @seealso [covidcast_meta()], [covidcast_epidata()], [epirange()] @@ -675,7 +683,8 @@ covidcast <- function( time_values, as_of = NULL, issues = NULL, - lag = NULL) { + lag = NULL, + fetch_args = fetch_args_list()) { # Check parameters if ( missing(source) || @@ -689,7 +698,7 @@ covidcast <- function( ) } - if (sum(!missing(issues), !missing(lag), !missing(as_of)) > 1) { + if (sum(!is.null(issues), !is.null(lag), !is.null(as_of)) > 1) { stop("`issues`, `lag`, and `as_of` are mutually exclusive") } @@ -743,7 +752,7 @@ covidcast <- function( create_epidata_field_info("missing_stderr", "int"), create_epidata_field_info("missing_sample_size", "int") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Delphi's ILINet forecasts @@ -752,14 +761,15 @@ covidcast <- function( #' #' @examples #' \dontrun{ -#' delphi(system = "ec", epiweek = 201501) %>% fetch() +#' delphi(system = "ec", epiweek = 201501) #' } #' @param system character. System name to fetch. #' @param epiweek [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -delphi <- function(system, epiweek) { +delphi <- function(system, epiweek, fetch_args = fetch_args_list()) { assert_character_param("system", system) assert_timeset_param("epiweek", epiweek, len = 1) epiweek <- parse_timeset_input(epiweek) @@ -773,7 +783,7 @@ delphi <- function(system, epiweek) { create_epidata_field_info("json", "text") ), only_supports_classic = TRUE - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Delphi's PAHO Dengue nowcast @@ -786,14 +796,15 @@ delphi <- function(system, epiweek) { #' dengue_nowcast( #' locations = "pr", #' epiweeks = epirange(201501, 202001) -#' ) %>% fetch() +#' ) #' } #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -dengue_nowcast <- function(locations, epiweeks) { +dengue_nowcast <- function(locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("locations", locations) assert_timeset_param("epiweeks", epiweeks) epiweeks <- parse_timeset_input(epiweeks) @@ -807,7 +818,7 @@ dengue_nowcast <- function(locations, epiweeks) { create_epidata_field_info("value", "float"), create_epidata_field_info("std", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Delphi's digital surveillance sensors for dengue in PAHO member @@ -815,7 +826,6 @@ dengue_nowcast <- function(locations, epiweeks) { #' #' API docs: #' -#' TODO: what are valid locations and names? #' @examples #' \dontrun{ #' pvt_dengue_sensors( @@ -823,16 +833,17 @@ dengue_nowcast <- function(locations, epiweeks) { #' names = "ght", #' locations = "ag", #' epiweeks = epirange(201501, 202001) -#' ) %>% fetch() +#' ) #' } #' @param auth string. Restricted access key (not the same as API key). #' @param names character. Names to fetch. #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -pvt_dengue_sensors <- function(auth, names, locations, epiweeks) { +pvt_dengue_sensors <- function(auth, names, locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("auth", auth, len = 1) assert_character_param("names", names) assert_character_param("locations", locations) @@ -853,7 +864,7 @@ pvt_dengue_sensors <- function(auth, names, locations, epiweeks) { create_epidata_field_info("epiweek", "epiweek"), create_epidata_field_info("value", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch ECDC data @@ -868,7 +879,7 @@ pvt_dengue_sensors <- function(auth, names, locations, epiweeks) { #' #' @examples #' \dontrun{ -#' ecdc_ili(regions = "austria", epiweeks = epirange(201901, 202001)) %>% fetch() +#' ecdc_ili(regions = "austria", epiweeks = epirange(201901, 202001)) #' } #' @param regions character. Regions to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. @@ -876,10 +887,11 @@ pvt_dengue_sensors <- function(auth, names, locations, epiweeks) { #' most recent issue is returned. Mutually exclusive with `lag`. #' @param lag integer. Optionally, the lag of the issues to fetch. If not set, #' the most recent issue is returned. Mutually exclusive with `issues`. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -ecdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL) { +ecdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL, fetch_args = fetch_args_list()) { assert_character_param("regions", regions) assert_timeset_param("epiweeks", epiweeks) assert_timeset_param("issues", issues, required = FALSE) @@ -906,7 +918,7 @@ ecdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL) { create_epidata_field_info("lag", "int"), create_epidata_field_info("incidence_rate", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch FluSurv hospitalization data @@ -922,7 +934,7 @@ ecdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL) { #' #' @examples #' \dontrun{ -#' flusurv(locations = "CA", epiweeks = epirange(201701, 201801)) %>% fetch() +#' flusurv(locations = "CA", epiweeks = epirange(201701, 201801)) #' } #' @param locations character. Character vector indicating location. #' @param epiweeks [`timeset`]. Epiweeks to fetch. @@ -930,10 +942,11 @@ ecdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL) { #' most recent issue is returned. Mutually exclusive with `lag`. #' @param lag integer. Optionally, the lag of the issues to fetch. If not set, #' the most recent issue is returned. Mutually exclusive with `issues`. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -flusurv <- function(locations, epiweeks, issues = NULL, lag = NULL) { +flusurv <- function(locations, epiweeks, issues = NULL, lag = NULL, fetch_args = fetch_args_list()) { assert_character_param("locations", locations) assert_timeset_param("epiweeks", epiweeks) assert_timeset_param("issues", issues, required = FALSE) @@ -965,7 +978,7 @@ flusurv <- function(locations, epiweeks, issues = NULL, lag = NULL) { create_epidata_field_info("rate_age_4", "float"), create_epidata_field_info("rate_overall", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch FluView virological data from clinical labs @@ -974,7 +987,7 @@ flusurv <- function(locations, epiweeks, issues = NULL, lag = NULL) { #' #' @examples #' \dontrun{ -#' fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701)) %>% fetch() +#' fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701)) #' } #' @param regions character. Regions to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch in the form @@ -984,10 +997,11 @@ flusurv <- function(locations, epiweeks, issues = NULL, lag = NULL) { #' most recent issue is returned. Mutually exclusive with `lag`. #' @param lag integer. Optionally, the lag of the issues to fetch. If not set, #' the most recent issue is returned. Mutually exclusive with `issues`. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -fluview_clinical <- function(regions, epiweeks, issues = NULL, lag = NULL) { +fluview_clinical <- function(regions, epiweeks, issues = NULL, lag = NULL, fetch_args = fetch_args_list()) { assert_character_param("regions", regions) assert_timeset_param("epiweeks", epiweeks) assert_timeset_param("issues", issues, required = FALSE) @@ -1019,7 +1033,7 @@ fluview_clinical <- function(regions, epiweeks, issues = NULL, lag = NULL) { create_epidata_field_info("percent_a", "float"), create_epidata_field_info("percent_b", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch FluView metadata @@ -1027,13 +1041,15 @@ fluview_clinical <- function(regions, epiweeks, issues = NULL, lag = NULL) { #' API docs: #' @examples #' \dontrun{ -#' fluview_meta() %>% fetch() +#' fluview_meta() #' } #' +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. +#' #' @return [`epidata_call`] #' #' @export -fluview_meta <- function() { +fluview_meta <- function(fetch_args = fetch_args_list()) { create_epidata_call( "fluview_meta/", list(), @@ -1059,7 +1075,7 @@ fluview_meta <- function() { #' . #' #' @examples -#' fluview(regions = "nat", epiweeks = epirange(201201, 202001)) %>% fetch() +#' fluview(regions = "nat", epiweeks = epirange(201201, 202001)) #' @param regions character. Locations to fetch. Can be any string IDs in #' national, HHS region, census division, most states and territories, and so #' on. Full list link below. @@ -1072,10 +1088,11 @@ fluview_meta <- function() { #' the most recent issue is returned. Mutually exclusive with `issues`. #' @param auth string. Optionally, restricted access key (not the same as API #' key). +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -fluview <- function(regions, epiweeks, issues = NULL, lag = NULL, auth = NULL) { +fluview <- function(regions, epiweeks, issues = NULL, lag = NULL, auth = NULL, fetch_args = fetch_args_list()) { assert_character_param("regions", regions) assert_timeset_param("epiweeks", epiweeks) assert_timeset_param("issues", issues, required = FALSE) @@ -1114,7 +1131,7 @@ fluview <- function(regions, epiweeks, issues = NULL, lag = NULL, auth = NULL) { create_epidata_field_info("wili", "float"), create_epidata_field_info("ili", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Google Flu Trends data @@ -1133,15 +1150,16 @@ fluview <- function(regions, epiweeks, issues = NULL, lag = NULL, auth = NULL) { #' #' @examples #' \dontrun{ -#' gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) %>% fetch() +#' gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) #' } #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`] Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' #' @return [`epidata_call`] #' #' @export -gft <- function(locations, epiweeks) { +gft <- function(locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("locations", locations) assert_timeset_param("epiweeks", epiweeks) epiweeks <- parse_timeset_input(epiweeks) @@ -1154,7 +1172,7 @@ gft <- function(locations, epiweeks) { create_epidata_field_info("epiweek", "epiweek"), create_epidata_field_info("num", "int") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Google Health Trends data @@ -1169,16 +1187,17 @@ gft <- function(locations, epiweeks) { #' locations = "ca", #' epiweeks = epirange(201201, 202001), #' query = "how to get over the flu" -#' ) %>% fetch() +#' ) #' } #' @param auth string. Restricted access key (not the same as API key). #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. #' @param query string. The query to be fetched. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -pvt_ght <- function(auth, locations, epiweeks, query) { +pvt_ght <- function(auth, locations, epiweeks, query, fetch_args = fetch_args_list()) { assert_character_param("auth", auth, len = 1) assert_character_param("locations", locations) assert_timeset_param("epiweeks", epiweeks) @@ -1198,7 +1217,7 @@ pvt_ght <- function(auth, locations, epiweeks, query) { create_epidata_field_info("epiweek", "epiweek"), create_epidata_field_info("value", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch KCDC data @@ -1208,7 +1227,7 @@ pvt_ght <- function(auth, locations, epiweeks, query) { #' TODO: find a non-trivial region #' @examples #' \dontrun{ -#' kcdc_ili(regions = "ROK", epiweeks = 200436) %>% fetch() +#' kcdc_ili(regions = "ROK", epiweeks = 200436) #' } #' @param regions character. Regions to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. @@ -1216,10 +1235,11 @@ pvt_ght <- function(auth, locations, epiweeks, query) { #' most recent issue is returned. Mutually exclusive with `lag`. #' @param lag integer. Optionally, the lag of the issues to fetch. If not set, #' the most recent issue is returned. Mutually exclusive with `issues`. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -kcdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL) { +kcdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL, fetch_args = fetch_args_list()) { assert_character_param("regions", regions) assert_timeset_param("epiweeks", epiweeks) assert_timeset_param("issues", issues, required = FALSE) @@ -1246,7 +1266,7 @@ kcdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL) { create_epidata_field_info("lag", "int"), create_epidata_field_info("ili", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch NoroSTAT metadata @@ -1255,27 +1275,34 @@ kcdc_ili <- function(regions, epiweeks, issues = NULL, lag = NULL) { #' #' @examples #' \dontrun{ -#' pvt_meta_norostat(auth = "yourkey") %>% fetch() +#' pvt_meta_norostat(auth = "yourkey") #' } #' @param auth string. Restricted access key (not the same as API key). +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -pvt_meta_norostat <- function(auth) { +pvt_meta_norostat <- function(auth, fetch_args = fetch_args_list()) { assert_character_param("auth", auth, len = 1) - create_epidata_call("meta_norostat/", list(auth = auth), only_supports_classic = TRUE) + create_epidata_call( + "meta_norostat/", + list(auth = auth), + only_supports_classic = TRUE + ) %>% fetch(fetch_args = fetch_args) } #' Fetch api metadata #' #' API docs: #' +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. +#' #' @return [`epidata_call`] #' #' @export -meta <- function() { - create_epidata_call("meta/", list(), only_supports_classic = TRUE) +meta <- function(fetch_args = fetch_args_list()) { + create_epidata_call("meta/", list(), only_supports_classic = TRUE) %>% fetch(fetch_args = fetch_args) } #' Fetch NIDSS dengue data @@ -1292,15 +1319,16 @@ meta <- function() { #' #' @examples #' \dontrun{ -#' nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 201301)) %>% fetch() +#' nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 201301)) #' } #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' #' @return [`epidata_call`] #' #' @export -nidss_dengue <- function(locations, epiweeks) { +nidss_dengue <- function(locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("locations", locations) assert_timeset_param("epiweeks", epiweeks) epiweeks <- parse_timeset_input(epiweeks) @@ -1313,7 +1341,7 @@ nidss_dengue <- function(locations, epiweeks) { create_epidata_field_info("epiweek", "epiweek"), create_epidata_field_info("count", "int") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch NIDSS flu data @@ -1325,7 +1353,7 @@ nidss_dengue <- function(locations, epiweeks) { #' #' @examples #' \dontrun{ -#' nidss_flu(regions = "taipei", epiweeks = epirange(201501, 201601)) %>% fetch() +#' nidss_flu(regions = "taipei", epiweeks = epirange(201501, 201601)) #' } #' @param regions character. Regions to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. @@ -1333,10 +1361,11 @@ nidss_dengue <- function(locations, epiweeks) { #' most recent issue is returned. Mutually exclusive with `lag`. #' @param lag integer. Optionally, the lag of the issues to fetch. If not set, #' the most recent issue is returned. Mutually exclusive with `issues`. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -nidss_flu <- function(regions, epiweeks, issues = NULL, lag = NULL) { +nidss_flu <- function(regions, epiweeks, issues = NULL, lag = NULL, fetch_args = fetch_args_list()) { assert_character_param("regions", regions) assert_timeset_param("epiweeks", epiweeks) assert_timeset_param("issues", issues, required = FALSE) @@ -1365,7 +1394,7 @@ nidss_flu <- function(regions, epiweeks, issues = NULL, lag = NULL) { create_epidata_field_info("visits", "int"), create_epidata_field_info("ili", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } @@ -1379,15 +1408,16 @@ nidss_flu <- function(regions, epiweeks, issues = NULL, lag = NULL) { #' auth = "yourkey", #' locations = "1", #' epiweeks = 201233 -#' ) %>% fetch() +#' ) #' } #' @param auth string. Your authentication key. #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -pvt_norostat <- function(auth, locations, epiweeks) { +pvt_norostat <- function(auth, locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("auth", auth, len = 1) assert_character_param("locations", locations, len = 1) assert_timeset_param("epiweeks", epiweeks) @@ -1405,7 +1435,7 @@ pvt_norostat <- function(auth, locations, epiweeks) { create_epidata_field_info("epiweek", "epiweek"), create_epidata_field_info("value", "int") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Delphi's ILI nowcast @@ -1420,14 +1450,15 @@ pvt_norostat <- function(auth, locations, epiweeks) { #' #' @examples #' \dontrun{ -#' nowcast(locations = "ca", epiweeks = epirange(201201, 201301)) %>% fetch() +#' nowcast(locations = "ca", epiweeks = epirange(201201, 201301)) #' } #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -nowcast <- function(locations, epiweeks) { +nowcast <- function(locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("locations", locations) assert_timeset_param("epiweeks", epiweeks) epiweeks <- parse_timeset_input(epiweeks) @@ -1441,7 +1472,7 @@ nowcast <- function(locations, epiweeks) { create_epidata_field_info("value", "float"), create_epidata_field_info("std", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch PAHO Dengue @@ -1450,7 +1481,7 @@ nowcast <- function(locations, epiweeks) { #' #' @examples #' \dontrun{ -#' paho_dengue(regions = "ca", epiweeks = epirange(201401, 201501)) %>% fetch() +#' paho_dengue(regions = "ca", epiweeks = epirange(201401, 201501)) #' } #' @param regions character. Regions to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. @@ -1458,10 +1489,11 @@ nowcast <- function(locations, epiweeks) { #' most recent issue is returned. Mutually exclusive with `lag`. #' @param lag integer. Optionally, the lag of the issues to fetch. If not set, #' the most recent issue is returned. Mutually exclusive with `issues`. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -paho_dengue <- function(regions, epiweeks, issues = NULL, lag = NULL) { +paho_dengue <- function(regions, epiweeks, issues = NULL, lag = NULL, fetch_args = fetch_args_list()) { assert_character_param("regions", regions) assert_timeset_param("epiweeks", epiweeks) assert_timeset_param("issues", issues, required = FALSE) @@ -1490,7 +1522,7 @@ paho_dengue <- function(regions, epiweeks, issues = NULL, lag = NULL) { create_epidata_field_info("num_deaths", "int"), create_epidata_field_info("incidence_rate", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Quidel COVID-19 and influenza testing data @@ -1503,15 +1535,16 @@ paho_dengue <- function(regions, epiweeks, issues = NULL, lag = NULL) { #' auth = "yourkey", #' epiweeks = epirange(201201, 202001), #' locations = "hhs1" -#' ) %>% fetch() +#' ) #' } #' @param auth string. Restricted access key (not the same as API key). #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -pvt_quidel <- function(auth, locations, epiweeks) { +pvt_quidel <- function(auth, locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("auth", auth, len = 1) assert_character_param("locations", locations) assert_timeset_param("epiweeks", epiweeks) @@ -1529,7 +1562,7 @@ pvt_quidel <- function(auth, locations, epiweeks) { create_epidata_field_info("epiweek", "epiweek"), create_epidata_field_info("value", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Delphi's digital surveillance sensors @@ -1543,16 +1576,17 @@ pvt_quidel <- function(auth, locations, epiweeks) { #' names = "sar3", #' locations = "nat", #' epiweeks = epirange(201501, 202001) -#' ) %>% fetch() +#' ) #' } #' @param auth string. Restricted access key (not the same as API key). #' @param names character. Sensor names to fetch. #' @param locations character. Locations to fetch. #' @param epiweeks [`timeset`]. Epiweeks to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -pvt_sensors <- function(auth, names, locations, epiweeks) { +pvt_sensors <- function(auth, names, locations, epiweeks, fetch_args = fetch_args_list()) { assert_character_param("auth", auth, len = 1) assert_character_param("names", names) assert_character_param("locations", locations) @@ -1573,7 +1607,7 @@ pvt_sensors <- function(auth, names, locations, epiweeks) { create_epidata_field_info("epiweek", "epiweek"), create_epidata_field_info("value", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch HealthTweets data @@ -1582,17 +1616,18 @@ pvt_sensors <- function(auth, names, locations, epiweeks) { #' #' @examples #' \dontrun{ -#' pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) %>% fetch() +#' pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) #' } #' @param auth string. Restricted access key (not the same as API key). #' @param locations character. Locations to fetch. #' @param dates [`timeset`]. Dates to fetch. Mutually exclusive with `epiweeks`. #' @param epiweeks [`timeset`]. Epiweeks to fetch. Mutually exclusive with #' `dates`. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -pvt_twitter <- function(auth, locations, dates = NULL, epiweeks = NULL) { +pvt_twitter <- function(auth, locations, dates = NULL, epiweeks = NULL, fetch_args = fetch_args_list()) { assert_character_param("auth", auth, len = 1) assert_character_param("locations", locations) assert_timeset_param("dates", dates, required = FALSE) @@ -1623,7 +1658,7 @@ pvt_twitter <- function(auth, locations, dates = NULL, epiweeks = NULL) { create_epidata_field_info("total", "int"), create_epidata_field_info("percent", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } #' Fetch Wikipedia access data @@ -1632,7 +1667,7 @@ pvt_twitter <- function(auth, locations, dates = NULL, epiweeks = NULL) { #' #' @examples #' \dontrun{ -#' wiki(articles = "avian_influenza", epiweeks = epirange(201501, 201601)) %>% fetch() +#' wiki(articles = "avian_influenza", epiweeks = epirange(201501, 201601)) #' } #' @param articles character. Articles to fetch. #' @param dates [`timeset`]. Dates to fetch. Mutually exclusive with `epiweeks`. @@ -1640,10 +1675,11 @@ pvt_twitter <- function(auth, locations, dates = NULL, epiweeks = NULL) { #' `dates`. #' @param language string. Language to fetch. #' @param hours integer. Optionally, the hours to fetch. +#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`. #' @return [`epidata_call`] #' #' @export -wiki <- function(articles, dates = NULL, epiweeks = NULL, hours = NULL, language = "en") { +wiki <- function(articles, dates = NULL, epiweeks = NULL, hours = NULL, language = "en", fetch_args = fetch_args_list()) { assert_character_param("articles", articles) assert_timeset_param("dates", dates, required = FALSE) assert_timeset_param("epiweeks", epiweeks, required = FALSE) @@ -1677,5 +1713,5 @@ wiki <- function(articles, dates = NULL, epiweeks = NULL, hours = NULL, language create_epidata_field_info("hour", "int"), create_epidata_field_info("value", "float") ) - ) + ) %>% fetch(fetch_args = fetch_args) } diff --git a/R/epidatacall.R b/R/epidatacall.R index 1e5fd818..404c574c 100644 --- a/R/epidatacall.R +++ b/R/epidatacall.R @@ -15,10 +15,9 @@ #' There are some other functions available for debugging and advanced usage: #' - `request_url` (for debugging): outputs the request URL from which data #' would be fetched (note additional parameters below) -#' - `with_base_url` (advanced): outputs an `epidata_call` that requests data -#' from a different Epidata server #' #' @examples +#' \dontrun{ #' covidcast( #' source = "jhu-csse", #' signals = "confirmed_7dav_incidence_prop", @@ -26,7 +25,8 @@ #' geo_type = "state", #' time_values = epirange(20200601, 20200801), #' geo_values = c("ca", "fl") -#' ) %>% fetch() +#' ) +#' } #' #' @param endpoint the epidata endpoint to call #' @param params the parameters to pass to the epidata endpoint @@ -98,6 +98,72 @@ print.epidata_call <- function(x, ...) { )) } +#' Fetch args +#' @rdname epidata_call +#' @aliases fetch_args +#' +#' A constructor for `fetch_args` objects, which are used to pass arguments to +#' the `fetch` function. +#' +#' @param fields a list of epidata fields to return, or NULL to return all +#' fields (default) e.g. c("time_value", "value") to return only the +#' time_value and value fields or c("-direction") to return everything except +#' the direction field +#' @param disable_date_parsing disable automatic date parsing +#' @param disable_data_frame_parsing disable automatic conversion to data frame; this +#' is only supported by endpoints that only support the 'classic' format (non-tabular) +#' @param return_empty boolean that allows returning an empty tibble if there is no data +#' @param timeout_seconds the maximum amount of time to wait for a response +#' @param base_url base URL to use +#' @param make_call boolean that allows skipping the call to the API and instead +#' returns the `epidata_call` object (useful for debugging) +#' @param debug boolean that allows returning the raw response from the API +#' @param format_type the format to request from the API, one of classic, json, csv; this +#' is only used by `fetch_debug` +#' @return +#' - For `fetch_args_list`: a `fetch_args` object +#' @export +#' +#' @importFrom checkmate assert_character assert_logical assert_numeric +#' +fetch_args_list <- function( + ..., + fields = NULL, + disable_date_parsing = FALSE, + disable_data_frame_parsing = FALSE, + return_empty = FALSE, + timeout_seconds = 30, + base_url = NULL, + make_call = TRUE, + debug = FALSE, + format_type = "json") { + assert_character(fields, null.ok = TRUE, len = NULL, any.missing = FALSE) + assert_logical(disable_date_parsing, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_logical(disable_data_frame_parsing, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_logical(return_empty, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_numeric(timeout_seconds, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_character(base_url, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_logical(make_call, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_logical(debug, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_character(format_type, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert(format_type %in% c("json", "csv", "classic"), "format_type must be one of json, csv, classic") + + structure( + list( + fields = fields, + disable_date_parsing = disable_date_parsing, + disable_data_frame_parsing = disable_data_frame_parsing, + return_empty = return_empty, + timeout_seconds = timeout_seconds, + base_url = base_url, + make_call = make_call, + debug = debug, + format_type = format_type + ), + class = "fetch_args" + ) +} + #' Fetches the data #' #' `fetch` usually returns the data in tibble format, but a few of the endpoints @@ -106,28 +172,31 @@ print.epidata_call <- function(x, ...) { #' #' @rdname epidata_call #' @param epidata_call an instance of `epidata_call` -#' @param fields a list of epidata fields to return, or NULL to return all -#' fields (default) e.g. c("time_value", "value") to return only the -#' time_value and value fields or c("-direction") to return everything except -#' the direction field -#' @param disable_date_parsing disable automatic date parsing -#' @param return_empty boolean that allows returning an empty tibble if there is no data -#' @param timeout_seconds the maximum amount of time to wait for a response +#' @param fetch_args a `fetch_args` object #' @return #' - For `fetch`: a tibble or a JSON-like list #' @export #' -fetch <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE, return_empty = FALSE, timeout_seconds = 30) { +fetch <- function(epidata_call, fetch_args = fetch_args_list()) { stopifnot(inherits(epidata_call, "epidata_call")) - stopifnot(is.null(fields) || is.character(fields)) - stopifnot(is.logical(disable_date_parsing), length(disable_date_parsing) == 1) - stopifnot(is.logical(return_empty)) - stopifnot(is.numeric(timeout_seconds)) + stopifnot(inherits(fetch_args, "fetch_args")) + + if (!is.null(fetch_args$base_url)) { + epidata_call <- with_base_url(epidata_call, fetch_args$base_url) + } + + if (!fetch_args$make_call) { + return(epidata_call) + } + + if (fetch_args$debug) { + return(fetch_debug(epidata_call, fetch_args)) + } if (epidata_call$only_supports_classic) { - return(fetch_classic(epidata_call, fields, return_empty = return_empty, timeout_seconds = timeout_seconds)) + return(fetch_classic(epidata_call, fetch_args)) } else { - return(fetch_tbl(epidata_call, fields, disable_date_parsing, return_empty, timeout_seconds = timeout_seconds)) + return(fetch_tbl(epidata_call, fetch_args)) } } @@ -135,13 +204,7 @@ fetch <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE, ret #' @rdname fetch_tbl #' #' @param epidata_call an instance of `epidata_call` -#' @param fields a list of epidata fields to return, or NULL to return all -#' fields (default) e.g. c("time_value", "value") to return only the -#' time_value and value fields or c("-direction") to return everything except -#' the direction field -#' @param disable_date_parsing disable automatic date parsing -#' @param return_empty boolean that allows returning an empty tibble if there is no data. -#' @param timeout_seconds the maximum amount of time to wait for a response +#' @param fetch_args a `fetch_args` object #' @importFrom readr read_csv #' @importFrom httr stop_for_status content #' @importFrom rlang abort @@ -149,25 +212,22 @@ fetch <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE, ret #' @return #' - For `fetch_tbl`: a [`tibble::tibble`] #' @keywords internal -fetch_tbl <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE, return_empty = FALSE, timeout_seconds = 30) { +fetch_tbl <- function(epidata_call, fetch_args = fetch_args_list()) { stopifnot(inherits(epidata_call, "epidata_call")) - stopifnot(is.null(fields) || is.character(fields)) - stopifnot(is.logical(disable_date_parsing), length(disable_date_parsing) == 1) - stopifnot(is.logical(return_empty)) - stopifnot(is.numeric(timeout_seconds)) + stopifnot(inherits(fetch_args, "fetch_args")) if (epidata_call$only_supports_classic) { - rlang::abort("This endpoint only supports the classic message format, due to a non-standard behavior. Use fetch_classic instead.", + rlang::abort("This endpoint only supports the classic message format, due to non-standard behavior. Use fetch_classic instead.", epidata_call = epidata_call, class = "only_supports_classic_format" ) } - response_content <- fetch_classic(epidata_call, fields, disable_data_frame_parsing = FALSE, return_empty = return_empty, timeout_seconds = timeout_seconds) - if (return_empty && length(response_content) == 0) { + response_content <- fetch_classic(epidata_call, fetch_args = fetch_args) + if (fetch_args$return_empty && length(response_content) == 0) { return(tibble()) } - return(parse_data_frame(epidata_call, response_content, disable_date_parsing) %>% as_tibble()) + return(parse_data_frame(epidata_call, response_content, fetch_args$disable_date_parsing) %>% as_tibble()) } #' Fetches the data, raises on epidata errors, and returns the results as a @@ -176,34 +236,24 @@ fetch_tbl <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE, #' @rdname fetch_classic #' #' @param epidata_call an instance of `epidata_call` -#' @param fields a list of epidata fields to return, or NULL to return all -#' fields (default) e.g. c("time_value", "value") to return only the -#' time_value and value fields or c("-direction") to return everything except -#' the direction field -#' @param disable_data_frame_parsing do not automatically cast the epidata -#' output to a data frame (some endpoints return a list of lists, which is not -#' a data frame) -#' @param return_empty boolean that allows returning an empty tibble if there is no data. -#' @param timeout_seconds the maximum amount of time to wait for a response +#' @param fetch_args a `fetch_args` object #' @importFrom httr stop_for_status content http_error #' @importFrom jsonlite fromJSON #' @return #' - For `fetch_classic`: a JSON-like list #' @keywords internal -fetch_classic <- function(epidata_call, fields = NULL, disable_data_frame_parsing = TRUE, return_empty = FALSE, timeout_seconds = 30) { +fetch_classic <- function(epidata_call, fetch_args = fetch_args_list()) { stopifnot(inherits(epidata_call, "epidata_call")) - stopifnot(is.null(fields) || is.character(fields)) - stopifnot(is.logical(return_empty)) - stopifnot(is.numeric(timeout_seconds)) + stopifnot(inherits(fetch_args, "fetch_args")) - response <- request_impl(epidata_call, "classic", fields, timeout_seconds) + response <- request_impl(epidata_call, "classic", fetch_args$fields, fetch_args$timeout_seconds) response_content <- httr::content(response, as = "text", encoding = "UTF-8") - response_content <- jsonlite::fromJSON(response_content, simplifyDataFrame = !disable_data_frame_parsing) + response_content <- jsonlite::fromJSON(response_content, simplifyDataFrame = !fetch_args$disable_data_frame_parsing) # success is 1, no results is -2, truncated is 2, -1 is generic error if (response_content$result != 1) { - if ((response_content$result != -2) && !(return_empty)) { + if ((response_content$result != -2) && !(fetch_args$return_empty)) { rlang::abort(paste0("epidata error: ", response_content$message), "epidata_error") } } @@ -213,8 +263,11 @@ fetch_classic <- function(epidata_call, fields = NULL, disable_data_frame_parsin return(response_content$epidata) } -fetch_debug <- function(epidata_call, format_type = "classic", fields = NULL, timeout_seconds = 30) { - response <- request_impl(epidata_call, format_type, fields, timeout_seconds) +fetch_debug <- function(epidata_call, fetch_args = fetch_args_list()) { + stopifnot(inherits(epidata_call, "epidata_call")) + stopifnot(inherits(fetch_args, "fetch_args")) + + response <- request_impl(epidata_call, fetch_args$format_type, fetch_args$fields, fetch_args$timeout_seconds) content <- httr::content(response, "text", encoding = "UTF-8") content } @@ -245,14 +298,11 @@ request_url <- function(epidata_call, format_type = "classic", fields = NULL) { } #' `epidata_call` object using a different base URL -#' @rdname epidata_call #' #' @param epidata_call an instance of `epidata_call` #' @param base_url base URL to use -#' @return -#' - For `with_base_url`: an `epidata_call` object -#' -#' @export +#' @return an `epidata_call` object +#' @keywords internal with_base_url <- function(epidata_call, base_url) { stopifnot(inherits(epidata_call, "epidata_call")) stopifnot(is.character(base_url), length(base_url) == 1) diff --git a/epidatr.Rproj b/epidatr.Rproj deleted file mode 100644 index 21a4da08..00000000 --- a/epidatr.Rproj +++ /dev/null @@ -1,17 +0,0 @@ -Version: 1.0 - -RestoreWorkspace: Default -SaveWorkspace: Default -AlwaysSaveHistory: Default - -EnableCodeIndexing: Yes -UseSpacesForTab: Yes -NumSpacesForTab: 2 -Encoding: UTF-8 - -RnwWeave: Sweave -LaTeX: pdfLaTeX - -BuildType: Package -PackageUseDevtools: Yes -PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/man/covid_hosp_facility.Rd b/man/covid_hosp_facility.Rd index 15d8c926..5c9dde8a 100644 --- a/man/covid_hosp_facility.Rd +++ b/man/covid_hosp_facility.Rd @@ -4,7 +4,12 @@ \alias{covid_hosp_facility} \title{Fetch COVID hospitalization data for specific facilities} \usage{ -covid_hosp_facility(hospital_pks, collection_weeks, publication_dates = NULL) +covid_hosp_facility( + hospital_pks, + collection_weeks, + publication_dates = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{hospital_pks}{character. Facility identifiers.} @@ -12,6 +17,8 @@ covid_hosp_facility(hospital_pks, collection_weeks, publication_dates = NULL) \item{collection_weeks}{\code{\link{timeset}}. Epiweeks to fetch.} \item{publication_dates}{\code{\link{timeset}}. Publication dates to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -37,7 +44,7 @@ used to look up facility identifiers in a variety of ways. covid_hosp_facility( hospital_pks = "100075", collection_weeks = epirange(20200101, 20200501) -) \%>\% fetch() +) } } \seealso{ diff --git a/man/covid_hosp_facility_lookup.Rd b/man/covid_hosp_facility_lookup.Rd index c945946b..cf96e1fc 100644 --- a/man/covid_hosp_facility_lookup.Rd +++ b/man/covid_hosp_facility_lookup.Rd @@ -9,7 +9,8 @@ covid_hosp_facility_lookup( ccn = NULL, city = NULL, zip = NULL, - fips_code = NULL + fips_code = NULL, + fetch_args = fetch_args_list() ) } \arguments{ @@ -22,6 +23,8 @@ covid_hosp_facility_lookup( \item{zip}{string. A 5-digit zip code.} \item{fips_code}{string. A 5-digit fips county code, zero-padded.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -41,8 +44,8 @@ For instance, specifying both city and state are not supported. } \examples{ \dontrun{ -covid_hosp_facility_lookup(state = "fl") \%>\% fetch() -covid_hosp_facility_lookup(city = "southlake") \%>\% fetch() +covid_hosp_facility_lookup(state = "fl") +covid_hosp_facility_lookup(city = "southlake") } } \seealso{ diff --git a/man/covid_hosp_state_timeseries.Rd b/man/covid_hosp_state_timeseries.Rd index 6475fbc3..e697e570 100644 --- a/man/covid_hosp_state_timeseries.Rd +++ b/man/covid_hosp_state_timeseries.Rd @@ -4,7 +4,12 @@ \alias{covid_hosp_state_timeseries} \title{Fetch COVID Hospitalization Data by State} \usage{ -covid_hosp_state_timeseries(states, dates, issues = NULL) +covid_hosp_state_timeseries( + states, + dates, + issues = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{states}{character. Two letter state abbreviations.} @@ -13,6 +18,8 @@ covid_hosp_state_timeseries(states, dates, issues = NULL) \item{issues}{\code{\link{timeset}}. Optionally, the issues to fetch. If not set, the most recent issue is returned.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -36,6 +43,6 @@ report annually. covid_hosp_state_timeseries( states = "fl", dates = epirange(20200101, 20200501) -) \%>\% fetch() +) } } diff --git a/man/covidcast.Rd b/man/covidcast.Rd index 941bba9c..e00df4a0 100644 --- a/man/covidcast.Rd +++ b/man/covidcast.Rd @@ -13,7 +13,8 @@ covidcast( time_values, as_of = NULL, issues = NULL, - lag = NULL + lag = NULL, + fetch_args = fetch_args_list() ) } \arguments{ @@ -46,6 +47,8 @@ specified, the most recent issue is returned. Mutually exclusive with \item{lag}{integer. Optionally, the lag of the issues to fetch. If not set, the most recent issue is returned. Mutually exclusive with \code{as_of} or \code{issues}.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -65,7 +68,7 @@ covidcast( time_type = "day", geo_values = "ca,fl", time_values = epirange(20200601, 20200801) -) \%>\% fetch() +) covidcast( source = "jhu-csse", signals = "confirmed_7dav_incidence_prop", @@ -73,7 +76,7 @@ covidcast( time_type = "day", geo_values = "*", time_values = epirange(20200601, 20200801) -) \%>\% fetch() +) } } \seealso{ diff --git a/man/covidcast_meta.Rd b/man/covidcast_meta.Rd index 9f9e9b41..cb91f170 100644 --- a/man/covidcast_meta.Rd +++ b/man/covidcast_meta.Rd @@ -4,7 +4,10 @@ \alias{covidcast_meta} \title{Fetch covidcast metadata} \usage{ -covidcast_meta() +covidcast_meta(fetch_args = fetch_args_list()) +} +\arguments{ +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -20,7 +23,7 @@ API docs: } \examples{ \dontrun{ -covidcast_meta() \%>\% fetch() +covidcast_meta() } } diff --git a/man/delphi.Rd b/man/delphi.Rd index e7baf732..63f69dba 100644 --- a/man/delphi.Rd +++ b/man/delphi.Rd @@ -4,12 +4,14 @@ \alias{delphi} \title{Fetch Delphi's ILINet forecasts} \usage{ -delphi(system, epiweek) +delphi(system, epiweek, fetch_args = fetch_args_list()) } \arguments{ \item{system}{character. System name to fetch.} \item{epiweek}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -19,6 +21,6 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/delphi.html} } \examples{ \dontrun{ -delphi(system = "ec", epiweek = 201501) \%>\% fetch() +delphi(system = "ec", epiweek = 201501) } } diff --git a/man/dengue_nowcast.Rd b/man/dengue_nowcast.Rd index 85a60314..831f77fa 100644 --- a/man/dengue_nowcast.Rd +++ b/man/dengue_nowcast.Rd @@ -4,12 +4,14 @@ \alias{dengue_nowcast} \title{Fetch Delphi's PAHO Dengue nowcast} \usage{ -dengue_nowcast(locations, epiweeks) +dengue_nowcast(locations, epiweeks, fetch_args = fetch_args_list()) } \arguments{ \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -25,6 +27,6 @@ TODO: what are valid locations here? dengue_nowcast( locations = "pr", epiweeks = epirange(201501, 202001) -) \%>\% fetch() +) } } diff --git a/man/ecdc_ili.Rd b/man/ecdc_ili.Rd index 2919ec99..382e3ce2 100644 --- a/man/ecdc_ili.Rd +++ b/man/ecdc_ili.Rd @@ -4,7 +4,13 @@ \alias{ecdc_ili} \title{Fetch ECDC data} \usage{ -ecdc_ili(regions, epiweeks, issues = NULL, lag = NULL) +ecdc_ili( + regions, + epiweeks, + issues = NULL, + lag = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{regions}{character. Regions to fetch.} @@ -16,6 +22,8 @@ most recent issue is returned. Mutually exclusive with \code{lag}.} \item{lag}{integer. Optionally, the lag of the issues to fetch. If not set, the most recent issue is returned. Mutually exclusive with \code{issues}.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -32,6 +40,6 @@ The list of location argument can be found in } \examples{ \dontrun{ -ecdc_ili(regions = "austria", epiweeks = epirange(201901, 202001)) \%>\% fetch() +ecdc_ili(regions = "austria", epiweeks = epirange(201901, 202001)) } } diff --git a/man/epidata_call.Rd b/man/epidata_call.Rd index 6336ddf6..79204c74 100644 --- a/man/epidata_call.Rd +++ b/man/epidata_call.Rd @@ -3,8 +3,23 @@ \name{create_epidata_call} \alias{create_epidata_call} \alias{epidata_call} +\alias{fetch_args_list} +\alias{fetch_args} +\alias{A} +\alias{constructor} +\alias{for} +\alias{`fetch_args`} +\alias{objects,} +\alias{which} +\alias{are} +\alias{used} +\alias{to} +\alias{pass} +\alias{arguments} +\alias{the} +\alias{`fetch`} +\alias{function.} \alias{fetch} -\alias{with_base_url} \title{An abstraction that holds information needed to make an epidata request} \usage{ create_epidata_call( @@ -14,15 +29,20 @@ create_epidata_call( only_supports_classic = FALSE ) -fetch( - epidata_call, +fetch_args_list( + ..., fields = NULL, disable_date_parsing = FALSE, + disable_data_frame_parsing = FALSE, return_empty = FALSE, - timeout_seconds = 30 + timeout_seconds = 30, + base_url = NULL, + make_call = TRUE, + debug = FALSE, + format_type = "json" ) -with_base_url(epidata_call, base_url) +fetch(epidata_call, fetch_args = fetch_args_list()) } \arguments{ \item{endpoint}{the epidata endpoint to call} @@ -33,8 +53,6 @@ with_base_url(epidata_call, base_url) \item{only_supports_classic}{if true only classic format is supported} -\item{epidata_call}{an instance of \code{epidata_call}} - \item{fields}{a list of epidata fields to return, or NULL to return all fields (default) e.g. c("time_value", "value") to return only the time_value and value fields or c("-direction") to return everything except @@ -42,11 +60,26 @@ the direction field} \item{disable_date_parsing}{disable automatic date parsing} +\item{disable_data_frame_parsing}{disable automatic conversion to data frame; this +is only supported by endpoints that only support the 'classic' format (non-tabular)} + \item{return_empty}{boolean that allows returning an empty tibble if there is no data} \item{timeout_seconds}{the maximum amount of time to wait for a response} \item{base_url}{base URL to use} + +\item{make_call}{boolean that allows skipping the call to the API and instead +returns the \code{epidata_call} object (useful for debugging)} + +\item{debug}{boolean that allows returning the raw response from the API} + +\item{format_type}{the format to request from the API, one of classic, json, csv; this +is only used by \code{fetch_debug}} + +\item{epidata_call}{an instance of \code{epidata_call}} + +\item{fetch_args}{a \code{fetch_args} object} } \value{ \itemize{ @@ -54,11 +87,11 @@ the direction field} } \itemize{ -\item For \code{fetch}: a tibble or a JSON-like list +\item For \code{fetch_args_list}: a \code{fetch_args} object } \itemize{ -\item For \code{with_base_url}: an \code{epidata_call} object +\item For \code{fetch}: a tibble or a JSON-like list } } \description{ @@ -75,8 +108,6 @@ There are some other functions available for debugging and advanced usage: \itemize{ \item \code{request_url} (for debugging): outputs the request URL from which data would be fetched (note additional parameters below) -\item \code{with_base_url} (advanced): outputs an \code{epidata_call} that requests data -from a different Epidata server } \code{fetch} usually returns the data in tibble format, but a few of the endpoints @@ -84,6 +115,7 @@ only support the JSON classic format (delphi, pvt_meta_norostat, and meta). In that case a JSON-like nested list structure is returned instead. } \examples{ +\dontrun{ covidcast( source = "jhu-csse", signals = "confirmed_7dav_incidence_prop", @@ -91,6 +123,7 @@ covidcast( geo_type = "state", time_values = epirange(20200601, 20200801), geo_values = c("ca", "fl") -) \%>\% fetch() +) +} } diff --git a/man/fetch_classic.Rd b/man/fetch_classic.Rd index dd597fec..14b30f59 100644 --- a/man/fetch_classic.Rd +++ b/man/fetch_classic.Rd @@ -5,29 +5,12 @@ \title{Fetches the data, raises on epidata errors, and returns the results as a JSON-like list} \usage{ -fetch_classic( - epidata_call, - fields = NULL, - disable_data_frame_parsing = TRUE, - return_empty = FALSE, - timeout_seconds = 30 -) +fetch_classic(epidata_call, fetch_args = fetch_args_list()) } \arguments{ \item{epidata_call}{an instance of \code{epidata_call}} -\item{fields}{a list of epidata fields to return, or NULL to return all -fields (default) e.g. c("time_value", "value") to return only the -time_value and value fields or c("-direction") to return everything except -the direction field} - -\item{disable_data_frame_parsing}{do not automatically cast the epidata -output to a data frame (some endpoints return a list of lists, which is not -a data frame)} - -\item{return_empty}{boolean that allows returning an empty tibble if there is no data.} - -\item{timeout_seconds}{the maximum amount of time to wait for a response} +\item{fetch_args}{a \code{fetch_args} object} } \value{ \itemize{ diff --git a/man/fetch_tbl.Rd b/man/fetch_tbl.Rd index 7dee090e..bb74e65e 100644 --- a/man/fetch_tbl.Rd +++ b/man/fetch_tbl.Rd @@ -4,27 +4,12 @@ \alias{fetch_tbl} \title{Fetches the data and returns a tibble} \usage{ -fetch_tbl( - epidata_call, - fields = NULL, - disable_date_parsing = FALSE, - return_empty = FALSE, - timeout_seconds = 30 -) +fetch_tbl(epidata_call, fetch_args = fetch_args_list()) } \arguments{ \item{epidata_call}{an instance of \code{epidata_call}} -\item{fields}{a list of epidata fields to return, or NULL to return all -fields (default) e.g. c("time_value", "value") to return only the -time_value and value fields or c("-direction") to return everything except -the direction field} - -\item{disable_date_parsing}{disable automatic date parsing} - -\item{return_empty}{boolean that allows returning an empty tibble if there is no data.} - -\item{timeout_seconds}{the maximum amount of time to wait for a response} +\item{fetch_args}{a \code{fetch_args} object} } \value{ \itemize{ diff --git a/man/flusurv.Rd b/man/flusurv.Rd index 44cdb3d8..fcf3a050 100644 --- a/man/flusurv.Rd +++ b/man/flusurv.Rd @@ -4,7 +4,13 @@ \alias{flusurv} \title{Fetch FluSurv hospitalization data} \usage{ -flusurv(locations, epiweeks, issues = NULL, lag = NULL) +flusurv( + locations, + epiweeks, + issues = NULL, + lag = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{locations}{character. Character vector indicating location.} @@ -16,6 +22,8 @@ most recent issue is returned. Mutually exclusive with \code{lag}.} \item{lag}{integer. Optionally, the lag of the issues to fetch. If not set, the most recent issue is returned. Mutually exclusive with \code{issues}.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -33,6 +41,6 @@ The list of location argument can be found in } \examples{ \dontrun{ -flusurv(locations = "CA", epiweeks = epirange(201701, 201801)) \%>\% fetch() +flusurv(locations = "CA", epiweeks = epirange(201701, 201801)) } } diff --git a/man/fluview.Rd b/man/fluview.Rd index 828b7b71..5af12080 100644 --- a/man/fluview.Rd +++ b/man/fluview.Rd @@ -4,7 +4,14 @@ \alias{fluview} \title{Fetch FluView data} \usage{ -fluview(regions, epiweeks, issues = NULL, lag = NULL, auth = NULL) +fluview( + regions, + epiweeks, + issues = NULL, + lag = NULL, + auth = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{regions}{character. Locations to fetch. Can be any string IDs in @@ -23,6 +30,8 @@ the most recent issue is returned. Mutually exclusive with \code{issues}.} \item{auth}{string. Optionally, restricted access key (not the same as API key).} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -40,5 +49,5 @@ The full list of location inputs can be accsssed at \url{https://github.com/cmu-delphi/delphi-epidata/blob/main/src/acquisition/fluview/fluview_locations.py}. } \examples{ -fluview(regions = "nat", epiweeks = epirange(201201, 202001)) \%>\% fetch() +fluview(regions = "nat", epiweeks = epirange(201201, 202001)) } diff --git a/man/fluview_clinical.Rd b/man/fluview_clinical.Rd index e2e495ac..5ee8b11b 100644 --- a/man/fluview_clinical.Rd +++ b/man/fluview_clinical.Rd @@ -4,7 +4,13 @@ \alias{fluview_clinical} \title{Fetch FluView virological data from clinical labs} \usage{ -fluview_clinical(regions, epiweeks, issues = NULL, lag = NULL) +fluview_clinical( + regions, + epiweeks, + issues = NULL, + lag = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{regions}{character. Regions to fetch.} @@ -18,6 +24,8 @@ most recent issue is returned. Mutually exclusive with \code{lag}.} \item{lag}{integer. Optionally, the lag of the issues to fetch. If not set, the most recent issue is returned. Mutually exclusive with \code{issues}.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -27,6 +35,6 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/fluview_clinical. } \examples{ \dontrun{ -fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701)) \%>\% fetch() +fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701)) } } diff --git a/man/fluview_meta.Rd b/man/fluview_meta.Rd index 04308b0b..ce69d797 100644 --- a/man/fluview_meta.Rd +++ b/man/fluview_meta.Rd @@ -4,7 +4,10 @@ \alias{fluview_meta} \title{Fetch FluView metadata} \usage{ -fluview_meta() +fluview_meta(fetch_args = fetch_args_list()) +} +\arguments{ +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -14,7 +17,7 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/fluview_meta.html } \examples{ \dontrun{ -fluview_meta() \%>\% fetch() +fluview_meta() } } diff --git a/man/gft.Rd b/man/gft.Rd index 719a10e1..c18b073c 100644 --- a/man/gft.Rd +++ b/man/gft.Rd @@ -4,12 +4,14 @@ \alias{gft} \title{Fetch Google Flu Trends data} \usage{ -gft(locations, epiweeks) +gft(locations, epiweeks, fetch_args = fetch_args_list()) } \arguments{ \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}} Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -30,6 +32,6 @@ and } \examples{ \dontrun{ -gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) \%>\% fetch() +gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) } } diff --git a/man/kcdc_ili.Rd b/man/kcdc_ili.Rd index c0c52961..8fde55d5 100644 --- a/man/kcdc_ili.Rd +++ b/man/kcdc_ili.Rd @@ -4,7 +4,13 @@ \alias{kcdc_ili} \title{Fetch KCDC data} \usage{ -kcdc_ili(regions, epiweeks, issues = NULL, lag = NULL) +kcdc_ili( + regions, + epiweeks, + issues = NULL, + lag = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{regions}{character. Regions to fetch.} @@ -16,6 +22,8 @@ most recent issue is returned. Mutually exclusive with \code{lag}.} \item{lag}{integer. Optionally, the lag of the issues to fetch. If not set, the most recent issue is returned. Mutually exclusive with \code{issues}.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -28,6 +36,6 @@ TODO: find a non-trivial region } \examples{ \dontrun{ -kcdc_ili(regions = "ROK", epiweeks = 200436) \%>\% fetch() +kcdc_ili(regions = "ROK", epiweeks = 200436) } } diff --git a/man/meta.Rd b/man/meta.Rd index 3bd1958d..10b652c7 100644 --- a/man/meta.Rd +++ b/man/meta.Rd @@ -4,7 +4,10 @@ \alias{meta} \title{Fetch api metadata} \usage{ -meta() +meta(fetch_args = fetch_args_list()) +} +\arguments{ +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} diff --git a/man/nidss_dengue.Rd b/man/nidss_dengue.Rd index 74cf4ca3..97b2d43b 100644 --- a/man/nidss_dengue.Rd +++ b/man/nidss_dengue.Rd @@ -4,12 +4,14 @@ \alias{nidss_dengue} \title{Fetch NIDSS dengue data} \usage{ -nidss_dengue(locations, epiweeks) +nidss_dengue(locations, epiweeks, fetch_args = fetch_args_list()) } \arguments{ \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -28,6 +30,6 @@ and } \examples{ \dontrun{ -nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 201301)) \%>\% fetch() +nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 201301)) } } diff --git a/man/nidss_flu.Rd b/man/nidss_flu.Rd index a5643698..f9998564 100644 --- a/man/nidss_flu.Rd +++ b/man/nidss_flu.Rd @@ -4,7 +4,13 @@ \alias{nidss_flu} \title{Fetch NIDSS flu data} \usage{ -nidss_flu(regions, epiweeks, issues = NULL, lag = NULL) +nidss_flu( + regions, + epiweeks, + issues = NULL, + lag = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{regions}{character. Regions to fetch.} @@ -16,6 +22,8 @@ most recent issue is returned. Mutually exclusive with \code{lag}.} \item{lag}{integer. Optionally, the lag of the issues to fetch. If not set, the most recent issue is returned. Mutually exclusive with \code{issues}.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -29,6 +37,6 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/nidss_flu.html} } \examples{ \dontrun{ -nidss_flu(regions = "taipei", epiweeks = epirange(201501, 201601)) \%>\% fetch() +nidss_flu(regions = "taipei", epiweeks = epirange(201501, 201601)) } } diff --git a/man/nowcast.Rd b/man/nowcast.Rd index 04c3c0bb..4b1d7c2a 100644 --- a/man/nowcast.Rd +++ b/man/nowcast.Rd @@ -4,12 +4,14 @@ \alias{nowcast} \title{Fetch Delphi's ILI nowcast} \usage{ -nowcast(locations, epiweeks) +nowcast(locations, epiweeks, fetch_args = fetch_args_list()) } \arguments{ \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -26,6 +28,6 @@ The full list of location inputs can be accsssed at } \examples{ \dontrun{ -nowcast(locations = "ca", epiweeks = epirange(201201, 201301)) \%>\% fetch() +nowcast(locations = "ca", epiweeks = epirange(201201, 201301)) } } diff --git a/man/paho_dengue.Rd b/man/paho_dengue.Rd index a8aea331..49be249e 100644 --- a/man/paho_dengue.Rd +++ b/man/paho_dengue.Rd @@ -4,7 +4,13 @@ \alias{paho_dengue} \title{Fetch PAHO Dengue} \usage{ -paho_dengue(regions, epiweeks, issues = NULL, lag = NULL) +paho_dengue( + regions, + epiweeks, + issues = NULL, + lag = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{regions}{character. Regions to fetch.} @@ -16,6 +22,8 @@ most recent issue is returned. Mutually exclusive with \code{lag}.} \item{lag}{integer. Optionally, the lag of the issues to fetch. If not set, the most recent issue is returned. Mutually exclusive with \code{issues}.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -25,6 +33,6 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/paho_dengue.html} } \examples{ \dontrun{ -paho_dengue(regions = "ca", epiweeks = epirange(201401, 201501)) \%>\% fetch() +paho_dengue(regions = "ca", epiweeks = epirange(201401, 201501)) } } diff --git a/man/parse_signal.Rd b/man/parse_signal.Rd index 47976dbc..31a51a14 100644 --- a/man/parse_signal.Rd +++ b/man/parse_signal.Rd @@ -11,14 +11,14 @@ parse_signal(signal, base_url) \item{signals}{data source to fetch} -\item{time_type}{data source to fetch} - -\item{time_values}{data source to fetch} - \item{geo_type}{geo_type to fetch} +\item{time_type}{data source to fetch} + \item{geo_values}{data source to fetch} +\item{time_values}{data source to fetch} + \item{as_of}{data source to fetch} \item{issues}{data source to fetch} diff --git a/man/pvt_cdc.Rd b/man/pvt_cdc.Rd index 4ada67d2..2ddf400a 100644 --- a/man/pvt_cdc.Rd +++ b/man/pvt_cdc.Rd @@ -4,7 +4,7 @@ \alias{pvt_cdc} \title{Fetch CDC page hits} \usage{ -pvt_cdc(auth, locations, epiweeks) +pvt_cdc(auth, locations, epiweeks, fetch_args = fetch_args_list()) } \arguments{ \item{auth}{string. Restricted access key (not the same as API key).} @@ -12,6 +12,9 @@ pvt_cdc(auth, locations, epiweeks) \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}. +See \code{fetch_args_list()} for details.} } \value{ \code{\link{epidata_call}} @@ -25,6 +28,6 @@ pvt_cdc( auth = "yourkey", locations = "fl,ca", epirange(201501, 201601) -) \%>\% fetch() +) } } diff --git a/man/pvt_dengue_sensors.Rd b/man/pvt_dengue_sensors.Rd index 43bb2fe5..eff8eab2 100644 --- a/man/pvt_dengue_sensors.Rd +++ b/man/pvt_dengue_sensors.Rd @@ -5,7 +5,13 @@ \title{Fetch Delphi's digital surveillance sensors for dengue in PAHO member countries} \usage{ -pvt_dengue_sensors(auth, names, locations, epiweeks) +pvt_dengue_sensors( + auth, + names, + locations, + epiweeks, + fetch_args = fetch_args_list() +) } \arguments{ \item{auth}{string. Restricted access key (not the same as API key).} @@ -15,6 +21,8 @@ pvt_dengue_sensors(auth, names, locations, epiweeks) \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -22,9 +30,6 @@ pvt_dengue_sensors(auth, names, locations, epiweeks) \description{ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/dengue_sensors.html} } -\details{ -TODO: what are valid locations and names? -} \examples{ \dontrun{ pvt_dengue_sensors( @@ -32,6 +37,6 @@ pvt_dengue_sensors( names = "ght", locations = "ag", epiweeks = epirange(201501, 202001) -) \%>\% fetch() +) } } diff --git a/man/pvt_ght.Rd b/man/pvt_ght.Rd index 4ec8dc4e..779907c7 100644 --- a/man/pvt_ght.Rd +++ b/man/pvt_ght.Rd @@ -4,7 +4,7 @@ \alias{pvt_ght} \title{Fetch Google Health Trends data} \usage{ -pvt_ght(auth, locations, epiweeks, query) +pvt_ght(auth, locations, epiweeks, query, fetch_args = fetch_args_list()) } \arguments{ \item{auth}{string. Restricted access key (not the same as API key).} @@ -14,6 +14,8 @@ pvt_ght(auth, locations, epiweeks, query) \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} \item{query}{string. The query to be fetched.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -31,6 +33,6 @@ pvt_ght( locations = "ca", epiweeks = epirange(201201, 202001), query = "how to get over the flu" -) \%>\% fetch() +) } } diff --git a/man/pvt_meta_norostat.Rd b/man/pvt_meta_norostat.Rd index 1aee23aa..015aced8 100644 --- a/man/pvt_meta_norostat.Rd +++ b/man/pvt_meta_norostat.Rd @@ -4,10 +4,12 @@ \alias{pvt_meta_norostat} \title{Fetch NoroSTAT metadata} \usage{ -pvt_meta_norostat(auth) +pvt_meta_norostat(auth, fetch_args = fetch_args_list()) } \arguments{ \item{auth}{string. Restricted access key (not the same as API key).} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -17,6 +19,6 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/meta_norostat.htm } \examples{ \dontrun{ -pvt_meta_norostat(auth = "yourkey") \%>\% fetch() +pvt_meta_norostat(auth = "yourkey") } } diff --git a/man/pvt_norostat.Rd b/man/pvt_norostat.Rd index 3a3e449b..b89df4d2 100644 --- a/man/pvt_norostat.Rd +++ b/man/pvt_norostat.Rd @@ -4,7 +4,7 @@ \alias{pvt_norostat} \title{Fetch NoroSTAT data (point data, no min/max)} \usage{ -pvt_norostat(auth, locations, epiweeks) +pvt_norostat(auth, locations, epiweeks, fetch_args = fetch_args_list()) } \arguments{ \item{auth}{string. Your authentication key.} @@ -12,6 +12,8 @@ pvt_norostat(auth, locations, epiweeks) \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -25,6 +27,6 @@ pvt_norostat( auth = "yourkey", locations = "1", epiweeks = 201233 -) \%>\% fetch() +) } } diff --git a/man/pvt_quidel.Rd b/man/pvt_quidel.Rd index 5b16b060..3aca941e 100644 --- a/man/pvt_quidel.Rd +++ b/man/pvt_quidel.Rd @@ -4,7 +4,7 @@ \alias{pvt_quidel} \title{Fetch Quidel COVID-19 and influenza testing data} \usage{ -pvt_quidel(auth, locations, epiweeks) +pvt_quidel(auth, locations, epiweeks, fetch_args = fetch_args_list()) } \arguments{ \item{auth}{string. Restricted access key (not the same as API key).} @@ -12,6 +12,8 @@ pvt_quidel(auth, locations, epiweeks) \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -25,6 +27,6 @@ pvt_quidel( auth = "yourkey", epiweeks = epirange(201201, 202001), locations = "hhs1" -) \%>\% fetch() +) } } diff --git a/man/pvt_sensors.Rd b/man/pvt_sensors.Rd index 088eec11..46905f26 100644 --- a/man/pvt_sensors.Rd +++ b/man/pvt_sensors.Rd @@ -4,7 +4,7 @@ \alias{pvt_sensors} \title{Fetch Delphi's digital surveillance sensors} \usage{ -pvt_sensors(auth, names, locations, epiweeks) +pvt_sensors(auth, names, locations, epiweeks, fetch_args = fetch_args_list()) } \arguments{ \item{auth}{string. Restricted access key (not the same as API key).} @@ -14,6 +14,8 @@ pvt_sensors(auth, names, locations, epiweeks) \item{locations}{character. Locations to fetch.} \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -28,6 +30,6 @@ pvt_sensors( names = "sar3", locations = "nat", epiweeks = epirange(201501, 202001) -) \%>\% fetch() +) } } diff --git a/man/pvt_twitter.Rd b/man/pvt_twitter.Rd index 2953c0ff..e5030631 100644 --- a/man/pvt_twitter.Rd +++ b/man/pvt_twitter.Rd @@ -4,7 +4,13 @@ \alias{pvt_twitter} \title{Fetch HealthTweets data} \usage{ -pvt_twitter(auth, locations, dates = NULL, epiweeks = NULL) +pvt_twitter( + auth, + locations, + dates = NULL, + epiweeks = NULL, + fetch_args = fetch_args_list() +) } \arguments{ \item{auth}{string. Restricted access key (not the same as API key).} @@ -15,6 +21,8 @@ pvt_twitter(auth, locations, dates = NULL, epiweeks = NULL) \item{epiweeks}{\code{\link{timeset}}. Epiweeks to fetch. Mutually exclusive with \code{dates}.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -24,6 +32,6 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/twitter.html} } \examples{ \dontrun{ -pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) \%>\% fetch() +pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) } } diff --git a/man/wiki.Rd b/man/wiki.Rd index 42ef3a2c..f13e76ac 100644 --- a/man/wiki.Rd +++ b/man/wiki.Rd @@ -4,7 +4,14 @@ \alias{wiki} \title{Fetch Wikipedia access data} \usage{ -wiki(articles, dates = NULL, epiweeks = NULL, hours = NULL, language = "en") +wiki( + articles, + dates = NULL, + epiweeks = NULL, + hours = NULL, + language = "en", + fetch_args = fetch_args_list() +) } \arguments{ \item{articles}{character. Articles to fetch.} @@ -17,6 +24,8 @@ wiki(articles, dates = NULL, epiweeks = NULL, hours = NULL, language = "en") \item{hours}{integer. Optionally, the hours to fetch.} \item{language}{string. Language to fetch.} + +\item{fetch_args}{\code{\link{fetch_args}}. Additional arguments to pass to \code{fetch()}.} } \value{ \code{\link{epidata_call}} @@ -26,6 +35,6 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/wiki.html} } \examples{ \dontrun{ -wiki(articles = "avian_influenza", epiweeks = epirange(201501, 201601)) \%>\% fetch() +wiki(articles = "avian_influenza", epiweeks = epirange(201501, 201601)) } } diff --git a/man/with_base_url.Rd b/man/with_base_url.Rd new file mode 100644 index 00000000..f6f42502 --- /dev/null +++ b/man/with_base_url.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/epidatacall.R +\name{with_base_url} +\alias{with_base_url} +\title{\code{epidata_call} object using a different base URL} +\usage{ +with_base_url(epidata_call, base_url) +} +\arguments{ +\item{epidata_call}{an instance of \code{epidata_call}} + +\item{base_url}{base URL to use} +} +\value{ +an \code{epidata_call} object +} +\description{ +\code{epidata_call} object using a different base URL +} +\keyword{internal} diff --git a/tests/testthat/test-covidcast.R b/tests/testthat/test-covidcast.R index 901abdb5..c22068fb 100644 --- a/tests/testthat/test-covidcast.R +++ b/tests/testthat/test-covidcast.R @@ -1,8 +1,21 @@ test_that("covidcast", { covidcast_api <- epidatr:::covidcast_epidata() expect_identical( - covidcast_api$sources$`fb-survey`$signals$smoothed_cli$call("nation", "us", epirange(20210405, 20210410)), - covidcast("fb-survey", "smoothed_cli", "nation", "day", "us", epirange(20210405, 20210410)) + covidcast_api$sources$`fb-survey`$signals$smoothed_cli$call( + "nation", + "us", + epirange(20210405, 20210410), + fetch_args = fetch_args_list(make_call = FALSE) + ), + covidcast( + "fb-survey", + "smoothed_cli", + "nation", + "day", + "us", + epirange(20210405, 20210410), + fetch_args = fetch_args_list(make_call = FALSE) + ) ) }) diff --git a/tests/testthat/test-endpoints.R b/tests/testthat/test-endpoints.R index a22b1018..5aadfb48 100644 --- a/tests/testthat/test-endpoints.R +++ b/tests/testthat/test-endpoints.R @@ -1,59 +1,148 @@ test_that("basic_epidata_call", { - expect_no_error(pvt_cdc(auth = "yourkey", "fl,ca", epirange(201501, 201601))) - expect_no_error(covid_hosp_facility_lookup(state = "fl")) - expect_no_error(covid_hosp_facility(hospital_pks = "100075", collection_weeks = epirange(20200101, 20200501))) - expect_no_error(covid_hosp_state_timeseries(states = "fl", dates = epirange(20200101, 20200501))) - expect_no_error(covidcast_meta()) - expect_no_error(covidcast(source = "jhu-csse", signals = "confirmed_7dav_incidence_prop", time_type = "day", geo_type = "state", time_values = epirange(20200601, 20200801), geo_values = "ca,fl")) - expect_no_error(delphi(system = "ec", epiweek = 202006)) - expect_no_error(dengue_nowcast(locations = "?", epiweeks = epirange(201501, 202001))) - expect_no_error(pvt_dengue_sensors(auth = "yourkey", names = "?", locations = "?", epiweeks = epirange(201501, 202001))) - expect_no_error(ecdc_ili(regions = "austria", epiweeks = epirange(201201, 202001))) - expect_no_error(flusurv(locations = "CA", epiweeks = epirange(201201, 202001))) - expect_no_error(fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701))) - expect_no_error(fluview_meta()) - expect_no_error(fluview(regions = "nat", epiweeks = epirange(201601, 201701))) - expect_no_error(gft(locations = "hhs1", epiweeks = epirange(201201, 202001))) - expect_no_error(pvt_ght(auth = "yourkey", locations = "ca", epiweeks = epirange(201201, 202001), query = "?")) - expect_no_error(kcdc_ili(regions = "?", epiweeks = epirange(201201, 202001))) - expect_no_error(pvt_meta_norostat(auth = "yourkey")) - expect_no_error(meta()) - expect_no_error(nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 202001))) - expect_no_error(nidss_flu(regions = "taipei", epiweeks = epirange(201201, 202001))) - expect_no_error(pvt_norostat(auth = "yourkey", locations = "Minnesota, Ohio, Oregon, Tennessee, and Wisconsin", epiweeks = epirange(201201, 202001))) - expect_no_error(nowcast(locations = "ca", epiweeks = epirange(201201, 202001))) - expect_no_error(paho_dengue(regions = "ca", epiweeks = epirange(201201, 202001))) - expect_no_error(pvt_quidel(auth = "yourkey", locations = "hhs1", epiweeks = epirange(201201, 202001))) - expect_no_error(pvt_sensors(auth = "yourkey", names = "sar3", locations = "nat", epiweeks = epirange(201501, 202001))) - expect_no_error(pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001))) - expect_no_error(wiki(articles = "avian_influenza", epiweeks = epirange(201501, 202001))) - - expect_no_error(pvt_cdc(auth = "yourkey", "fl,ca", epirange(201501, 201601)) %>% request_url()) - expect_no_error(covid_hosp_facility_lookup(state = "fl") %>% request_url()) - expect_no_error(covid_hosp_facility(hospital_pks = "100075", collection_weeks = epirange(20200101, 20200501)) %>% request_url()) - expect_no_error(covid_hosp_state_timeseries(states = "fl", dates = epirange(20200101, 20200501)) %>% request_url()) - expect_no_error(covidcast_meta() %>% request_url()) - expect_no_error(covidcast(source = "jhu-csse", signals = "confirmed_7dav_incidence_prop", time_type = "day", geo_type = "state", time_values = epirange(20200601, 20200801), geo_values = "ca,fl") %>% request_url()) - expect_no_error(delphi(system = "ec", epiweek = 202006) %>% request_url()) - expect_no_error(dengue_nowcast(locations = "?", epiweeks = epirange(201501, 202001)) %>% request_url()) - expect_no_error(pvt_dengue_sensors(auth = "yourkey", names = "?", locations = "?", epiweeks = epirange(201501, 202001)) %>% request_url()) - expect_no_error(ecdc_ili(regions = "austria", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(flusurv(locations = "CA", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701)) %>% request_url()) - expect_no_error(fluview_meta() %>% request_url()) - expect_no_error(fluview(regions = "nat", epiweeks = epirange(201601, 201701)) %>% request_url()) - expect_no_error(gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(pvt_ght(auth = "yourkey", locations = "ca", epiweeks = epirange(201201, 202001), query = "?") %>% request_url()) - expect_no_error(kcdc_ili(regions = "?", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(pvt_meta_norostat(auth = "yourkey") %>% request_url()) - expect_no_error(meta() %>% request_url()) - expect_no_error(nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(nidss_flu(regions = "taipei", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(pvt_norostat(auth = "yourkey", locations = "Minnesota, Ohio, Oregon, Tennessee, and Wisconsin", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(nowcast(locations = "ca", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(paho_dengue(regions = "ca", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(pvt_quidel(auth = "yourkey", locations = "hhs1", epiweeks = epirange(201201, 202001)) %>% request_url()) - expect_no_error(pvt_sensors(auth = "yourkey", names = "sar3", locations = "nat", epiweeks = epirange(201501, 202001)) %>% request_url()) - expect_no_error(pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) %>% request_url()) - expect_no_error(wiki(articles = "avian_influenza", epiweeks = epirange(201501, 202001)) %>% request_url()) + expect_no_error(pvt_cdc( + auth = "yourkey", + "fl,ca", + epirange(201501, 201601), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(covid_hosp_facility_lookup( + state = "fl", + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(covid_hosp_facility( + hospital_pks = "100075", + collection_weeks = epirange(20200101, 20200501), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(covid_hosp_state_timeseries( + states = "fl", + dates = epirange(20200101, 20200501), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(covidcast_meta( + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(covidcast( + source = "jhu-csse", + signals = "confirmed_7dav_incidence_prop", + time_type = "day", + geo_type = "state", + time_values = epirange(20200601, 20200801), + geo_values = "ca,fl", + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(delphi( + system = "ec", + epiweek = 202006, + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(dengue_nowcast( + locations = "?", + epiweeks = epirange(201501, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(pvt_dengue_sensors( + auth = "yourkey", + names = "?", + locations = "?", + epiweeks = epirange(201501, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(ecdc_ili( + regions = "austria", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(flusurv( + locations = "CA", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(fluview_clinical( + regions = "nat", + epiweeks = epirange(201601, 201701), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(fluview_meta( + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(fluview( + regions = "nat", + epiweeks = epirange(201601, 201701), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(gft( + locations = "hhs1", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(pvt_ght( + auth = "yourkey", + locations = "ca", + epiweeks = epirange(201201, 202001), + query = "?", + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(kcdc_ili( + regions = "?", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(pvt_meta_norostat( + auth = "yourkey", + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(meta( + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(nidss_dengue( + locations = "taipei", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(nidss_flu( + regions = "taipei", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(pvt_norostat( + auth = "yourkey", + locations = "Minnesota, Ohio, Oregon, Tennessee, and Wisconsin", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(nowcast( + locations = "ca", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(paho_dengue( + regions = "ca", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(pvt_quidel( + auth = "yourkey", + locations = "hhs1", + epiweeks = epirange(201201, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(pvt_sensors( + auth = "yourkey", + names = "sar3", + locations = "nat", + epiweeks = epirange(201501, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(pvt_twitter( + auth = "yourkey", + locations = "CA", + epiweeks = epirange(201501, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) + expect_no_error(wiki( + articles = "avian_influenza", + epiweeks = epirange(201501, 202001), + fetch_args = fetch_args_list(make_call = FALSE) + ) %>% request_url()) }) diff --git a/tests/testthat/test-epidatacall.R b/tests/testthat/test-epidatacall.R index aac38439..f520784d 100644 --- a/tests/testthat/test-epidatacall.R +++ b/tests/testthat/test-epidatacall.R @@ -1,6 +1,11 @@ test_that("request_impl http errors", { # should give a 401 error - epidata_call <- pvt_cdc(auth = "ImALittleTeapot", epiweeks = epirange(202003, 202304), locations = "ma") + epidata_call <- pvt_cdc( + auth = "ImALittleTeapot", + epiweeks = epirange(202003, 202304), + locations = "ma", + fetch_args = fetch_args_list(make_call = FALSE) + ) local_mocked_bindings( # generated via # url <- full_url(epidata_call) @@ -23,6 +28,53 @@ test_that("request_impl http errors", { expect_error(response <- epidata_call %>% request_impl("csv"), class = "http_500") }) +test_that("fetch_args", { + expect_identical( + fetch_args_list(), + structure( + list( + fields = NULL, + disable_date_parsing = FALSE, + disable_data_frame_parsing = FALSE, + return_empty = FALSE, + timeout_seconds = 30, + base_url = NULL, + make_call = TRUE, + debug = FALSE, + format_type = "json" + ), + class = "fetch_args" + ) + ) + expect_identical( + fetch_args_list( + fields = c("a", "b"), + disable_date_parsing = TRUE, + disable_data_frame_parsing = TRUE, + return_empty = TRUE, + timeout_seconds = 10, + base_url = "https://example.com", + make_call = FALSE, + debug = TRUE, + format_type = "classic" + ), + structure( + list( + fields = c("a", "b"), + disable_date_parsing = TRUE, + disable_data_frame_parsing = TRUE, + return_empty = TRUE, + timeout_seconds = 10, + base_url = "https://example.com", + make_call = FALSE, + debug = TRUE, + format_type = "classic" + ), + class = "fetch_args" + ) + ) +}) + test_that("fetch and fetch_tbl", { epidata_call <- covidcast( source = "jhu-csse", @@ -30,7 +82,8 @@ test_that("fetch and fetch_tbl", { time_type = "day", geo_type = "state", time_values = epirange("2020-06-01", "2020-08-01"), - geo_values = "ca,fl" + geo_values = "ca,fl", + fetch_args = fetch_args_list(make_call = FALSE) ) local_mocked_bindings( request_impl = function(...) NULL, @@ -58,7 +111,7 @@ test_that("fetch and fetch_tbl", { .package = "httr" ) # testing that the fields fill as expected - res <- epidata_call %>% fetch(fields = c("time_value", "value")) + res <- epidata_call %>% fetch(fetch_args_list(fields = c("time_value", "value"))) expect_equal(res, tbl_out[c("time_value", "value")]) }) @@ -69,7 +122,8 @@ test_that("fetch_tbl warns on non-success", { time_type = "day", geo_type = "state", time_values = epirange("2020-06-01", "2020-08-01"), - geo_values = "ca,fl" + geo_values = "ca,fl", + fetch_args = fetch_args_list(make_call = FALSE) ) local_mocked_bindings( @@ -98,7 +152,11 @@ test_that("fetch_tbl warns on non-success", { test_that("classic only fetch", { # delphi is an example endpoint that only suports the classic call - epidata_call <- delphi(system = "ec", epiweek = 201501) + epidata_call <- delphi( + system = "ec", + epiweek = 201501, + fetch_args = fetch_args_list(make_call = FALSE) + ) local_mocked_bindings( # generated using # epidata_call %>% diff --git a/tests/testthat/test-model.R b/tests/testthat/test-model.R index 99f548ee..770e3db5 100644 --- a/tests/testthat/test-model.R +++ b/tests/testthat/test-model.R @@ -49,7 +49,11 @@ test_that("`parse_timeset_input` on valid inputs", { test_that("null parsing", { # parse_data_frame (df[[info$name]] = NULL)-> parse_value - epidata_call <- flusurv(locations = "ca", epiweeks = 202001) + epidata_call <- flusurv( + locations = "ca", + epiweeks = 202001, + fetch_args = fetch_args_list(make_call = FALSE) + ) # mocked data generated with # epidata_call %>% # fetch_classic() %>% diff --git a/vignettes/epidatr.Rmd b/vignettes/epidatr.Rmd index ce8fbbb3..9c731cf0 100644 --- a/vignettes/epidatr.Rmd +++ b/vignettes/epidatr.Rmd @@ -44,19 +44,12 @@ In this case, the `covidcast()` function lets us specify these parameters for the endpoint: ```{r} -epicall <- covidcast( +covidcast( "fb-survey", "smoothed_cli", "state", "day", "pa", epirange(20210405, 20210410) ) ``` -The `epicall` object is now an object representing the query we wish to make. -The `fetch()` function allows us to retrieve the results as a tibble: - -```{r} -epicall %>% fetch() -``` - The [Delphi Epidata API documentation](https://cmu-delphi.github.io/delphi-epidata/) has more information on the available endpoints and arguments. Examples queries with all the endpoint functions available in this package are given [below](#example-queries). @@ -91,8 +84,7 @@ These objects can be used directly to fetch data, without requiring us to use the `covidcast()` function. Simply use the `$call` attribute of the object: ```{r} -epicall <- epidata$signals$`fb-survey:smoothed_cli`$call("state", "pa", epirange(20210405, 20210410)) -epicall %>% fetch() +epidata$signals$`fb-survey:smoothed_cli`$call("state", "pa", epirange(20210405, 20210410)) ``` ## Example Queries @@ -114,7 +106,7 @@ covidcast( time_type = "day", time_values = epirange(20201221, 20201225), geo_values = "06059" -) %>% fetch() +) ``` The `covidcast` endpoint supports `*` in its time and geo fields: @@ -127,7 +119,7 @@ covidcast( time_type = "day", time_values = epirange(20201221, 20201225), geo_values = "*" -) %>% fetch() +) ``` ### Other Covid Endpoints @@ -137,10 +129,10 @@ covidcast( API docs: ```{r, eval = FALSE} -covid_hosp_facility_lookup(city = "southlake") %>% fetch() -covid_hosp_facility_lookup(state = "WY") %>% fetch() +covid_hosp_facility_lookup(city = "southlake") +covid_hosp_facility_lookup(state = "WY") # A non-example (there is no city called New York in Wyoming) -covid_hosp_facility_lookup(state = "WY", city = "New York") %>% fetch() +covid_hosp_facility_lookup(state = "WY", city = "New York") ``` #### COVID-19 Hospitalization by Facility @@ -151,7 +143,7 @@ API docs: % fetch() +) ``` #### COVID-19 Hospitalization by State @@ -159,7 +151,7 @@ covid_hosp_facility( API docs: ```{r, eval = FALSE} -covid_hosp_state_timeseries(states = "MA", dates = "20200510") %>% fetch() +covid_hosp_state_timeseries(states = "MA", dates = "20200510") ``` ### Flu Endpoints @@ -169,7 +161,7 @@ covid_hosp_state_timeseries(states = "MA", dates = "20200510") %>% fetch() API docs: ```{r, eval = FALSE} -del <- delphi(system = "ec", epiweek = 201501) %>% fetch() +del <- delphi(system = "ec", epiweek = 201501) names(del[[1L]]$forecast) ``` @@ -178,7 +170,7 @@ names(del[[1L]]$forecast) API docs: ```{r, eval = FALSE} -flusurv(locations = "ca", epiweeks = 202001) %>% fetch() +flusurv(locations = "ca", epiweeks = 202001) ``` #### Fluview data @@ -186,7 +178,7 @@ flusurv(locations = "ca", epiweeks = 202001) %>% fetch() API docs: ```{r, eval = FALSE} -fluview(regions = "nat", epiweeks = epirange(201201, 202001)) %>% fetch() +fluview(regions = "nat", epiweeks = epirange(201201, 202001)) ``` #### Fluview virological data from clinical labs @@ -194,7 +186,7 @@ fluview(regions = "nat", epiweeks = epirange(201201, 202001)) %>% fetch() API docs: ```{r, eval = FALSE} -fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701)) %>% fetch() +fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701)) ``` #### Fluview metadata @@ -202,7 +194,7 @@ fluview_clinical(regions = "nat", epiweeks = epirange(201601, 201701)) %>% fetch API docs: ```{r, eval = FALSE} -fluview_meta() %>% fetch() +fluview_meta() ``` #### Google Flu Trends data @@ -210,7 +202,7 @@ fluview_meta() %>% fetch() API docs: ```{r, eval = FALSE} -gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) %>% fetch() +gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) ``` #### ECDC ILI @@ -218,7 +210,7 @@ gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) %>% fetch() API docs: ```{r, eval = FALSE} -ecdc_ili(regions = "Armenia", epiweeks = 201840) %>% fetch() +ecdc_ili(regions = "Armenia", epiweeks = 201840) ``` #### KCDC ILI @@ -226,7 +218,7 @@ ecdc_ili(regions = "Armenia", epiweeks = 201840) %>% fetch() API docs: ```{r, eval = FALSE} -kcdc_ili(regions = "ROK", epiweeks = 200436) %>% fetch() +kcdc_ili(regions = "ROK", epiweeks = 200436) ``` #### NIDSS Flu @@ -234,7 +226,7 @@ kcdc_ili(regions = "ROK", epiweeks = 200436) %>% fetch() API docs: ```{r, eval = FALSE} -nidss_flu(regions = "taipei", epiweeks = epirange(200901, 201301)) %>% fetch() +nidss_flu(regions = "taipei", epiweeks = epirange(200901, 201301)) ``` #### ILI Nearby Nowcast @@ -242,7 +234,7 @@ nidss_flu(regions = "taipei", epiweeks = epirange(200901, 201301)) %>% fetch() API docs: ```{r, eval = FALSE} -nowcast(locations = "ca", epiweeks = epirange(202201, 202319)) %>% fetch() +nowcast(locations = "ca", epiweeks = epirange(202201, 202319)) ``` ### Dengue Endpoints @@ -252,7 +244,7 @@ nowcast(locations = "ca", epiweeks = epirange(202201, 202319)) %>% fetch() API docs: ```{r, eval = FALSE} -dengue_nowcast(locations = "pr", epiweeks = epirange(201401, 202301)) %>% fetch() +dengue_nowcast(locations = "pr", epiweeks = epirange(201401, 202301)) ``` #### NIDSS dengue @@ -260,7 +252,7 @@ dengue_nowcast(locations = "pr", epiweeks = epirange(201401, 202301)) %>% fetch( API docs: ```{r, eval = FALSE} -nidss_dengue(locations = "taipei", epiweeks = epirange(200301, 201301)) %>% fetch() +nidss_dengue(locations = "taipei", epiweeks = epirange(200301, 201301)) ``` ### PAHO Dengue @@ -268,7 +260,7 @@ nidss_dengue(locations = "taipei", epiweeks = epirange(200301, 201301)) %>% fetc API docs: ```{r, eval=FALSE} -paho_dengue(regions = "ca", epiweeks = epirange(200201, 202319)) %>% fetch() +paho_dengue(regions = "ca", epiweeks = epirange(200201, 202319)) ``` ### Other Endpoints @@ -278,7 +270,7 @@ paho_dengue(regions = "ca", epiweeks = epirange(200201, 202319)) %>% fetch() API docs: ```{r, eval = FALSE} -wiki(language = "en", articles = "influenza", epiweeks = epirange(202001, 202319)) %>% fetch() +wiki(language = "en", articles = "influenza", epiweeks = epirange(202001, 202319)) ``` ### Private methods @@ -291,7 +283,7 @@ To actually run these locally, you will need to store these secrets in your `.Re API docs: ```{r, eval=FALSE} -pvt_cdc(auth = Sys.getenv("SECRET_API_AUTH_CDC"), epiweeks = epirange(202003, 202304), locations = "ma") %>% fetch() +pvt_cdc(auth = Sys.getenv("SECRET_API_AUTH_CDC"), epiweeks = epirange(202003, 202304), locations = "ma") ``` #### Dengue Digital Surveillance Sensors @@ -304,7 +296,7 @@ pvt_dengue_sensors( names = "ght", locations = "ag", epiweeks = epirange(201404, 202004) -) %>% fetch() +) ``` #### Google Health Trends @@ -317,7 +309,7 @@ pvt_ght( epiweeks = epirange(199301, 202304), locations = "ma", query = "how to get over the flu" -) %>% fetch() +) ``` #### NoroSTAT metadata @@ -325,7 +317,7 @@ pvt_ght( API docs: ```{r, eval=FALSE} -pvt_meta_norostat(auth = Sys.getenv("SECRET_API_AUTH_NOROSTAT")) %>% fetch() +pvt_meta_norostat(auth = Sys.getenv("SECRET_API_AUTH_NOROSTAT")) ``` #### NoroSTAT data @@ -333,7 +325,7 @@ pvt_meta_norostat(auth = Sys.getenv("SECRET_API_AUTH_NOROSTAT")) %>% fetch() API docs: ```{r, eval=FALSE} -pvt_norostat(auth = Sys.getenv("SECRET_API_AUTH_NOROSTAT"), locations = "1", epiweeks = 201233) %>% fetch() +pvt_norostat(auth = Sys.getenv("SECRET_API_AUTH_NOROSTAT"), locations = "1", epiweeks = 201233) ``` #### Quidel Influenza testing @@ -341,7 +333,7 @@ pvt_norostat(auth = Sys.getenv("SECRET_API_AUTH_NOROSTAT"), locations = "1", epi API docs: ```{r, eval=FALSE} -pvt_quidel(auth = Sys.getenv("SECRET_API_AUTH_QUIDEL"), locations = "hhs1", epiweeks = epirange(200301, 202105)) %>% fetch() +pvt_quidel(auth = Sys.getenv("SECRET_API_AUTH_QUIDEL"), locations = "hhs1", epiweeks = epirange(200301, 202105)) ``` #### Sensors @@ -354,7 +346,7 @@ pvt_sensors( names = "sar3", locations = "nat", epiweeks = epirange(200301, 202105) -) %>% fetch() +) ``` #### Twitter @@ -366,6 +358,6 @@ pvt_twitter( auth = Sys.getenv("SECRET_API_AUTH_TWITTER"), locations = "nat", epiweeks = epirange(200301, 202105) -) %>% fetch() +) ``` From aab10f1176320e52214d39ee308c136ec0a5319b Mon Sep 17 00:00:00 2001 From: dsweber2 Date: Tue, 29 Aug 2023 13:07:47 -0700 Subject: [PATCH 02/15] docs: fetch_args_list its own page, various minor --- R/epidatacall.R | 55 +++++++++++++++++------------- README.md | 6 ++-- _pkgdown.yml | 1 + man/epidata_call.Rd | 83 ++++++++------------------------------------- 4 files changed, 49 insertions(+), 96 deletions(-) diff --git a/R/epidatacall.R b/R/epidatacall.R index 404c574c..2a8cc666 100644 --- a/R/epidatacall.R +++ b/R/epidatacall.R @@ -3,29 +3,33 @@ #' @aliases epidata_call #' #' @description -#' `epidata_call` objects are generated by endpoint functions like -#' [`covidcast`]; should be piped into the `fetch` function to fetch and format -#' the data. For most endpoints this will return a tibble, but a few -#' non-COVIDCAST endpoints only support will return a JSON-like list instead. +#' `epidata_call` objects are generated internally by endpoint functions like +#' [`covidcast`]; by default, they are piped directly into the `fetch` +#' function to fetch and format the data. For most endpoints this will return +#' a tibble, but a few non-COVIDCAST endpoints only support will return a +#' JSON-like list instead. #' #' `create_epidata_call` is the constructor for `epidata_call` objects, but you -#' should not need to use it directly; instead, use an endpoint function, e.g., -#' [`covidcast`], to generate an `epidata_call` for the data of interest. +#' should not need to use it directly; instead, use an endpoint function, +#' e.g., [`covidcast`], to generate an `epidata_call` for the data of +#' interest. #' -#' There are some other functions available for debugging and advanced usage: -#' - `request_url` (for debugging): outputs the request URL from which data -#' would be fetched (note additional parameters below) +#' There are some other functions available for debugging and advanced usage: - +#' `request_url` (for debugging): outputs the request URL from which data +#' would be fetched (note additional parameters below) #' #' @examples #' \dontrun{ -#' covidcast( +#' call <- covidcast( #' source = "jhu-csse", #' signals = "confirmed_7dav_incidence_prop", #' time_type = "day", #' geo_type = "state", #' time_values = epirange(20200601, 20200801), -#' geo_values = c("ca", "fl") +#' geo_values = c("ca", "fl"), +#' fetch_args = fetch_args_list(make_call = FALSE) #' ) +#' call %>% fetch() #' } #' #' @param endpoint the epidata endpoint to call @@ -98,8 +102,8 @@ print.epidata_call <- function(x, ...) { )) } -#' Fetch args -#' @rdname epidata_call +#' Customize fetch settings +#' @rdname fetch_args_list #' @aliases fetch_args #' #' A constructor for `fetch_args` objects, which are used to pass arguments to @@ -107,19 +111,24 @@ print.epidata_call <- function(x, ...) { #' #' @param fields a list of epidata fields to return, or NULL to return all #' fields (default) e.g. c("time_value", "value") to return only the -#' time_value and value fields or c("-direction") to return everything except -#' the direction field -#' @param disable_date_parsing disable automatic date parsing +#' time_value and value fields or c("-direction") to return everything except +#' the direction field +#' @param disable_date_parsing disable automatic date parsing; by default `FALSE` #' @param disable_data_frame_parsing disable automatic conversion to data frame; this -#' is only supported by endpoints that only support the 'classic' format (non-tabular) -#' @param return_empty boolean that allows returning an empty tibble if there is no data -#' @param timeout_seconds the maximum amount of time to wait for a response -#' @param base_url base URL to use +#' is only supported by endpoints that only support the 'classic' format (non-tabular). +#' by default `FALSE` +#' @param return_empty boolean that allows returning an empty tibble if there is no data; +#' by default `FALSE` +#' @param timeout_seconds the maximum amount of time to wait for a response; by default +#' `FALSE` +#' @param base_url base URL to use; by default `NULL`, which means the global base url +#' `"https://api.delphi.cmu.edu/epidata/"` #' @param make_call boolean that allows skipping the call to the API and instead -#' returns the `epidata_call` object (useful for debugging) -#' @param debug boolean that allows returning the raw response from the API +#' returns the `epidata_call` object (useful for debugging); by default `TRUE` +#' @param debug boolean that allows returning the raw response from the API; by default +#' `FALSE` #' @param format_type the format to request from the API, one of classic, json, csv; this -#' is only used by `fetch_debug` +#' is only used by `fetch_debug`, and by default is `"json"` #' @return #' - For `fetch_args_list`: a `fetch_args` object #' @export diff --git a/README.md b/README.md index 72699a8a..229bdc6f 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,12 @@ epicall <- covidcast( geo_values = "us", time_values = epirange(20210405, 20210410) ) -epicall %>% fetch() ``` ``` # A tibble: 6 × 15 geo_value signal source geo_type time_type time_value - + 1 us smoothed_cli fb-surv… nation day 2021-04-05 2 us smoothed_cli fb-surv… nation day 2021-04-06 3 us smoothed_cli fb-surv… nation day 2021-04-07 @@ -64,8 +63,7 @@ will read by default. Note that for the time being, the private endpoints (i.e. those prefixed with `pvt`) will require a separate key that needs to be passed as an argument. - [mit-image]: https://img.shields.io/badge/License-MIT-yellow.svg [mit-url]: https://opensource.org/licenses/MIT [github-actions-image]: https://github.com/cmu-delphi/epidatr/workflows/ci/badge.svg -[github-actions-url]: https://github.com/cmu-delphi/epidatr/actions \ No newline at end of file +[github-actions-url]: https://github.com/cmu-delphi/epidatr/actions diff --git a/_pkgdown.yml b/_pkgdown.yml index 1a3ae552..8bc9144d 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -18,6 +18,7 @@ reference: desc: Query Delphi Epidata endpoints - contents: - epidata_call + - fetch_args_list - covid_hosp_facility - covid_hosp_facility_lookup - covid_hosp_state_timeseries diff --git a/man/epidata_call.Rd b/man/epidata_call.Rd index 79204c74..f044c52c 100644 --- a/man/epidata_call.Rd +++ b/man/epidata_call.Rd @@ -3,22 +3,6 @@ \name{create_epidata_call} \alias{create_epidata_call} \alias{epidata_call} -\alias{fetch_args_list} -\alias{fetch_args} -\alias{A} -\alias{constructor} -\alias{for} -\alias{`fetch_args`} -\alias{objects,} -\alias{which} -\alias{are} -\alias{used} -\alias{to} -\alias{pass} -\alias{arguments} -\alias{the} -\alias{`fetch`} -\alias{function.} \alias{fetch} \title{An abstraction that holds information needed to make an epidata request} \usage{ @@ -29,19 +13,6 @@ create_epidata_call( only_supports_classic = FALSE ) -fetch_args_list( - ..., - fields = NULL, - disable_date_parsing = FALSE, - disable_data_frame_parsing = FALSE, - return_empty = FALSE, - timeout_seconds = 30, - base_url = NULL, - make_call = TRUE, - debug = FALSE, - format_type = "json" -) - fetch(epidata_call, fetch_args = fetch_args_list()) } \arguments{ @@ -53,30 +24,6 @@ fetch(epidata_call, fetch_args = fetch_args_list()) \item{only_supports_classic}{if true only classic format is supported} -\item{fields}{a list of epidata fields to return, or NULL to return all -fields (default) e.g. c("time_value", "value") to return only the -time_value and value fields or c("-direction") to return everything except -the direction field} - -\item{disable_date_parsing}{disable automatic date parsing} - -\item{disable_data_frame_parsing}{disable automatic conversion to data frame; this -is only supported by endpoints that only support the 'classic' format (non-tabular)} - -\item{return_empty}{boolean that allows returning an empty tibble if there is no data} - -\item{timeout_seconds}{the maximum amount of time to wait for a response} - -\item{base_url}{base URL to use} - -\item{make_call}{boolean that allows skipping the call to the API and instead -returns the \code{epidata_call} object (useful for debugging)} - -\item{debug}{boolean that allows returning the raw response from the API} - -\item{format_type}{the format to request from the API, one of classic, json, csv; this -is only used by \code{fetch_debug}} - \item{epidata_call}{an instance of \code{epidata_call}} \item{fetch_args}{a \code{fetch_args} object} @@ -86,29 +33,25 @@ is only used by \code{fetch_debug}} \item For \code{create_epidata_call}: an \code{epidata_call} object } -\itemize{ -\item For \code{fetch_args_list}: a \code{fetch_args} object -} - \itemize{ \item For \code{fetch}: a tibble or a JSON-like list } } \description{ -\code{epidata_call} objects are generated by endpoint functions like -\code{\link{covidcast}}; should be piped into the \code{fetch} function to fetch and format -the data. For most endpoints this will return a tibble, but a few -non-COVIDCAST endpoints only support will return a JSON-like list instead. +\code{epidata_call} objects are generated internally by endpoint functions like +\code{\link{covidcast}}; by default, they are piped directly into the \code{fetch} +function to fetch and format the data. For most endpoints this will return +a tibble, but a few non-COVIDCAST endpoints only support will return a +JSON-like list instead. \code{create_epidata_call} is the constructor for \code{epidata_call} objects, but you -should not need to use it directly; instead, use an endpoint function, e.g., -\code{\link{covidcast}}, to generate an \code{epidata_call} for the data of interest. +should not need to use it directly; instead, use an endpoint function, +e.g., \code{\link{covidcast}}, to generate an \code{epidata_call} for the data of +interest. -There are some other functions available for debugging and advanced usage: -\itemize{ -\item \code{request_url} (for debugging): outputs the request URL from which data +There are some other functions available for debugging and advanced usage: - +\code{request_url} (for debugging): outputs the request URL from which data would be fetched (note additional parameters below) -} \code{fetch} usually returns the data in tibble format, but a few of the endpoints only support the JSON classic format (delphi, pvt_meta_norostat, and meta). @@ -116,14 +59,16 @@ In that case a JSON-like nested list structure is returned instead. } \examples{ \dontrun{ -covidcast( +call <- covidcast( source = "jhu-csse", signals = "confirmed_7dav_incidence_prop", time_type = "day", geo_type = "state", time_values = epirange(20200601, 20200801), - geo_values = c("ca", "fl") + geo_values = c("ca", "fl"), + fetch_args = fetch_args_list(make_call = FALSE) ) +call \%>\% fetch() } } From 72306ffac5b5d21fb74ec72dc8145f6063480222 Mon Sep 17 00:00:00 2001 From: dsweber2 Date: Tue, 29 Aug 2023 13:23:07 -0700 Subject: [PATCH 03/15] docs+fix: make_call->dry_run, fixed pkgdown name --- R/epidatacall.R | 12 +++---- _pkgdown.yml | 2 +- man/epidata_call.Rd | 2 +- tests/testthat/test-covidcast.R | 4 +-- tests/testthat/test-endpoints.R | 56 +++++++++++++++---------------- tests/testthat/test-epidatacall.R | 14 ++++---- tests/testthat/test-model.R | 2 +- 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/R/epidatacall.R b/R/epidatacall.R index 2a8cc666..ee47f7f6 100644 --- a/R/epidatacall.R +++ b/R/epidatacall.R @@ -27,7 +27,7 @@ #' geo_type = "state", #' time_values = epirange(20200601, 20200801), #' geo_values = c("ca", "fl"), -#' fetch_args = fetch_args_list(make_call = FALSE) +#' fetch_args = fetch_args_list(dry_run = TRUE) #' ) #' call %>% fetch() #' } @@ -123,7 +123,7 @@ print.epidata_call <- function(x, ...) { #' `FALSE` #' @param base_url base URL to use; by default `NULL`, which means the global base url #' `"https://api.delphi.cmu.edu/epidata/"` -#' @param make_call boolean that allows skipping the call to the API and instead +#' @param dry_run boolean that allows skipping the call to the API and instead #' returns the `epidata_call` object (useful for debugging); by default `TRUE` #' @param debug boolean that allows returning the raw response from the API; by default #' `FALSE` @@ -143,7 +143,7 @@ fetch_args_list <- function( return_empty = FALSE, timeout_seconds = 30, base_url = NULL, - make_call = TRUE, + dry_run = FALSE, debug = FALSE, format_type = "json") { assert_character(fields, null.ok = TRUE, len = NULL, any.missing = FALSE) @@ -152,7 +152,7 @@ fetch_args_list <- function( assert_logical(return_empty, null.ok = TRUE, len = 1L, any.missing = FALSE) assert_numeric(timeout_seconds, null.ok = TRUE, len = 1L, any.missing = FALSE) assert_character(base_url, null.ok = TRUE, len = 1L, any.missing = FALSE) - assert_logical(make_call, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_logical(dry_run, null.ok = FALSE, len = 1L, any.missing = TRUE) assert_logical(debug, null.ok = TRUE, len = 1L, any.missing = FALSE) assert_character(format_type, null.ok = TRUE, len = 1L, any.missing = FALSE) assert(format_type %in% c("json", "csv", "classic"), "format_type must be one of json, csv, classic") @@ -165,7 +165,7 @@ fetch_args_list <- function( return_empty = return_empty, timeout_seconds = timeout_seconds, base_url = base_url, - make_call = make_call, + dry_run = dry_run, debug = debug, format_type = format_type ), @@ -194,7 +194,7 @@ fetch <- function(epidata_call, fetch_args = fetch_args_list()) { epidata_call <- with_base_url(epidata_call, fetch_args$base_url) } - if (!fetch_args$make_call) { + if (!fetch_args$dry_run) { return(epidata_call) } diff --git a/_pkgdown.yml b/_pkgdown.yml index 8bc9144d..0105ea40 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -18,7 +18,7 @@ reference: desc: Query Delphi Epidata endpoints - contents: - epidata_call - - fetch_args_list + - fetch_args - covid_hosp_facility - covid_hosp_facility_lookup - covid_hosp_state_timeseries diff --git a/man/epidata_call.Rd b/man/epidata_call.Rd index f044c52c..00fa0fe0 100644 --- a/man/epidata_call.Rd +++ b/man/epidata_call.Rd @@ -66,7 +66,7 @@ call <- covidcast( geo_type = "state", time_values = epirange(20200601, 20200801), geo_values = c("ca", "fl"), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) call \%>\% fetch() } diff --git a/tests/testthat/test-covidcast.R b/tests/testthat/test-covidcast.R index c22068fb..4025aead 100644 --- a/tests/testthat/test-covidcast.R +++ b/tests/testthat/test-covidcast.R @@ -5,7 +5,7 @@ test_that("covidcast", { "nation", "us", epirange(20210405, 20210410), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ), covidcast( "fb-survey", @@ -14,7 +14,7 @@ test_that("covidcast", { "day", "us", epirange(20210405, 20210410), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) ) }) diff --git a/tests/testthat/test-endpoints.R b/tests/testthat/test-endpoints.R index 5aadfb48..3812314b 100644 --- a/tests/testthat/test-endpoints.R +++ b/tests/testthat/test-endpoints.R @@ -3,24 +3,24 @@ test_that("basic_epidata_call", { auth = "yourkey", "fl,ca", epirange(201501, 201601), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(covid_hosp_facility_lookup( state = "fl", - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(covid_hosp_facility( hospital_pks = "100075", collection_weeks = epirange(20200101, 20200501), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(covid_hosp_state_timeseries( states = "fl", dates = epirange(20200101, 20200501), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(covidcast_meta( - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(covidcast( source = "jhu-csse", @@ -29,120 +29,120 @@ test_that("basic_epidata_call", { geo_type = "state", time_values = epirange(20200601, 20200801), geo_values = "ca,fl", - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(delphi( system = "ec", epiweek = 202006, - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(dengue_nowcast( locations = "?", epiweeks = epirange(201501, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(pvt_dengue_sensors( auth = "yourkey", names = "?", locations = "?", epiweeks = epirange(201501, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(ecdc_ili( regions = "austria", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(flusurv( locations = "CA", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(fluview_clinical( regions = "nat", epiweeks = epirange(201601, 201701), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(fluview_meta( - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(fluview( regions = "nat", epiweeks = epirange(201601, 201701), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(gft( locations = "hhs1", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(pvt_ght( auth = "yourkey", locations = "ca", epiweeks = epirange(201201, 202001), query = "?", - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(kcdc_ili( regions = "?", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(pvt_meta_norostat( auth = "yourkey", - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(meta( - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(nidss_dengue( locations = "taipei", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(nidss_flu( regions = "taipei", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(pvt_norostat( auth = "yourkey", locations = "Minnesota, Ohio, Oregon, Tennessee, and Wisconsin", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(nowcast( locations = "ca", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(paho_dengue( regions = "ca", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(pvt_quidel( auth = "yourkey", locations = "hhs1", epiweeks = epirange(201201, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(pvt_sensors( auth = "yourkey", names = "sar3", locations = "nat", epiweeks = epirange(201501, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(pvt_twitter( auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) expect_no_error(wiki( articles = "avian_influenza", epiweeks = epirange(201501, 202001), - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) %>% request_url()) }) diff --git a/tests/testthat/test-epidatacall.R b/tests/testthat/test-epidatacall.R index f520784d..1e95a8a4 100644 --- a/tests/testthat/test-epidatacall.R +++ b/tests/testthat/test-epidatacall.R @@ -4,7 +4,7 @@ test_that("request_impl http errors", { auth = "ImALittleTeapot", epiweeks = epirange(202003, 202304), locations = "ma", - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) local_mocked_bindings( # generated via @@ -39,7 +39,7 @@ test_that("fetch_args", { return_empty = FALSE, timeout_seconds = 30, base_url = NULL, - make_call = TRUE, + dry_run = FALSE, debug = FALSE, format_type = "json" ), @@ -54,7 +54,7 @@ test_that("fetch_args", { return_empty = TRUE, timeout_seconds = 10, base_url = "https://example.com", - make_call = FALSE, + dry_run = TRUE, debug = TRUE, format_type = "classic" ), @@ -66,7 +66,7 @@ test_that("fetch_args", { return_empty = TRUE, timeout_seconds = 10, base_url = "https://example.com", - make_call = FALSE, + dry_run = TRUE, debug = TRUE, format_type = "classic" ), @@ -83,7 +83,7 @@ test_that("fetch and fetch_tbl", { geo_type = "state", time_values = epirange("2020-06-01", "2020-08-01"), geo_values = "ca,fl", - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) local_mocked_bindings( request_impl = function(...) NULL, @@ -123,7 +123,7 @@ test_that("fetch_tbl warns on non-success", { geo_type = "state", time_values = epirange("2020-06-01", "2020-08-01"), geo_values = "ca,fl", - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) local_mocked_bindings( @@ -155,7 +155,7 @@ test_that("classic only fetch", { epidata_call <- delphi( system = "ec", epiweek = 201501, - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) local_mocked_bindings( # generated using diff --git a/tests/testthat/test-model.R b/tests/testthat/test-model.R index 770e3db5..12dee187 100644 --- a/tests/testthat/test-model.R +++ b/tests/testthat/test-model.R @@ -52,7 +52,7 @@ test_that("null parsing", { epidata_call <- flusurv( locations = "ca", epiweeks = 202001, - fetch_args = fetch_args_list(make_call = FALSE) + fetch_args = fetch_args_list(dry_run = TRUE) ) # mocked data generated with # epidata_call %>% From e9ba7140caa7b32d7a4ae1339c43a07948113f98 Mon Sep 17 00:00:00 2001 From: dsweber2 Date: Tue, 29 Aug 2023 14:16:17 -0700 Subject: [PATCH 04/15] fix: actually change `dry_run` logic --- R/epidatacall.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/epidatacall.R b/R/epidatacall.R index ee47f7f6..d02ae07d 100644 --- a/R/epidatacall.R +++ b/R/epidatacall.R @@ -194,7 +194,7 @@ fetch <- function(epidata_call, fetch_args = fetch_args_list()) { epidata_call <- with_base_url(epidata_call, fetch_args$base_url) } - if (!fetch_args$dry_run) { + if (fetch_args$dry_run) { return(epidata_call) } From b3cbd5d8bab348951a90900d25679ffffb7a4184 Mon Sep 17 00:00:00 2001 From: dsweber2 Date: Tue, 29 Aug 2023 14:56:15 -0700 Subject: [PATCH 05/15] CI: fixing pkgdown --- _pkgdown.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pkgdown.yml b/_pkgdown.yml index 0105ea40..8bc9144d 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -18,7 +18,7 @@ reference: desc: Query Delphi Epidata endpoints - contents: - epidata_call - - fetch_args + - fetch_args_list - covid_hosp_facility - covid_hosp_facility_lookup - covid_hosp_state_timeseries From 65b730262a49e888ca7e142ee26d829a261b3c23 Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Tue, 29 Aug 2023 15:12:59 -0700 Subject: [PATCH 06/15] fix: angry, very angry --- man/fetch_args_list.Rd | 74 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 man/fetch_args_list.Rd diff --git a/man/fetch_args_list.Rd b/man/fetch_args_list.Rd new file mode 100644 index 00000000..52fe78e3 --- /dev/null +++ b/man/fetch_args_list.Rd @@ -0,0 +1,74 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/epidatacall.R +\name{fetch_args_list} +\alias{fetch_args_list} +\alias{fetch_args} +\alias{A} +\alias{constructor} +\alias{for} +\alias{`fetch_args`} +\alias{objects,} +\alias{which} +\alias{are} +\alias{used} +\alias{to} +\alias{pass} +\alias{arguments} +\alias{the} +\alias{`fetch`} +\alias{function.} +\title{Customize fetch settings} +\usage{ +fetch_args_list( + ..., + fields = NULL, + disable_date_parsing = FALSE, + disable_data_frame_parsing = FALSE, + return_empty = FALSE, + timeout_seconds = 30, + base_url = NULL, + dry_run = FALSE, + debug = FALSE, + format_type = "json" +) +} +\arguments{ +\item{...}{not used for values, forces later arguments to bind by name} + +\item{fields}{a list of epidata fields to return, or NULL to return all +fields (default) e.g. c("time_value", "value") to return only the +time_value and value fields or c("-direction") to return everything except +the direction field} + +\item{disable_date_parsing}{disable automatic date parsing; by default \code{FALSE}} + +\item{disable_data_frame_parsing}{disable automatic conversion to data frame; this +is only supported by endpoints that only support the 'classic' format (non-tabular). +by default \code{FALSE}} + +\item{return_empty}{boolean that allows returning an empty tibble if there is no data; +by default \code{FALSE}} + +\item{timeout_seconds}{the maximum amount of time to wait for a response; by default +\code{FALSE}} + +\item{base_url}{base URL to use; by default \code{NULL}, which means the global base url +\code{"https://api.delphi.cmu.edu/epidata/"}} + +\item{dry_run}{boolean that allows skipping the call to the API and instead +returns the \code{epidata_call} object (useful for debugging); by default \code{TRUE}} + +\item{debug}{boolean that allows returning the raw response from the API; by default +\code{FALSE}} + +\item{format_type}{the format to request from the API, one of classic, json, csv; this +is only used by \code{fetch_debug}, and by default is \code{"json"}} +} +\value{ +\itemize{ +\item For \code{fetch_args_list}: a \code{fetch_args} object +} +} +\description{ +Customize fetch settings +} From aacd75617d60f3c897e684faa2bcb5edab0c4c10 Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Tue, 29 Aug 2023 15:13:52 -0700 Subject: [PATCH 07/15] doc+fix: minor minor validation things --- R/epidatacall.R | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/R/epidatacall.R b/R/epidatacall.R index d02ae07d..83c67970 100644 --- a/R/epidatacall.R +++ b/R/epidatacall.R @@ -109,6 +109,7 @@ print.epidata_call <- function(x, ...) { #' A constructor for `fetch_args` objects, which are used to pass arguments to #' the `fetch` function. #' +#' @param ... not used for values, forces later arguments to bind by name #' @param fields a list of epidata fields to return, or NULL to return all #' fields (default) e.g. c("time_value", "value") to return only the #' time_value and value fields or c("-direction") to return everything except @@ -146,15 +147,15 @@ fetch_args_list <- function( dry_run = FALSE, debug = FALSE, format_type = "json") { - assert_character(fields, null.ok = TRUE, len = NULL, any.missing = FALSE) - assert_logical(disable_date_parsing, null.ok = TRUE, len = 1L, any.missing = FALSE) - assert_logical(disable_data_frame_parsing, null.ok = TRUE, len = 1L, any.missing = FALSE) - assert_logical(return_empty, null.ok = TRUE, len = 1L, any.missing = FALSE) - assert_numeric(timeout_seconds, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_character(fields, null.ok = TRUE, any.missing = FALSE) + assert_logical(disable_date_parsing, null.ok = FALSE, len = 1L, any.missing = FALSE) + assert_logical(disable_data_frame_parsing, null.ok = FALSE, len = 1L, any.missing = FALSE) + assert_logical(return_empty, null.ok = FALSE, len = 1L, any.missing = FALSE) + assert_numeric(timeout_seconds, null.ok = FALSE, len = 1L, any.missing = FALSE) assert_character(base_url, null.ok = TRUE, len = 1L, any.missing = FALSE) assert_logical(dry_run, null.ok = FALSE, len = 1L, any.missing = TRUE) - assert_logical(debug, null.ok = TRUE, len = 1L, any.missing = FALSE) - assert_character(format_type, null.ok = TRUE, len = 1L, any.missing = FALSE) + assert_logical(debug, null.ok = FALSE, len = 1L, any.missing = FALSE) + assert_character(format_type, null.ok = FALSE, len = 1L, any.missing = FALSE) assert(format_type %in% c("json", "csv", "classic"), "format_type must be one of json, csv, classic") structure( From f6edcf92648fc936e92f42f1f4dd5ff651fca87e Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Tue, 29 Aug 2023 15:28:44 -0700 Subject: [PATCH 08/15] ci: add automated chore actions --- .github/workflows/ci.yml | 12 +----- .github/workflows/document.yaml | 42 +++++++++++++++++++ .github/workflows/lint.yaml | 32 +++++++++++++++ .github/workflows/style.yaml | 73 +++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/document.yaml create mode 100644 .github/workflows/lint.yaml create mode 100644 .github/workflows/style.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be44f0a8..211ca30e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,17 +19,7 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: any::devtools, any::rcmdcheck, any::styler, any::lintr, local::. - - - name: Style / Format - shell: Rscript {0} - run: styler::style_pkg(dry="fail") - - - name: Lint - shell: Rscript {0} - run: | - devtools::load_all() - lintr::lint_package('.') + extra-packages: any::devtools, any::rcmdcheck, local::. - uses: r-lib/actions/check-r-package@v2 env: diff --git a/.github/workflows/document.yaml b/.github/workflows/document.yaml new file mode 100644 index 00000000..f8b2d8fa --- /dev/null +++ b/.github/workflows/document.yaml @@ -0,0 +1,42 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + paths: ["R/**"] + +name: Document + +jobs: + document: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - name: Checkout repo + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup R + uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - name: Install dependencies + uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::roxygen2 + needs: roxygen2 + + - name: Document + run: roxygen2::roxygenise() + shell: Rscript {0} + + - name: Commit and push changes + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + git add man/\* NAMESPACE DESCRIPTION + git commit -m "docs: update documentation" || echo "No changes to commit" + git pull --ff-only + git push origin diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 00000000..ceceed68 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,32 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master, dev] + pull_request: + branches: [main, master, dev] + +name: lint + +jobs: + lint: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v3 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::lintr, local::. + needs: lint + + - name: Lint + run: lintr::lint_package() + shell: Rscript {0} + env: + LINTR_ERROR_ON_LINT: true diff --git a/.github/workflows/style.yaml b/.github/workflows/style.yaml new file mode 100644 index 00000000..bee3e482 --- /dev/null +++ b/.github/workflows/style.yaml @@ -0,0 +1,73 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + paths: ["**.[rR]", "**.[qrR]md", "**.[rR]markdown", "**.[rR]nw", "**.[rR]profile"] + +name: Style + +jobs: + style: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - name: Checkout repo + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup R + uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - name: Install dependencies + uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::styler, any::roxygen2 + needs: styler + + - name: Enable styler cache + run: styler::cache_activate() + shell: Rscript {0} + + - name: Determine cache location + id: styler-location + run: | + cat( + "location=", + styler::cache_info(format = "tabular")$location, + "\n", + file = Sys.getenv("GITHUB_OUTPUT"), + append = TRUE, + sep = "" + ) + shell: Rscript {0} + + - name: Cache styler + uses: actions/cache@v3 + with: + path: ${{ steps.styler-location.outputs.location }} + key: ${{ runner.os }}-styler-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-styler- + ${{ runner.os }}- + + - name: Style + run: styler::style_pkg() + shell: Rscript {0} + + - name: Commit and push changes + run: | + if FILES_TO_COMMIT=($(git diff-index --name-only ${{ github.sha }} \ + | egrep --ignore-case '\.(R|[qR]md|Rmarkdown|Rnw|Rprofile)$')) + then + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + git commit ${FILES_TO_COMMIT[*]} -m "Style code (GHA)" + git pull --ff-only + git push origin + else + echo "No changes to commit." + fi From 0d5c806e02aaedc448420b9b86280283d277d311 Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Tue, 29 Aug 2023 16:16:44 -0700 Subject: [PATCH 09/15] clean: remove unused dir --- data-raw/process.R | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 data-raw/process.R diff --git a/data-raw/process.R b/data-raw/process.R deleted file mode 100644 index 8ab17ab2..00000000 --- a/data-raw/process.R +++ /dev/null @@ -1,2 +0,0 @@ -# d = as.data.frame(read.csv('./data-raw/d.csv', row.names=1)) -# usethis::use_data(d, compress="bzip2", overwrite = TRUE) From 586a8e50a2ef23c80010ecc3bc20efe6c504247f Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Tue, 29 Aug 2023 16:17:05 -0700 Subject: [PATCH 10/15] lint: snake case a few variables --- R/check.R | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/R/check.R b/R/check.R index 87d9825c..51792bc1 100644 --- a/R/check.R +++ b/R/check.R @@ -2,30 +2,30 @@ #' @importFrom checkmate assert_character assert_integerish #' @keywords internal assert_character_param <- function(name, value, len = NULL, required = TRUE) { - null.ok <- !required + null_ok <- !required assert_integerish(len, null.ok = TRUE, .var.name = "len") - assert_character(value, null.ok = null.ok, len = len, any.missing = FALSE, .var.name = name) + assert_character(value, null.ok = null_ok, len = len, any.missing = FALSE, .var.name = name) } #' Allows integer-like vectors #' @importFrom checkmate assert_integerish #' @keywords internal assert_integerish_param <- function(name, value, len = NULL, required = TRUE) { - null.ok <- !required + null_ok <- !required assert_integerish(len, null.ok = TRUE, .var.name = "len") - assert_integerish(value, null.ok = null.ok, len = len, any.missing = FALSE, .var.name = name) + assert_integerish(value, null.ok = null_ok, len = len, any.missing = FALSE, .var.name = name) } #' Allows a vector of date_like params: date, character, or integer-like #' @importFrom checkmate check_date check_character check_integerish #' @keywords internal assert_date_param <- function(name, value, len = NULL, required = TRUE) { - null.ok <- !required + null_ok <- !required assert_integerish(len, null.ok = TRUE, .var.name = "len") assert( - check_date(value, len = len, any.missing = FALSE, null.ok = null.ok), - check_character(value, len = len, any.missing = FALSE, null.ok = null.ok), - check_integerish(value, len = len, any.missing = FALSE, null.ok = null.ok), + check_date(value, len = len, any.missing = FALSE, null.ok = null_ok), + check_character(value, len = len, any.missing = FALSE, null.ok = null_ok), + check_integerish(value, len = len, any.missing = FALSE, null.ok = null_ok), combine = "or", .var.name = name ) @@ -36,19 +36,19 @@ assert_date_param <- function(name, value, len = NULL, required = TRUE) { #' @importFrom checkmate assert check_character check_date check_integerish check_class check_list check_names #' @keywords internal assert_timeset_param <- function(name, value, len = NULL, required = TRUE) { - null.ok <- !required + null_ok <- !required assert_integerish(len, len = 1L, null.ok = TRUE, .var.name = "len") assert( - check_class(value, "EpiRange", null.ok = null.ok), + check_class(value, "EpiRange", null.ok = null_ok), check_names(names(value), type = "unnamed"), combine = "or", .var.name = name ) assert( - check_date(value, len = len, any.missing = FALSE, null.ok = null.ok), - check_character(value, len = len, any.missing = FALSE, null.ok = null.ok), - check_integerish(value, len = len, any.missing = FALSE, null.ok = null.ok), - check_class(value, "EpiRange", null.ok = null.ok), + check_date(value, len = len, any.missing = FALSE, null.ok = null_ok), + check_character(value, len = len, any.missing = FALSE, null.ok = null_ok), + check_integerish(value, len = len, any.missing = FALSE, null.ok = null_ok), + check_class(value, "EpiRange", null.ok = null_ok), combine = "or", .var.name = name ) From a2f6a229557cca0e55aa544166e49a90b82952d7 Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Tue, 29 Aug 2023 16:58:16 -0700 Subject: [PATCH 11/15] lint+style+refactor: use cli over rlang --- .lintr | 2 +- DESCRIPTION | 1 - NAMESPACE | 1 - R/auth.R | 8 +++---- R/covidcast.R | 2 +- R/endpoints.R | 22 ++++++++++++++--- R/epidatacall.R | 23 ++++++++++++++---- tests/testthat/generate_test_data.R | 37 +++++++++++++++++++++++++++++ tests/testthat/test-covidcast.R | 9 +------ tests/testthat/test-epidatacall.R | 33 ++++++++----------------- tests/testthat/test-model.R | 6 +---- vignettes/epidatr.Rmd | 2 +- 12 files changed, 94 insertions(+), 52 deletions(-) create mode 100644 tests/testthat/generate_test_data.R diff --git a/.lintr b/.lintr index a213de14..a63d9147 100644 --- a/.lintr +++ b/.lintr @@ -1,4 +1,4 @@ -linters: with_defaults( +linters: linters_with_defaults( line_length_linter(120), cyclocomp_linter = NULL ) diff --git a/DESCRIPTION b/DESCRIPTION index c33e6b1f..1bee541e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,7 +25,6 @@ Imports: magrittr, MMWRweek, readr, - rlang, tibble, xml2 RoxygenNote: 7.2.3 diff --git a/NAMESPACE b/NAMESPACE index 6a2d3570..8528c483 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -65,7 +65,6 @@ importFrom(httr,stop_for_status) importFrom(jsonlite,fromJSON) importFrom(magrittr,"%>%") importFrom(readr,read_csv) -importFrom(rlang,abort) importFrom(tibble,as_tibble) importFrom(tibble,tibble) importFrom(xml2,read_html) diff --git a/R/auth.R b/R/auth.R index 4a0865fa..1450fe5b 100644 --- a/R/auth.R +++ b/R/auth.R @@ -17,17 +17,17 @@ get_auth_key <- function() { return(key) } - rlang::warn( + cli::cli_warn( c( - "No API key found. You will be limited to non-complex queries and encounter rate limits if you proceed.", - "To avoid this, you can get your key by registering at https://api.delphi.cmu.edu/epidata/admin/registration_form and then:", + "No API key found. You will be limited to non-complex queries and encounter rate limits if you proceed. + To avoid this, you can get your key by registering + at https://api.delphi.cmu.edu/epidata/admin/registration_form and then:", "i" = "set the environment variable DELPHI_EPIDATA_KEY", "i" = "set the option 'delphi.epidata.key'", "", "To save your key for later sessions (and hide it from your code), you can edit your .Renviron file with:", "i" = "usethis::edit_r_environ()" ), - use_cli_format = TRUE, .frequency = "regularly", .frequency_id = "delphi.epidata.key" ) diff --git a/R/covidcast.R b/R/covidcast.R index 6d9d860d..0bf43e0c 100644 --- a/R/covidcast.R +++ b/R/covidcast.R @@ -117,7 +117,7 @@ covidcast_epidata <- function(base_url = global_base_url, timeout_seconds = 30) if (response$status_code != 200) { # 500, 429, 401 are possible msg <- "fetch data from API" - if (httr::http_type(response) == "text/html" & length(response$content) > 0) { + if (httr::http_type(response) == "text/html" && length(response$content) > 0) { # grab the error information out of the returned HTML document msg <- paste(msg, ":", xml2::xml_text(xml2::xml_find_all( xml2::read_html(content(response, "text")), diff --git a/R/endpoints.R b/R/endpoints.R index d763fe15..608691ce 100644 --- a/R/endpoints.R +++ b/R/endpoints.R @@ -77,7 +77,13 @@ pvt_cdc <- function(auth, locations, epiweeks, fetch_args = fetch_args_list()) { #' @seealso [covid_hosp_facility()] #' #' @export -covid_hosp_facility_lookup <- function(state = NULL, ccn = NULL, city = NULL, zip = NULL, fips_code = NULL, fetch_args = fetch_args_list()) { +covid_hosp_facility_lookup <- function( + state = NULL, + ccn = NULL, + city = NULL, + zip = NULL, + fips_code = NULL, + fetch_args = fetch_args_list()) { assert_character_param("state", state, len = 1, required = FALSE) assert_character_param("ccn", ccn, len = 1, required = FALSE) assert_character_param("city", city, len = 1, required = FALSE) @@ -154,7 +160,11 @@ covid_hosp_facility_lookup <- function(state = NULL, ccn = NULL, city = NULL, zi #' @seealso [covid_hosp_facility_lookup()], [epirange()] #' @export # -covid_hosp_facility <- function(hospital_pks, collection_weeks, publication_dates = NULL, fetch_args = fetch_args_list()) { +covid_hosp_facility <- function( + hospital_pks, + collection_weeks, + publication_dates = NULL, + fetch_args = fetch_args_list()) { assert_character_param("hospital_pks", hospital_pks) assert_timeset_param("collection_weeks", collection_weeks) assert_timeset_param("publication_dates", publication_dates, required = FALSE) @@ -1679,7 +1689,13 @@ pvt_twitter <- function(auth, locations, dates = NULL, epiweeks = NULL, fetch_ar #' @return [`epidata_call`] #' #' @export -wiki <- function(articles, dates = NULL, epiweeks = NULL, hours = NULL, language = "en", fetch_args = fetch_args_list()) { +wiki <- function( + articles, + dates = NULL, + epiweeks = NULL, + hours = NULL, + language = "en", + fetch_args = fetch_args_list()) { assert_character_param("articles", articles) assert_timeset_param("dates", dates, required = FALSE) assert_timeset_param("epiweeks", epiweeks, required = FALSE) diff --git a/R/epidatacall.R b/R/epidatacall.R index 83c67970..c5f73819 100644 --- a/R/epidatacall.R +++ b/R/epidatacall.R @@ -217,7 +217,6 @@ fetch <- function(epidata_call, fetch_args = fetch_args_list()) { #' @param fetch_args a `fetch_args` object #' @importFrom readr read_csv #' @importFrom httr stop_for_status content -#' @importFrom rlang abort #' @importFrom tibble as_tibble tibble #' @return #' - For `fetch_tbl`: a [`tibble::tibble`] @@ -227,7 +226,11 @@ fetch_tbl <- function(epidata_call, fetch_args = fetch_args_list()) { stopifnot(inherits(fetch_args, "fetch_args")) if (epidata_call$only_supports_classic) { - rlang::abort("This endpoint only supports the classic message format, due to non-standard behavior. Use fetch_classic instead.", + cli::cli_abort( + c( + "This endpoint only supports the classic message format, due to non-standard behavior. + Use fetch_classic instead." + ), epidata_call = epidata_call, class = "only_supports_classic_format" ) @@ -264,11 +267,23 @@ fetch_classic <- function(epidata_call, fetch_args = fetch_args_list()) { # success is 1, no results is -2, truncated is 2, -1 is generic error if (response_content$result != 1) { if ((response_content$result != -2) && !(fetch_args$return_empty)) { - rlang::abort(paste0("epidata error: ", response_content$message), "epidata_error") + cli::cli_abort( + c( + "epidata error: ", + response_content$message + ), + class = "epidata_error" + ) } } if (response_content$message != "success") { - rlang::warn(paste0("epidata warning: ", response_content$message), "epidata_warning") + cli::cli_warn( + c( + "epidata warning: ", + response_content$message + ), + class = "epidata_warning" + ) } return(response_content$epidata) } diff --git a/tests/testthat/generate_test_data.R b/tests/testthat/generate_test_data.R new file mode 100644 index 00000000..95032fba --- /dev/null +++ b/tests/testthat/generate_test_data.R @@ -0,0 +1,37 @@ +epidata_call %>% + fetch_classic() %>% + readr::write_rds(testthat::test_path("data/flusurv-epiweeks.rds")) + +url <- full_url(epidata_call) +params <- request_arguments(epidata_call, "csv", NULL) +result <- do_request(url, params) %>% readr::write_rds(testthat::test_path("data/test-http401.rds")) + +epidata_call <- pvt_afhsb( + auth = Sys.getenv("SECRET_API_AUTH_AFHSB"), + locations = "mn", + epiweeks = epirange(202002, 202110), + flu_types = "flu1" +) +url <- full_url(epidata_call) +params <- request_arguments(epidata_call, "csv", NULL) +response <- do_request(url, params) %>% readr::write_rds(testthat::test_path("data/test-http500.rds")) + +epidata_call %>% + fetch_debug(format_type = "classic") %>% + readr::write_rds(testthat::test_path("data/test-classic.rds")) + +epidata_call %>% + fetch_debug(format_type = "classic", fields = c("time_value", "value")) %>% + readr::write_rds(testthat::test_path("data/test-narrower-fields.rds")) + +epidata_call %>% + fetch_debug(format_type = "classic") %>% + readr::write_rds(testthat::test_path("data/test-classic-only.rds")) + +response <- httr::RETRY("GET", + url = "https://httpbin.org/status/400", + query = list(), + terminate_on = c(400, 401, 403, 405, 414, 500), + http_headers, + httr::authenticate("epidata", get_auth_key()) +) %>% readr::write_rds(testthat::test_path("data/test-do_request-httpbin.rds")) diff --git a/tests/testthat/test-covidcast.R b/tests/testthat/test-covidcast.R index 4025aead..bf6e43b3 100644 --- a/tests/testthat/test-covidcast.R +++ b/tests/testthat/test-covidcast.R @@ -28,14 +28,7 @@ test_that("dataframe converters", { }) test_that("http errors", { - # generated with - # response <- httr::RETRY("GET", - # url = "https://httpbin.org/status/400", - # query = list(), - # terminate_on = c(400, 401, 403, 405, 414, 500), - # http_headers, - # httr::authenticate("epidata", get_auth_key()) - # ) %>% readr::write_rds(testthat::test_path("data/test-do_request-httpbin.rds")) + # see generate_test_data.R local_mocked_bindings( do_request = function(...) readRDS(testthat::test_path("data/test-do_request-httpbin.rds")) ) diff --git a/tests/testthat/test-epidatacall.R b/tests/testthat/test-epidatacall.R index 1e95a8a4..3dc5ac77 100644 --- a/tests/testthat/test-epidatacall.R +++ b/tests/testthat/test-epidatacall.R @@ -7,21 +7,14 @@ test_that("request_impl http errors", { fetch_args = fetch_args_list(dry_run = TRUE) ) local_mocked_bindings( - # generated via - # url <- full_url(epidata_call) - # params <- request_arguments(epidata_call, "csv", NULL) - # result <- do_request(url, params) %>% readr::write_rds(testthat::test_path("data/test-http401.rds")) + # see generate_test_data.R do_request = function(...) readRDS(testthat::test_path("data/test-http401.rds")), ) expect_error(response <- epidata_call %>% request_impl("csv"), class = "http_401") # should give a 500 error (the afhsb endpoint is removed) - # generated via - # epidata_call <- pvt_afhsb(auth = Sys.getenv("SECRET_API_AUTH_AFHSB"), locations = "mn", epiweeks = epirange(202002, 202110), flu_types = "flu1") - # url <- full_url(epidata_call) - # params <- request_arguments(epidata_call, "csv", NULL) - # response <- do_request(url, params) %>% readr::write_rds(testthat::test_path("data/test-http500.rds")) + # see generate_test_data.R local_mocked_bindings( do_request = function(...) readRDS(testthat::test_path("data/test-http500.rds")) ) @@ -90,10 +83,7 @@ test_that("fetch and fetch_tbl", { .package = "epidatr" ) local_mocked_bindings( - # RDS file generated with - # epidata_call %>% - # fetch_debug(format_type = "classic") %>% - # readr::write_rds(testthat::test_path("data/test-classic.rds")) + # see generate_test_data.R content = function(...) readRDS(testthat::test_path("data/test-classic.rds")), .package = "httr" ) @@ -103,10 +93,7 @@ test_that("fetch and fetch_tbl", { expect_identical(out, tbl_out) local_mocked_bindings( - # RDS file generated with - # epidata_call %>% - # fetch_debug(format_type = "classic", fields = c("time_value", "value")) %>% - # readr::write_rds(testthat::test_path("data/test-narrower-fields.rds")) + # see generate_test_data.R content = function(...) readRDS(testthat::test_path("data/test-narrower-fields.rds")), .package = "httr" ) @@ -134,12 +121,15 @@ test_that("fetch_tbl warns on non-success", { content = function(...) NULL, .package = "httr" ) - artificial_warning <- "* This is a warning with a leading asterisk and {braces} to make sure we don't have bulleting/glue bugs." + artificial_warning <- paste( + "* This is a warning with a leading asterisk and {braces}", + " to make sure we don't have bulleting/glue bugs." + ) debug_triplet <- readRDS(testthat::test_path("data/test-classic.rds")) %>% jsonlite::fromJSON() %>% `[[<-`("message", artificial_warning) local_mocked_bindings( - # see generation code above + # see generate_test_data.R fromJSON = function(...) debug_triplet, .package = "jsonlite" ) @@ -158,10 +148,7 @@ test_that("classic only fetch", { fetch_args = fetch_args_list(dry_run = TRUE) ) local_mocked_bindings( - # generated using - # epidata_call %>% - # fetch_debug(format_type = "classic") %>% - # readr::write_rds(testthat::test_path("data/test-classic-only.rds")) + # see generate_test_data.R content = function(...) readRDS(testthat::test_path("data/test-classic-only.rds")), .package = "httr" ) diff --git a/tests/testthat/test-model.R b/tests/testthat/test-model.R index 12dee187..c0723d01 100644 --- a/tests/testthat/test-model.R +++ b/tests/testthat/test-model.R @@ -54,17 +54,13 @@ test_that("null parsing", { epiweeks = 202001, fetch_args = fetch_args_list(dry_run = TRUE) ) - # mocked data generated with - # epidata_call %>% - # fetch_classic() %>% - # readr::write_rds(testthat::test_path("data/flusurv-epiweeks.rds")) + # see generate_test_data.R mock_df <- as.data.frame(readr::read_rds(testthat::test_path("data/flusurv-epiweeks.rds"))) metadata <- epidata_call$meta mock_df[[metadata[[1]]$name]][1] <- list(NULL) mock_df[[metadata[[2]]$name]] <- c(TRUE) epidata_call$meta[[2]]$type <- "bool" res <- parse_data_frame(epidata_call, mock_df) %>% as_tibble() - # expect_null(res[["release_date"]]) # this is actually a list expect_true(res$location) # if the call has no metadata, return the whole frame as is diff --git a/vignettes/epidatr.Rmd b/vignettes/epidatr.Rmd index 9c731cf0..8f323542 100644 --- a/vignettes/epidatr.Rmd +++ b/vignettes/epidatr.Rmd @@ -8,7 +8,7 @@ vignette: > --- ```{r, echo = FALSE, message = FALSE} -knitr::opts_chunk$set(collapse = T, comment = "#>") +knitr::opts_chunk$set(collapse = TRUE, comment = "#>") options(tibble.print_min = 4L, tibble.print_max = 4L, max.print = 4L) library(epidatr) library(dplyr) From 9424f6042f29c3d021ecd56c09868c8a50f1b928 Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Wed, 30 Aug 2023 11:14:25 -0700 Subject: [PATCH 12/15] build: add dsweber2 to codeowners --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index bb07c510..16f04afb 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @dshemetov @brookslogan +* @dshemetov @brookslogan @dsweber2 From 7014060af6665dd1b880327eeeef1c05330fe44f Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Wed, 30 Aug 2023 11:14:42 -0700 Subject: [PATCH 13/15] ci: format commit message from ci --- .github/workflows/document.yaml | 2 +- .github/workflows/style.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/document.yaml b/.github/workflows/document.yaml index f8b2d8fa..cc12ccc7 100644 --- a/.github/workflows/document.yaml +++ b/.github/workflows/document.yaml @@ -37,6 +37,6 @@ jobs: git config --local user.name "$GITHUB_ACTOR" git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" git add man/\* NAMESPACE DESCRIPTION - git commit -m "docs: update documentation" || echo "No changes to commit" + git commit -m "docs: update documentation (GHA)" || echo "No changes to commit" git pull --ff-only git push origin diff --git a/.github/workflows/style.yaml b/.github/workflows/style.yaml index bee3e482..15a6f47a 100644 --- a/.github/workflows/style.yaml +++ b/.github/workflows/style.yaml @@ -65,7 +65,7 @@ jobs: then git config --local user.name "$GITHUB_ACTOR" git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" - git commit ${FILES_TO_COMMIT[*]} -m "Style code (GHA)" + git commit ${FILES_TO_COMMIT[*]} -m "style: styler (GHA)" git pull --ff-only git push origin else From 94461a50464f5b2c1b89e5ad7d7b9561362f65ac Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Wed, 30 Aug 2023 11:14:55 -0700 Subject: [PATCH 14/15] docs: minor vignette updates --- vignettes/epidatr.Rmd | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/vignettes/epidatr.Rmd b/vignettes/epidatr.Rmd index 8f323542..0a1a1fec 100644 --- a/vignettes/epidatr.Rmd +++ b/vignettes/epidatr.Rmd @@ -41,7 +41,7 @@ documentation, we see that we need to specify a data source name, a signal name, a geographic level, a time resolution, and the location and times of interest. In this case, the `covidcast()` function lets us specify these parameters for -the endpoint: +the endpoint and returns a tibble with the results: ```{r} covidcast( @@ -51,7 +51,7 @@ covidcast( ``` The [Delphi Epidata API documentation](https://cmu-delphi.github.io/delphi-epidata/) has more information on the available endpoints and arguments. -Examples queries with all the endpoint functions available in this package are given [below](#example-queries). +Example queries with all the endpoint functions available in this package are given [below](#example-queries). ## Advanced Usage (Experimental) @@ -95,7 +95,6 @@ epidata$signals$`fb-survey:smoothed_cli`$call("state", "pa", epirange(20210405, API docs: - County geo_values are [FIPS codes](https://en.wikipedia.org/wiki/List_of_United_States_FIPS_codes_by_county) and are discussed in the API docs [here](https://cmu-delphi.github.io/delphi-epidata/api/covidcast_geography.html). The example below is for Orange County, California. ```{r} From a25e532edfc244c5be534f90146204e6c683fa1d Mon Sep 17 00:00:00 2001 From: Dmitry Shemetov Date: Wed, 30 Aug 2023 11:20:18 -0700 Subject: [PATCH 15/15] chore: bump version to 0.9.0 --- .bumpversion.cfg | 2 +- DESCRIPTION | 2 +- NEWS.md | 19 +++++++++++++------ R/constants.R | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index ee79dd70..7565dd9d 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.8.0 +current_version = 0.9.0 commit = False tag = False diff --git a/DESCRIPTION b/DESCRIPTION index 1bee541e..2ad5bd4c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: epidatr Type: Package Title: Client for Delphi's Epidata API -Version: 0.8.0 +Version: 0.9.0 Authors@R: c( person("Logan", "Brooks", email = "lcbrooks@andrew.cmu.edu", role = c("aut", "cre")), diff --git a/NEWS.md b/NEWS.md index e933b1f7..c4cdcde9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,13 +1,20 @@ +# epidatr 0.9.0 + +- Major interface change: all endpoints now fetch by default. +- Make all `fetch` function internal. +- Change `fetch` and `fetch_*` function interfaces now rely on `fetch_args_list`. +- Added `fetch_args_list` which returns a list of arguments to be passed to `fetch`. + # epidatr 0.8.0 -- Fix source name duplication bug in `covidcast_epidata` -- Mark `covidcast_epidata` as experimental and do not export it -- Change `covidcast` arg `data_source` to `source` -- Make `covidcast` args `issues`, `lag`, and `as_of` mutually exclusive +- Fix source name duplication bug in `covidcast_epidata`. +- Mark `covidcast_epidata` as experimental and do not export it. +- Change `covidcast` arg `data_source` to `source`. +- Make `covidcast` args `issues`, `lag`, and `as_of` mutually exclusive. - Make `covid_hosp_facility_lookup` args `state,` `ccn`, `city`, `zip`, and - `fips_code` mutually exclusive + `fips_code` mutually exclusive. - Update documentation to only refer to character or strings (not character - vectors or character strings) + vectors or character strings). # epidatr 0.7.1 diff --git a/R/constants.R b/R/constants.R index cf926be2..eb852c0b 100644 --- a/R/constants.R +++ b/R/constants.R @@ -1,3 +1,3 @@ -version <- "0.8.0" +version <- "0.9.0" http_headers <- httr::add_headers("User-Agent" = paste0("epidatr/", version), "Accept-Encoding" = "gzip") global_base_url <- "https://api.delphi.cmu.edu/epidata/"