diff --git a/.editorconfig b/.editorconfig index 8a80734f..c2cdfb8a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,7 +9,7 @@ root = true # Change these settings to your own preference indent_style = space -indent_size = 4 +indent_size = 2 # We recommend you to keep these unchanged end_of_line = lf diff --git a/DESCRIPTION b/DESCRIPTION index 4358689f..cf3a0bec 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: epidatr Type: Package Title: Client for Delphi's Epidata API -Version: 1.0.0 +Version: 0.5.0 Authors@R: as.person(c( "Jacob Bien [aut]", "Logan Brooks [aut]", @@ -20,15 +20,16 @@ Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) Imports: - httr, - jsonlite, - readr, - MMWRweek, - rlang + cli, + httr, + jsonlite, + readr, + MMWRweek, + rlang, + magrittr RoxygenNote: 7.2.3 Suggests: knitr, - magrittr, rmarkdown, testthat VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index f1dcc7c3..ee40c03f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,13 @@ # Generated by roxygen2: do not edit by hand +S3method(as.data.frame,covidcast_data_signal_list) +S3method(as.data.frame,covidcast_data_source_list) +S3method(print,covidcast_data_signal) +S3method(print,covidcast_data_source) +S3method(print,covidcast_epidata) +S3method(print,epidata_call) +S3method(print,epidata_csv) +export("%>%") export(covid_hosp_facility) export(covid_hosp_facility_lookup) export(covid_hosp_state_timeseries) @@ -12,7 +20,6 @@ export(ecdc_ili) export(epirange) export(fetch_classic) export(fetch_csv) -export(fetch_df) export(fetch_json) export(fetch_tbl) export(flusurv) @@ -46,5 +53,6 @@ importFrom(httr,http_error) importFrom(httr,modify_url) importFrom(httr,stop_for_status) importFrom(jsonlite,fromJSON) +importFrom(magrittr,"%>%") importFrom(readr,read_csv) importFrom(rlang,abort) diff --git a/R/constants.R b/R/constants.R index e4894ce6..fe985828 100644 --- a/R/constants.R +++ b/R/constants.R @@ -1,3 +1,3 @@ version <- "1.0.0" -http_headers <- httr::add_headers("User-Agent" = paste0("epidatr/", version)) +http_headers <- httr::add_headers("User-Agent" = paste0("epidatr/", version), "Accept-Encoding" = "gzip") global_base_url <- "https://delphi.cmu.edu/epidata/" diff --git a/R/covidcast.R b/R/covidcast.R index a2c9fbba..df9726fa 100644 --- a/R/covidcast.R +++ b/R/covidcast.R @@ -30,6 +30,7 @@ parse_signal <- function(signal, base_url) { r } +#' @export print.covidcast_data_signal <- function(signal, ...) { print(signal$name) print(signal$key) @@ -46,6 +47,8 @@ parse_source <- function(source, base_url) { r } +#' @method as.data.frame covidcast_data_signal_list +#' @export as.data.frame.covidcast_data_signal_list <- function(signals, ...) { as.data.frame(do.call(rbind, lapply(signals, function(x) { sub <- x[c( @@ -74,6 +77,7 @@ as.data.frame.covidcast_data_signal_list <- function(signals, ...) { }), ...) } +#' @export print.covidcast_data_source <- function(source, ...) { print(source$name, ...) print(source$source, ...) @@ -127,6 +131,8 @@ covidcast_epidata <- function(base_url = global_base_url) { ) } +#' @method as.data.frame covidcast_data_source_list +#' @export as.data.frame.covidcast_data_source_list <- function(sources, ...) { as.data.frame(do.call(rbind, lapply(sources, function(x) { sub <- x[c( @@ -141,6 +147,7 @@ as.data.frame.covidcast_data_source_list <- function(sources, ...) { }), ...) } +#' @export print.covidcast_epidata <- function(epidata, ...) { print("COVIDcast Epidata Fetcher") print("Sources:") diff --git a/R/endpoints.R b/R/endpoints.R index 438354a2..29a8d7b4 100644 --- a/R/endpoints.R +++ b/R/endpoints.R @@ -9,9 +9,13 @@ #' @return [`epidata_call`] #' #' @examples -#' \donttest{ -#' call <- pvt_afhsb(auth = "yourkey", "fl,ca", epirange(202001, 202110), "flu1,flu2-flu1") -#' fetch_tbl(call) +#' \dontrun{ +#' pvt_afhsb( +#' auth = "yourkey", +#' "fl,ca", +#' epirange(202001, 202110), +#' "flu1,flu2-flu1" +#' ) %>% fetch_tbl() #' } #' @export pvt_afhsb <- function(auth, locations, epiweeks, flu_types) { @@ -42,9 +46,12 @@ pvt_afhsb <- function(auth, locations, epiweeks, flu_types) { #' API docs: https://cmu-delphi.github.io/delphi-epidata/api/cdc.html #' #' @examples -#' \donttest{ -#' call <- pvt_cdc(auth = "yourkey", epirange(20210101, 20210201), "fl,ca") -#' fetch_tbl(call) +#' \dontrun{ +#' pvt_cdc( +#' auth = "yourkey", +#' epirange(20210101, 20210201), +#' "fl,ca" +#' ) %>% fetch_tbl() #' } #' @param auth string. Authentication token. #' @param epiweeks [`epirange`]. Epiweeks to fetch. @@ -94,8 +101,7 @@ pvt_cdc <- function(auth, epiweeks, locations) { #' #' @examples #' \donttest{ -#' call <- covid_hosp_facility_lookup(state = "fl") -#' fetch_tbl(call) +#' covid_hosp_facility_lookup(state = "fl") %>% fetch_tbl() #' } #' @param state string. A two-letter character string state abbreviation. #' @param ccn string. A character string for facility CMS certification number. @@ -160,8 +166,10 @@ covid_hosp_facility_lookup <- #' #' @examples #' \donttest{ -#' call <- covid_hosp_facility(hospital_pks = "100075", collection_weeks = epirange(20200101, 20200501)) -#' fetch_tbl(call) +#' covid_hosp_facility( +#' hospital_pks = "100075", +#' collection_weeks = epirange(20200101, 20200501) +#' ) %>% fetch_tbl() #' } #' @param hospital_pks string. A character string of facility unique identifiers. #' @param collection_weeks [`epirange`]. Epiweeks to fetch. @@ -448,8 +456,10 @@ covid_hosp_facility <- #' #' @examples #' \donttest{ -#' call <- covid_hosp_state_timeseries(states = "fl", dates = epirange(20200101, 20200501)) -#' fetch_tbl(call) +#' covid_hosp_state_timeseries( +#' states = "fl", +#' dates = epirange(20200101, 20200501) +#' ) %>% fetch_tbl() #' } #' @param states character vector. Two letter state abbreviations. #' @param dates [`epirange`]. Dates to fetch. @@ -600,8 +610,7 @@ covid_hosp_state_timeseries <- #' #' @examples #' \donttest{ -#' call_meta <- covidcast_meta() -#' fetch_classic(call_meta) +#' covidcast_meta() %>% fetch_classic() #' } #' #' @seealso [covidcast()] @@ -639,15 +648,14 @@ covidcast_meta <- function() { #' #' @examples #' \donttest{ -#' call <- covidcast( +#' covidcast( #' data_source = "jhu-csse", #' signals = "confirmed_7dav_incidence_prop", #' time_type = "day", #' geo_type = "state", #' time_values = epirange(20200601, 20200801), #' geo_values = "ca,fl" -#' ) -#' fetch_tbl(call) +#' ) %>% fetch_tbl() #' } #' @param data_source string. The data source to query (see: #' ). @@ -749,8 +757,7 @@ covidcast <- #' #' @examples #' \donttest{ -#' call <- delphi(system = "ec", epiweek = 202006) -#' fetch_classic(call) +#' delphi(system = "ec", epiweek = 202006) %>% fetch_classic() #' } #' @param system string. The system name to fetch. #' @param epiweek [`epirange`]. The epiweeks to fetch. @@ -779,9 +786,11 @@ delphi <- function(system, epiweek) { #' #' TODO: what are valid locations here? #' @examples -#' \donttest{ -#' call <- dengue_nowcast(locations = "?", epiweeks = epirange(201501, 202001)) -#' fetch_classic(call) +#' \dontrun{ +#' dengue_nowcast( +#' locations = "?", +#' epiweeks = epirange(201501, 202001) +#' ) %>% fetch_classic() #' } #' @param locations character vector. The locations to fetch. #' @param epiweeks [`epirange`]. The epiweeks to fetch. @@ -810,9 +819,13 @@ dengue_nowcast <- function(locations, epiweeks) { #' #' TODO: what are valid locations and names? #' @examples -#' \donttest{ -#' call <- pvt_dengue_sensors(auth = "yourkey", names = "?", locations = "?", epiweeks = epirange(201501, 202001)) -#' fetch_classic(call) +#' \dontrun{ +#' pvt_dengue_sensors( +#' auth = "yourkey", +#' names = "?", +#' locations = "?", +#' epiweeks = epirange(201501, 202001) +#' ) %>% fetch_classic() #' } #' @param auth string. Your authentication token. #' @param names character vector. The list of names to fetch. @@ -855,8 +868,7 @@ pvt_dengue_sensors <- function(auth, names, locations, epiweeks) { #' #' @examples #' \donttest{ -#' call <- ecdc_ili(regions = "austria", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' ecdc_ili(regions = "austria", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' } #' @param regions character vector. The regions to fetch. #' @param epiweeks [`epirange`]. The epiweeks to fetch. @@ -909,8 +921,7 @@ ecdc_ili <- function(regions, #' #' @examples #' \donttest{ -#' call <- flusurv(locations = "CA", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' flusurv(locations = "CA", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' } #' @param locations character vector. Character strings indicating location. #' @param epiweeks [`epirange`]. The epiweeks to fetch. @@ -946,12 +957,12 @@ flusurv <- function(locations, create_epidata_field_info("issue", "date"), create_epidata_field_info("epiweek", "epiweek"), create_epidata_field_info("lag", "int"), - create_epidata_field_info("rage_age_0", "float"), - create_epidata_field_info("rage_age_1", "float"), - create_epidata_field_info("rage_age_2", "float"), - create_epidata_field_info("rage_age_3", "float"), - create_epidata_field_info("rage_age_4", "float"), - create_epidata_field_info("rage_overall", "float") + create_epidata_field_info("rate_age_0", "float"), + create_epidata_field_info("rate_age_1", "float"), + create_epidata_field_info("rate_age_2", "float"), + create_epidata_field_info("rate_age_3", "float"), + create_epidata_field_info("rate_age_4", "float"), + create_epidata_field_info("rate_overall", "float") ) ) } @@ -962,9 +973,7 @@ flusurv <- function(locations, #' #' @examples #' \donttest{ -#' # can take a couple minutes; donttesting while cmu-delphi/delphi-epidata#48 is unresolved -#' call <- fluview_clinical(regions = "nat", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' fluview_clinical(regions = "nat", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' } #' @param regions character vector. The regions to fetch. #' @param epiweeks [`epirange`]. The epiweeks to fetch in the form epirange(startweek,endweek), where startweek @@ -1044,8 +1053,7 @@ fluview_meta <- function() { #' . #' #' @examples -#' call <- fluview(regions = "nat", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' fluview(regions = "nat", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' @param regions character vector. The locations to fetch. Can we any string IDs in national, HHS region, #' census division, most states and territories, and so on. Full list link below. #' @param epiweeks [`epirange`]. The epiweeks to fetch in the form epirange(startweek,endweek), where startweek @@ -1114,8 +1122,7 @@ fluview <- #' . #' #' @examples -#' call <- gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' @param locations character vector. The locations to be fetched. #' @param epiweeks [`epirange`] The epiweeks to be fetched. #' @@ -1142,9 +1149,13 @@ gft <- function(locations, epiweeks) { #' #' TODO: find a non-trivial query #' @examples -#' \donttest{ -#' call <- pvt_ght(auth = "yourkey", locations = "ca", epiweeks = epirange(201201, 202001), query = "?") -#' fetch_classic(call) +#' \dontrun{ +#' pvt_ght( +#' auth = "yourkey", +#' locations = "ca", +#' epiweeks = epirange(201201, 202001), +#' query = "?" +#' ) %>% fetch_classic() #' } #' @param auth string. Your authentication token. #' @param locations character vector. The locations to be fetched. @@ -1180,9 +1191,8 @@ pvt_ght <- function(auth, locations, epiweeks, query) { #' #' TODO: find a non-trivial region #' @examples -#' \donttest{ -#' call <- kcdc_ili(regions = "?", epiweeks = epirange(201201, 202001)) -#' fetch_tbl(call) +#' \dontrun{ +#' kcdc_ili(regions = "?", epiweeks = epirange(201201, 202001)) %>% fetch_tbl() #' } #' @param regions character vector. The regions to be fetched. #' @param epiweeks [`epirange`]. The epiweeks to be fetched. @@ -1274,8 +1284,7 @@ meta <- function() { #' #' @examples #' \donttest{ -#' call <- nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' } #' @param locations character vector. The locations to fetch. #' @param epiweeks [`epirange`]. The epiweeks to fetched. @@ -1306,8 +1315,7 @@ nidss_dengue <- function(locations, epiweeks) { #' #' @examples #' \donttest{ -#' call <- nidss_flu(regions = "taipei", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' nidss_flu(regions = "taipei", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' } #' @param regions character vector. The regions to fetch. #' @param epiweeks [`epirange`]. The epiweeks to fetch. @@ -1358,13 +1366,12 @@ nidss_flu <- #' API docs: https://cmu-delphi.github.io/delphi-epidata/api/norostat.html #' #' @examples -#' \donttest{ -#' call <- pvt_norostat( +#' \dontrun{ +#' pvt_norostat( #' auth = "yourkey", #' location = "Minnesota, Ohio, Oregon, Tennessee, and Wisconsin", #' epiweeks = epirange(201201, 202001) -#' ) -#' fetch_classic(call) +#' ) %>% fetch_classic() #' } #' @param auth string. Your authentication key. #' @param location character vector. The locations to fetch. @@ -1403,8 +1410,7 @@ pvt_norostat <- function(auth, location, epiweeks) { #' #' @examples #' \donttest{ -#' call <- nowcast(location = "ca", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' nowcast(location = "ca", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' } #' @param locations character vector. The locations to fetch. #' @param epiweeks [`epirange`]. The epiweeks to fetch. @@ -1433,8 +1439,7 @@ nowcast <- function(locations, epiweeks) { #' #' @examples #' \donttest{ -#' call <- paho_dengue(regions = "ca", epiweeks = epirange(201201, 202001)) -#' fetch_classic(call) +#' paho_dengue(regions = "ca", epiweeks = epirange(201201, 202001)) %>% fetch_classic() #' } #' @param regions character vector. The regions to fetch. #' @param epiweeks [`epirange`]. The epiweeks to fetch. @@ -1483,9 +1488,12 @@ paho_dengue <- function(regions, #' API docs: https://cmu-delphi.github.io/delphi-epidata/api/quidel.html #' #' @examples -#' \donttest{ -#' call <- pvt_quidel(auth = "yourkey", epiweeks = epirange(201201, 202001), locations = "hhs1") -#' fetch_classic(call) +#' \dontrun{ +#' pvt_quidel( +#' auth = "yourkey", +#' epiweeks = epirange(201201, 202001), +#' locations = "hhs1" +#' ) %>% fetch_classic() #' } #' @param auth string. Your authentication key. #' @param epiweeks [`epirange`]. The epiweeks to fetch. @@ -1518,9 +1526,13 @@ pvt_quidel <- function(auth, epiweeks, locations) { #' API docs: https://cmu-delphi.github.io/delphi-epidata/api/sensors.html #' #' @examples -#' \donttest{ -#' call <- pvt_sensors(auth = "yourkey", names = "sar3", locations = "nat", epiweeks = epirange(201501, 202001)) -#' fetch_classic(call) +#' \dontrun{ +#' pvt_sensors( +#' auth = "yourkey", +#' names = "sar3", +#' locations = "nat", +#' epiweeks = epirange(201501, 202001) +#' ) %>% fetch_classic() #' } #' @param auth string. Your authentication key. #' @param names character vector. The names of the sensors to fetch. @@ -1557,9 +1569,8 @@ pvt_sensors <- function(auth, names, locations, epiweeks) { #' API docs: https://cmu-delphi.github.io/delphi-epidata/api/twitter.html #' #' @examples -#' \donttest{ -#' call <- pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) -#' fetch_tbl(call) +#' \dontrun{ +#' pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) %>% fetch_tbl() #' } #' @param auth string. Your authentication key. #' @param locations character vector. The locations to fetch. @@ -1609,8 +1620,7 @@ pvt_twitter <- #' #' @examples #' \donttest{ -#' call <- wiki(articles = "avian_influenza", epiweeks = epirange(201501, 202001)) -#' fetch_tbl(call) +#' wiki(articles = "avian_influenza", epiweeks = epirange(201501, 202001)) %>% fetch_tbl() #' } #' @param articles character vector. The articles to fetch. #' @param dates [`epirange`]. The dates to fetch. Mutually exclusive with `epiweeks`. diff --git a/R/epidatacall.R b/R/epidatacall.R index 34ff5019..c0f18674 100644 --- a/R/epidatacall.R +++ b/R/epidatacall.R @@ -1,11 +1,50 @@ -#' an abstraction that holds the information needed to make an epidata call +#' An abstraction that holds information needed to make an epidata request +#' @rdname epidata_call +#' @aliases epidata_call +#' +#' @description +#' `epidata_call` objects are generated by endpoint functions like [`covidcast`]; +#' should be piped into a `fetch_*` function to fetch and +#' format the data: +#' - `fetch_tbl`: fetches the data, formats as a [`tibble::tibble`] +#' - `fetch_classic`: fetches the data in "classic" format: a list with three +#' elements: +#' - `"epidata"`: some R object (typically a `data.frame`) holding the data, +#' if the request was successful +#' - `"message"`: string; either `"success"` or some error/warning message +#' - `"result"`: integer; either 1 or some error/warning code +#' - `fetch_csv`: fetches the data, formats as a string containing CSV data, +#' with a special class `"epidata_csv"` added to prevent accidentally +#' printing a huge number of lines +#' - `fetch_json`: fetches the data in JSON format then converts to a `data.frame` +#' +#' `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. +#' +#' 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 +#' covidcast( +#' data_source = "jhu-csse", +#' signals = "confirmed_7dav_incidence_prop", +#' time_type = "day", +#' geo_type = "state", +#' time_values = epirange(20200601, 20200801), +#' geo_values = c("ca", "fl") +#' ) %>% fetch_tbl() #' #' @param endpoint the epidata endpoint to call #' @param params the parameters to pass to the epidata endpoint #' @param meta meta data to attach to the epidata call #' @param only_supports_classic if true only classic format is supported #' -#' @return an epidata_call instance +#' @return +#' - For `create_epidata_call`: an `epidata_call` object #' create_epidata_call <- function(endpoint, params, meta = NULL, only_supports_classic = FALSE) { @@ -28,19 +67,6 @@ create_epidata_call <- function(endpoint, params, meta = NULL, ) } -#' use a different base url -#' -#' @param epidata_call and instance of epidata_call -#' @param base_url basee url to use -#' -#' @export -with_base_url <- function(epidata_call, base_url) { - stopifnot(inherits(epidata_call, "epidata_call")) - stopifnot(is.character(base_url), length(base_url) == 1) - epidata_call$base_url <- base_url - epidata_call -} - request_arguments <- function(epidata_call, format_type, fields = NULL) { stopifnot(inherits(epidata_call, "epidata_call")) @@ -66,51 +92,81 @@ request_arguments <- formatted_params } -full_url <- function(epidata_call) { +#' @export +print.epidata_call <- function(epidata_call) { stopifnot(inherits(epidata_call, "epidata_call")) - join_url(epidata_call$base_url, epidata_call$endpoint) + cli::cli_h1(" object:") + cli::cli_bullets(c( + "*" = "Use fetch_classic, fetch_json, or fetch_csv to actually fetch the data", + "*" = paste0("Request URL: ", request_url(epidata_call)) + )) } -#' returns the full request url for the given epidata_call +#' Fetches the data and returns a tibble +#' @rdname epidata_call #' -#' @param epidata_call and instance of epidata_call -#' @param format_type format to return one of classic,json,csv +#' @param epidata_call an instance of `epidata_call` #' @param fields filter fields -#' @importFrom httr modify_url -#' @return full url +#' @param disable_date_parsing disable automatic date parsing +#' @importFrom readr read_csv +#' @importFrom httr stop_for_status content +#' @importFrom rlang abort +#' @return +#' - For `fetch_tbl`: a [`tibble::tibble`] #' #' @export -request_url <- function(epidata_call, format_type = "classic", fields = NULL) { - stopifnot(inherits(epidata_call, "epidata_call")) - url <- full_url(epidata_call) - params <- request_arguments(epidata_call, format_type, fields) - httr::modify_url(url, query = params) -} - -print.epidata_call <- function(epidata_call) { +fetch_tbl <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE) { stopifnot(inherits(epidata_call, "epidata_call")) - print("epidata_call instance, use fetch_classic, fetch_json, fetch_df, fetch_csv to fetch the data") - print(request_url(epidata_call)) -} + stopifnot(is.null(fields) || is.character(fields)) + stopifnot(is.logical(disable_date_parsing), length(disable_date_parsing) == 1) + if (epidata_call$only_supports_classic) { + rlang::abort("the endpoint only supports the classic message format, due to a non-standard behavior", + epidata_call = epidata_call, + class = "only_supports_classic_format" + ) + } + r <- fetch_csv(epidata_call, fields) + meta <- epidata_call$meta + fields_pred <- fields_to_predicate(fields) + col_names <- c() + col_types <- list() + for (i in seq_len(length(meta))) { + info <- meta[[i]] + if (fields_pred(info$name)) { + col_names <- c(col_names, info$name) + col_types[info$name] <- info_to_type(info, disable_date_parsing) + } + } + tbl <- if (length(col_names) > 0) { + readr::read_csv(r, col_types = col_types) + } else { + readr::read_csv(r) + } -request_impl <- function(epidata_call, format_type, fields = NULL) { - stopifnot(inherits(epidata_call, "epidata_call")) - stopifnot(format_type %in% c("json", "csv", "classic")) - # API call - url <- full_url(epidata_call) - params <- request_arguments(epidata_call, format_type, fields) - do_request(url, params) + if (!disable_date_parsing) { + # parse weeks + columns <- colnames(tbl) + for (i in seq_len(length(meta))) { + info <- meta[[i]] + if (info$name %in% columns && info$type == "epiweek") { + tbl[[info$name]] <- parse_api_week(tbl[[info$name]]) + } + } + } + tbl } -#' fetches the data and returns the classic format +#' Fetches the data and returns the classic format +#' @rdname epidata_call #' -#' @param epidata_call and instance of epidata_call +#' @param epidata_call an instance of `epidata_call` #' @param fields filter fields #' @param disable_date_parsing disable automatic date parsing #' @importFrom httr stop_for_status content http_error #' @importFrom jsonlite fromJSON -#' @return parsed json message +#' @return +#' - For `fetch_classic`: the "classic" 3-element list format (see above) #' #' @export fetch_classic <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE) { @@ -131,58 +187,113 @@ fetch_classic <- function(epidata_call, fields = NULL, disable_date_parsing = FA m } -#' fetches the data and returns the josn format +#' Fetches the data and returns the CSV text +#' @rdname epidata_call #' -#' @param epidata_call and instance of epidata_call +#' @param epidata_call an instance of `epidata_call` #' @param fields filter fields -#' @param disable_date_parsing disable automatic date parsing #' @importFrom httr stop_for_status content -#' @importFrom jsonlite fromJSON #' @importFrom rlang abort -#' @return parsed json message +#' @return +#' - For `fetch_csv`: a string containing CSV text, with the `"epidata_csv"` class added #' #' @export -fetch_json <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE) { +fetch_csv <- function(epidata_call, fields = NULL) { stopifnot(inherits(epidata_call, "epidata_call")) stopifnot(is.null(fields) || is.character(fields)) - stopifnot(is.logical(disable_date_parsing), length(disable_date_parsing) == 1) if (epidata_call$only_supports_classic) { - rlang::abort("the endpoint only supports the classic message format, due to an non-standard behavior", + rlang::abort("the endpoint only supports the classic message format, due to a non-standard behavior", epidata_call = epidata_call, class = "only_supports_classic_format" ) } - res <- request_impl(epidata_call, "json", fields) + res <- request_impl(epidata_call, "csv", fields) httr::stop_for_status(res) - r <- httr::content(res, "text", encoding = "UTF-8") - parse_data_frame(epidata_call, jsonlite::fromJSON(r), disable_date_parsing = disable_date_parsing) + data <- httr::content(res, "text", encoding = "UTF-8") + class(data) <- c("epidata_csv", class(data)) + data } -#' fetches the data and returns the CSV text +#' Fetches the data in JSON format, then converts into an R object +#' @rdname epidata_call #' -#' @param epidata_call and instance of epidata_call +#' @param epidata_call an instance of `epidata_call` #' @param fields filter fields +#' @param disable_date_parsing disable automatic date parsing #' @importFrom httr stop_for_status content +#' @importFrom jsonlite fromJSON #' @importFrom rlang abort -#' @return CSV text +#' @return +#' - For `fetch_json`: a `data.frame` form of the object from the parsed JSON +#' response #' #' @export -fetch_csv <- function(epidata_call, fields = NULL) { +fetch_json <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE) { stopifnot(inherits(epidata_call, "epidata_call")) stopifnot(is.null(fields) || is.character(fields)) + stopifnot(is.logical(disable_date_parsing), length(disable_date_parsing) == 1) if (epidata_call$only_supports_classic) { - rlang::abort("the endpoint only supports the classic message format, due to an non-standard behavior", + rlang::abort("the endpoint only supports the classic message format, due to a non-standard behavior", epidata_call = epidata_call, class = "only_supports_classic_format" ) } - res <- request_impl(epidata_call, "csv", fields) + res <- request_impl(epidata_call, "json", fields) httr::stop_for_status(res) - data <- httr::content(res, "text", encoding = "UTF-8") - class(data) <- c("epidata_csv", class(data)) - data + r <- httr::content(res, "text", encoding = "UTF-8") + parse_data_frame(epidata_call, jsonlite::fromJSON(r), disable_date_parsing = disable_date_parsing) +} + +full_url <- function(epidata_call) { + stopifnot(inherits(epidata_call, "epidata_call")) + join_url(epidata_call$base_url, epidata_call$endpoint) } +#' Returns the full request url for the given epidata_call +#' @rdname epidata_call +#' +#' @param epidata_call an instance of `epidata_call` +#' @param format_type format to return one of classic,json,csv +#' @param fields filter fields +#' @importFrom httr modify_url +#' @return +#' - For `request_url`: a string containing the URL +#' +#' @export +request_url <- function(epidata_call, format_type = "classic", fields = NULL) { + stopifnot(inherits(epidata_call, "epidata_call")) + url <- full_url(epidata_call) + params <- request_arguments(epidata_call, format_type, fields) + httr::modify_url(url, query = params) +} + +#' `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`: another `epidata_call` object +#' +#' @export +with_base_url <- function(epidata_call, base_url) { + stopifnot(inherits(epidata_call, "epidata_call")) + stopifnot(is.character(base_url), length(base_url) == 1) + epidata_call$base_url <- base_url + epidata_call +} + +request_impl <- function(epidata_call, format_type, fields = NULL) { + stopifnot(inherits(epidata_call, "epidata_call")) + stopifnot(format_type %in% c("json", "csv", "classic")) + # API call + url <- full_url(epidata_call) + params <- request_arguments(epidata_call, format_type, fields) + + do_request(url, params) +} + +#' @export print.epidata_csv <- function(x, ...) { char_limit <- getOption("epidata_csv__char_limit", default = 300L) cat( @@ -215,68 +326,3 @@ info_to_type <- function(info, disable_date_parsing = FALSE) { stopifnot(!is.null(r)) r } - -#' fetches the data and returns data frame -#' -#' @param epidata_call and instance of epidata_call -#' @param fields filter fields -#' @param disable_date_parsing disable automatic date parsing -#' @importFrom readr read_csv -#' @importFrom httr stop_for_status content -#' @importFrom rlang abort -#' @return tibble -#' -#' @export -fetch_tbl <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE) { - stopifnot(inherits(epidata_call, "epidata_call")) - stopifnot(is.null(fields) || is.character(fields)) - stopifnot(is.logical(disable_date_parsing), length(disable_date_parsing) == 1) - if (epidata_call$only_supports_classic) { - rlang::abort("the endpoint only supports the classic message format, due to an non-standard behavior", - epidata_call = epidata_call, - class = "only_supports_classic_format" - ) - } - r <- fetch_csv(epidata_call, fields) - meta <- epidata_call$meta - fields_pred <- fields_to_predicate(fields) - col_names <- c() - col_types <- list() - for (i in seq_len(length(meta))) { - info <- meta[[i]] - if (fields_pred(info$name)) { - col_names <- c(col_names, info$name) - col_types[info$name] <- info_to_type(info, disable_date_parsing) - } - } - tbl <- if (length(col_names) > 0) { - readr::read_csv(r, col_types = col_types) - } else { - readr::read_csv(r) - } - - - if (!disable_date_parsing) { - # parse weeks - columns <- colnames(tbl) - for (i in seq_len(length(meta))) { - info <- meta[[i]] - if (info$name %in% columns && info$type == "epiweek") { - tbl[[info$name]] <- parse_api_week(tbl[[info$name]]) - } - } - } - tbl -} - -#' fetches the data and returns data frame -#' -#' @param epidata_call and instance of epidata_call -#' @param fields filter fields -#' @param disable_date_parsing disable automatic date parsing -#' @return data.frame -#' -#' @export -fetch_df <- function(epidata_call, fields = NULL, disable_date_parsing = FALSE) { - as.data.frame(fetch_tbl(epidata_call, fields, disable_date_parsing)) -} diff --git a/R/parse.R b/R/parse.R index 0219d932..3aae1d4d 100644 --- a/R/parse.R +++ b/R/parse.R @@ -3,6 +3,7 @@ parse_api_date <- function(value) { } #' parses a week +#' @param value value to be converted to an epiweek #' @importFrom MMWRweek MMWRweek2Date #' @return a date parse_api_week <- function(value) { diff --git a/R/utils-pipe.R b/R/utils-pipe.R new file mode 100644 index 00000000..fd0b1d13 --- /dev/null +++ b/R/utils-pipe.R @@ -0,0 +1,14 @@ +#' Pipe operator +#' +#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +#' +#' @name %>% +#' @rdname pipe +#' @keywords internal +#' @export +#' @importFrom magrittr %>% +#' @usage lhs \%>\% rhs +#' @param lhs A value or the magrittr placeholder. +#' @param rhs A function call using the magrittr semantics. +#' @return The result of calling `rhs(lhs)`. +NULL diff --git a/man/covid_hosp_facility.Rd b/man/covid_hosp_facility.Rd index 5f02a196..83804176 100644 --- a/man/covid_hosp_facility.Rd +++ b/man/covid_hosp_facility.Rd @@ -31,8 +31,10 @@ in a variety of ways. } \examples{ \donttest{ -call <- covid_hosp_facility(hospital_pks = "100075", collection_weeks = epirange(20200101, 20200501)) -fetch_tbl(call) +covid_hosp_facility( + hospital_pks = "100075", + collection_weeks = epirange(20200101, 20200501) +) \%>\% fetch_tbl() } } \seealso{ diff --git a/man/covid_hosp_facility_lookup.Rd b/man/covid_hosp_facility_lookup.Rd index 1d68073e..7c501bac 100644 --- a/man/covid_hosp_facility_lookup.Rd +++ b/man/covid_hosp_facility_lookup.Rd @@ -39,8 +39,7 @@ For instance, specifying both city and state are not supported. } \examples{ \donttest{ -call <- covid_hosp_facility_lookup(state = "fl") -fetch_tbl(call) +covid_hosp_facility_lookup(state = "fl") \%>\% fetch_tbl() } } \seealso{ diff --git a/man/covid_hosp_state_timeseries.Rd b/man/covid_hosp_state_timeseries.Rd index ca070816..da0b7181 100644 --- a/man/covid_hosp_state_timeseries.Rd +++ b/man/covid_hosp_state_timeseries.Rd @@ -29,7 +29,9 @@ Starting October 1, 2022, some facilities are only required to report annually. } \examples{ \donttest{ -call <- covid_hosp_state_timeseries(states = "fl", dates = epirange(20200101, 20200501)) -fetch_tbl(call) +covid_hosp_state_timeseries( + states = "fl", + dates = epirange(20200101, 20200501) +) \%>\% fetch_tbl() } } diff --git a/man/covidcast.Rd b/man/covidcast.Rd index 1781c19c..5a98b750 100644 --- a/man/covidcast.Rd +++ b/man/covidcast.Rd @@ -51,15 +51,14 @@ COVIDcast public dashboard: \url{https://delphi.cmu.edu/covidcast/} } \examples{ \donttest{ -call <- covidcast( +covidcast( data_source = "jhu-csse", signals = "confirmed_7dav_incidence_prop", time_type = "day", geo_type = "state", time_values = epirange(20200601, 20200801), geo_values = "ca,fl" -) -fetch_tbl(call) +) \%>\% fetch_tbl() } } \seealso{ diff --git a/man/covidcast_meta.Rd b/man/covidcast_meta.Rd index 05c71751..35bc5367 100644 --- a/man/covidcast_meta.Rd +++ b/man/covidcast_meta.Rd @@ -19,8 +19,7 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/covidcast_meta.ht } \examples{ \donttest{ -call_meta <- covidcast_meta() -fetch_classic(call_meta) +covidcast_meta() \%>\% fetch_classic() } } diff --git a/man/create_epidata_call.Rd b/man/create_epidata_call.Rd deleted file mode 100644 index 764f3aeb..00000000 --- a/man/create_epidata_call.Rd +++ /dev/null @@ -1,28 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/epidatacall.R -\name{create_epidata_call} -\alias{create_epidata_call} -\title{an abstraction that holds the information needed to make an epidata call} -\usage{ -create_epidata_call( - endpoint, - params, - meta = NULL, - only_supports_classic = FALSE -) -} -\arguments{ -\item{endpoint}{the epidata endpoint to call} - -\item{params}{the parameters to pass to the epidata endpoint} - -\item{meta}{meta data to attach to the epidata call} - -\item{only_supports_classic}{if true only classic format is supported} -} -\value{ -an epidata_call instance -} -\description{ -an abstraction that holds the information needed to make an epidata call -} diff --git a/man/delphi.Rd b/man/delphi.Rd index 7ca7e733..5a551def 100644 --- a/man/delphi.Rd +++ b/man/delphi.Rd @@ -19,7 +19,6 @@ API docs: https://cmu-delphi.github.io/delphi-epidata/api/delphi.html } \examples{ \donttest{ -call <- delphi(system = "ec", epiweek = 202006) -fetch_classic(call) +delphi(system = "ec", epiweek = 202006) \%>\% fetch_classic() } } diff --git a/man/dengue_nowcast.Rd b/man/dengue_nowcast.Rd index e2b20964..9be632e9 100644 --- a/man/dengue_nowcast.Rd +++ b/man/dengue_nowcast.Rd @@ -21,8 +21,10 @@ API docs: https://cmu-delphi.github.io/delphi-epidata/api/dengue_nowcast.html TODO: what are valid locations here? } \examples{ -\donttest{ -call <- dengue_nowcast(locations = "?", epiweeks = epirange(201501, 202001)) -fetch_classic(call) +\dontrun{ +dengue_nowcast( + locations = "?", + epiweeks = epirange(201501, 202001) +) \%>\% fetch_classic() } } diff --git a/man/ecdc_ili.Rd b/man/ecdc_ili.Rd index cae05211..7726d333 100644 --- a/man/ecdc_ili.Rd +++ b/man/ecdc_ili.Rd @@ -31,7 +31,6 @@ The list of location argument can be found in } \examples{ \donttest{ -call <- ecdc_ili(regions = "austria", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +ecdc_ili(regions = "austria", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } } diff --git a/man/epidata_call.Rd b/man/epidata_call.Rd new file mode 100644 index 00000000..bb67bbfd --- /dev/null +++ b/man/epidata_call.Rd @@ -0,0 +1,124 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/epidatacall.R +\name{create_epidata_call} +\alias{create_epidata_call} +\alias{epidata_call} +\alias{fetch_tbl} +\alias{fetch_classic} +\alias{fetch_csv} +\alias{fetch_json} +\alias{request_url} +\alias{with_base_url} +\title{An abstraction that holds information needed to make an epidata request} +\usage{ +create_epidata_call( + endpoint, + params, + meta = NULL, + only_supports_classic = FALSE +) + +fetch_tbl(epidata_call, fields = NULL, disable_date_parsing = FALSE) + +fetch_classic(epidata_call, fields = NULL, disable_date_parsing = FALSE) + +fetch_csv(epidata_call, fields = NULL) + +fetch_json(epidata_call, fields = NULL, disable_date_parsing = FALSE) + +request_url(epidata_call, format_type = "classic", fields = NULL) + +with_base_url(epidata_call, base_url) +} +\arguments{ +\item{endpoint}{the epidata endpoint to call} + +\item{params}{the parameters to pass to the epidata endpoint} + +\item{meta}{meta data to attach to the epidata call} + +\item{only_supports_classic}{if true only classic format is supported} + +\item{epidata_call}{an instance of \code{epidata_call}} + +\item{fields}{filter fields} + +\item{disable_date_parsing}{disable automatic date parsing} + +\item{format_type}{format to return one of classic,json,csv} + +\item{base_url}{base URL to use} +} +\value{ +\itemize{ +\item For \code{create_epidata_call}: an \code{epidata_call} object +} + +\itemize{ +\item For \code{fetch_tbl}: a \code{\link[tibble:tibble]{tibble::tibble}} +} + +\itemize{ +\item For \code{fetch_classic}: the "classic" 3-element list format (see above) +} + +\itemize{ +\item For \code{fetch_csv}: a string containing CSV text, with the \code{"epidata_csv"} class added +} + +\itemize{ +\item For \code{fetch_json}: a \code{data.frame} form of the object from the parsed JSON +response +} + +\itemize{ +\item For \code{request_url}: a string containing the URL +} + +\itemize{ +\item For \code{with_base_url}: another \code{epidata_call} object +} +} +\description{ +\code{epidata_call} objects are generated by endpoint functions like \code{\link{covidcast}}; +should be piped into a \verb{fetch_*} function to fetch and +format the data: +\itemize{ +\item \code{fetch_tbl}: fetches the data, formats as a \code{\link[tibble:tibble]{tibble::tibble}} +\item \code{fetch_classic}: fetches the data in "classic" format: a list with three +elements: +\itemize{ +\item \code{"epidata"}: some R object (typically a \code{data.frame}) holding the data, +if the request was successful +\item \code{"message"}: string; either \code{"success"} or some error/warning message +\item \code{"result"}: integer; either 1 or some error/warning code +} +\item \code{fetch_csv}: fetches the data, formats as a string containing CSV data, +with a special class \code{"epidata_csv"} added to prevent accidentally +printing a huge number of lines +\item \code{fetch_json}: fetches the data in JSON format then converts to a \code{data.frame} +} + +\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. + +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 +} +} +\examples{ +covidcast( + data_source = "jhu-csse", + signals = "confirmed_7dav_incidence_prop", + time_type = "day", + geo_type = "state", + time_values = epirange(20200601, 20200801), + geo_values = c("ca", "fl") +) \%>\% fetch_tbl() + +} diff --git a/man/fetch_classic.Rd b/man/fetch_classic.Rd deleted file mode 100644 index 46727177..00000000 --- a/man/fetch_classic.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/epidatacall.R -\name{fetch_classic} -\alias{fetch_classic} -\title{fetches the data and returns the classic format} -\usage{ -fetch_classic(epidata_call, fields = NULL, disable_date_parsing = FALSE) -} -\arguments{ -\item{epidata_call}{and instance of epidata_call} - -\item{fields}{filter fields} - -\item{disable_date_parsing}{disable automatic date parsing} -} -\value{ -parsed json message -} -\description{ -fetches the data and returns the classic format -} diff --git a/man/fetch_csv.Rd b/man/fetch_csv.Rd deleted file mode 100644 index 5893863a..00000000 --- a/man/fetch_csv.Rd +++ /dev/null @@ -1,19 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/epidatacall.R -\name{fetch_csv} -\alias{fetch_csv} -\title{fetches the data and returns the CSV text} -\usage{ -fetch_csv(epidata_call, fields = NULL) -} -\arguments{ -\item{epidata_call}{and instance of epidata_call} - -\item{fields}{filter fields} -} -\value{ -CSV text -} -\description{ -fetches the data and returns the CSV text -} diff --git a/man/fetch_df.Rd b/man/fetch_df.Rd deleted file mode 100644 index bebdea81..00000000 --- a/man/fetch_df.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/epidatacall.R -\name{fetch_df} -\alias{fetch_df} -\title{fetches the data and returns data frame} -\usage{ -fetch_df(epidata_call, fields = NULL, disable_date_parsing = FALSE) -} -\arguments{ -\item{epidata_call}{and instance of epidata_call} - -\item{fields}{filter fields} - -\item{disable_date_parsing}{disable automatic date parsing} -} -\value{ -data.frame -} -\description{ -fetches the data and returns data frame -} diff --git a/man/fetch_json.Rd b/man/fetch_json.Rd deleted file mode 100644 index 8ad19c3b..00000000 --- a/man/fetch_json.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/epidatacall.R -\name{fetch_json} -\alias{fetch_json} -\title{fetches the data and returns the josn format} -\usage{ -fetch_json(epidata_call, fields = NULL, disable_date_parsing = FALSE) -} -\arguments{ -\item{epidata_call}{and instance of epidata_call} - -\item{fields}{filter fields} - -\item{disable_date_parsing}{disable automatic date parsing} -} -\value{ -parsed json message -} -\description{ -fetches the data and returns the josn format -} diff --git a/man/fetch_tbl.Rd b/man/fetch_tbl.Rd deleted file mode 100644 index f84e96d2..00000000 --- a/man/fetch_tbl.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/epidatacall.R -\name{fetch_tbl} -\alias{fetch_tbl} -\title{fetches the data and returns data frame} -\usage{ -fetch_tbl(epidata_call, fields = NULL, disable_date_parsing = FALSE) -} -\arguments{ -\item{epidata_call}{and instance of epidata_call} - -\item{fields}{filter fields} - -\item{disable_date_parsing}{disable automatic date parsing} -} -\value{ -tibble -} -\description{ -fetches the data and returns data frame -} diff --git a/man/flusurv.Rd b/man/flusurv.Rd index 4767d2a3..36dfeb85 100644 --- a/man/flusurv.Rd +++ b/man/flusurv.Rd @@ -32,7 +32,6 @@ The list of location argument can be found in } \examples{ \donttest{ -call <- flusurv(locations = "CA", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +flusurv(locations = "CA", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } } diff --git a/man/fluview.Rd b/man/fluview.Rd index 9decdc8c..1d48d99d 100644 --- a/man/fluview.Rd +++ b/man/fluview.Rd @@ -36,6 +36,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{ -call <- fluview(regions = "nat", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +fluview(regions = "nat", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } diff --git a/man/fluview_clinical.Rd b/man/fluview_clinical.Rd index fba10898..363db4d5 100644 --- a/man/fluview_clinical.Rd +++ b/man/fluview_clinical.Rd @@ -26,8 +26,6 @@ API docs: https://cmu-delphi.github.io/delphi-epidata/api/fluview_clinical.html } \examples{ \donttest{ -# can take a couple minutes; donttesting while cmu-delphi/delphi-epidata#48 is unresolved -call <- fluview_clinical(regions = "nat", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +fluview_clinical(regions = "nat", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } } diff --git a/man/gft.Rd b/man/gft.Rd index 1da80988..4ca5367e 100644 --- a/man/gft.Rd +++ b/man/gft.Rd @@ -26,6 +26,5 @@ can be found in \url{https://github.com/cmu-delphi/delphi-epidata/blob/main/labe \url{https://github.com/cmu-delphi/delphi-epidata/blob/main/labels/cities.txt}. } \examples{ -call <- gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +gft(locations = "hhs1", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } diff --git a/man/kcdc_ili.Rd b/man/kcdc_ili.Rd index 8eb74b24..c2465d7d 100644 --- a/man/kcdc_ili.Rd +++ b/man/kcdc_ili.Rd @@ -27,8 +27,7 @@ API docs: https://cmu-delphi.github.io/delphi-epidata/api/kcdc_ili.html TODO: find a non-trivial region } \examples{ -\donttest{ -call <- kcdc_ili(regions = "?", epiweeks = epirange(201201, 202001)) -fetch_tbl(call) +\dontrun{ +kcdc_ili(regions = "?", epiweeks = epirange(201201, 202001)) \%>\% fetch_tbl() } } diff --git a/man/nidss_dengue.Rd b/man/nidss_dengue.Rd index 5bdb0035..6a48dc0c 100644 --- a/man/nidss_dengue.Rd +++ b/man/nidss_dengue.Rd @@ -26,7 +26,6 @@ Possible location inputs can be found in } \examples{ \donttest{ -call <- nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +nidss_dengue(locations = "taipei", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } } diff --git a/man/nidss_flu.Rd b/man/nidss_flu.Rd index aa9215a9..7266dace 100644 --- a/man/nidss_flu.Rd +++ b/man/nidss_flu.Rd @@ -28,7 +28,6 @@ API docs: \url{https://cmu-delphi.github.io/delphi-epidata/api/nidss_flu.html} } \examples{ \donttest{ -call <- nidss_flu(regions = "taipei", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +nidss_flu(regions = "taipei", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } } diff --git a/man/nowcast.Rd b/man/nowcast.Rd index f951bae8..61ea8e80 100644 --- a/man/nowcast.Rd +++ b/man/nowcast.Rd @@ -25,7 +25,6 @@ The full list of location inputs can be accsssed at } \examples{ \donttest{ -call <- nowcast(location = "ca", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +nowcast(location = "ca", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } } diff --git a/man/paho_dengue.Rd b/man/paho_dengue.Rd index 7faee859..6e7c767a 100644 --- a/man/paho_dengue.Rd +++ b/man/paho_dengue.Rd @@ -25,7 +25,6 @@ API docs: https://cmu-delphi.github.io/delphi-epidata/api/paho_dengue.html } \examples{ \donttest{ -call <- paho_dengue(regions = "ca", epiweeks = epirange(201201, 202001)) -fetch_classic(call) +paho_dengue(regions = "ca", epiweeks = epirange(201201, 202001)) \%>\% fetch_classic() } } diff --git a/man/parse_api_week.Rd b/man/parse_api_week.Rd index 362acd90..386aa2db 100644 --- a/man/parse_api_week.Rd +++ b/man/parse_api_week.Rd @@ -6,6 +6,9 @@ \usage{ parse_api_week(value) } +\arguments{ +\item{value}{value to be converted to an epiweek} +} \value{ a date } diff --git a/man/pipe.Rd b/man/pipe.Rd new file mode 100644 index 00000000..a648c296 --- /dev/null +++ b/man/pipe.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-pipe.R +\name{\%>\%} +\alias{\%>\%} +\title{Pipe operator} +\usage{ +lhs \%>\% rhs +} +\arguments{ +\item{lhs}{A value or the magrittr placeholder.} + +\item{rhs}{A function call using the magrittr semantics.} +} +\value{ +The result of calling \code{rhs(lhs)}. +} +\description{ +See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +} +\keyword{internal} diff --git a/man/pvt_afhsb.Rd b/man/pvt_afhsb.Rd index 4d2251ea..16f88d23 100644 --- a/man/pvt_afhsb.Rd +++ b/man/pvt_afhsb.Rd @@ -22,8 +22,12 @@ pvt_afhsb(auth, locations, epiweeks, flu_types) API docs: https://cmu-delphi.github.io/delphi-epidata/api/afhsb.html } \examples{ -\donttest{ -call <- pvt_afhsb(auth = "yourkey", "fl,ca", epirange(202001, 202110), "flu1,flu2-flu1") -fetch_tbl(call) +\dontrun{ +pvt_afhsb( + auth = "yourkey", + "fl,ca", + epirange(202001, 202110), + "flu1,flu2-flu1" +) \%>\% fetch_tbl() } } diff --git a/man/pvt_cdc.Rd b/man/pvt_cdc.Rd index 07482b6c..61568083 100644 --- a/man/pvt_cdc.Rd +++ b/man/pvt_cdc.Rd @@ -20,8 +20,11 @@ pvt_cdc(auth, epiweeks, locations) API docs: https://cmu-delphi.github.io/delphi-epidata/api/cdc.html } \examples{ -\donttest{ -call <- pvt_cdc(auth = "yourkey", epirange(20210101, 20210201), "fl,ca") -fetch_tbl(call) +\dontrun{ +pvt_cdc( + auth = "yourkey", + epirange(20210101, 20210201), + "fl,ca" +) \%>\% fetch_tbl() } } diff --git a/man/pvt_dengue_sensors.Rd b/man/pvt_dengue_sensors.Rd index 953890ad..f7ff1239 100644 --- a/man/pvt_dengue_sensors.Rd +++ b/man/pvt_dengue_sensors.Rd @@ -25,8 +25,12 @@ API docs: https://cmu-delphi.github.io/delphi-epidata/api/dengue_sensors.html TODO: what are valid locations and names? } \examples{ -\donttest{ -call <- pvt_dengue_sensors(auth = "yourkey", names = "?", locations = "?", epiweeks = epirange(201501, 202001)) -fetch_classic(call) +\dontrun{ +pvt_dengue_sensors( + auth = "yourkey", + names = "?", + locations = "?", + epiweeks = epirange(201501, 202001) +) \%>\% fetch_classic() } } diff --git a/man/pvt_ght.Rd b/man/pvt_ght.Rd index d39b78bd..6cdb487a 100644 --- a/man/pvt_ght.Rd +++ b/man/pvt_ght.Rd @@ -25,8 +25,12 @@ API docs: https://cmu-delphi.github.io/delphi-epidata/api/ght.html TODO: find a non-trivial query } \examples{ -\donttest{ -call <- pvt_ght(auth = "yourkey", locations = "ca", epiweeks = epirange(201201, 202001), query = "?") -fetch_classic(call) +\dontrun{ +pvt_ght( + auth = "yourkey", + locations = "ca", + epiweeks = epirange(201201, 202001), + query = "?" +) \%>\% fetch_classic() } } diff --git a/man/pvt_norostat.Rd b/man/pvt_norostat.Rd index 4d01a831..e421fe55 100644 --- a/man/pvt_norostat.Rd +++ b/man/pvt_norostat.Rd @@ -20,12 +20,11 @@ pvt_norostat(auth, location, epiweeks) API docs: https://cmu-delphi.github.io/delphi-epidata/api/norostat.html } \examples{ -\donttest{ -call <- pvt_norostat( +\dontrun{ +pvt_norostat( auth = "yourkey", location = "Minnesota, Ohio, Oregon, Tennessee, and Wisconsin", epiweeks = epirange(201201, 202001) -) -fetch_classic(call) +) \%>\% fetch_classic() } } diff --git a/man/pvt_quidel.Rd b/man/pvt_quidel.Rd index f2fbe4f5..a067d4de 100644 --- a/man/pvt_quidel.Rd +++ b/man/pvt_quidel.Rd @@ -20,8 +20,11 @@ pvt_quidel(auth, epiweeks, locations) API docs: https://cmu-delphi.github.io/delphi-epidata/api/quidel.html } \examples{ -\donttest{ -call <- pvt_quidel(auth = "yourkey", epiweeks = epirange(201201, 202001), locations = "hhs1") -fetch_classic(call) +\dontrun{ +pvt_quidel( + auth = "yourkey", + epiweeks = epirange(201201, 202001), + locations = "hhs1" +) \%>\% fetch_classic() } } diff --git a/man/pvt_sensors.Rd b/man/pvt_sensors.Rd index 961a6109..a9f1a1c6 100644 --- a/man/pvt_sensors.Rd +++ b/man/pvt_sensors.Rd @@ -22,8 +22,12 @@ pvt_sensors(auth, names, locations, epiweeks) API docs: https://cmu-delphi.github.io/delphi-epidata/api/sensors.html } \examples{ -\donttest{ -call <- pvt_sensors(auth = "yourkey", names = "sar3", locations = "nat", epiweeks = epirange(201501, 202001)) -fetch_classic(call) +\dontrun{ +pvt_sensors( + auth = "yourkey", + names = "sar3", + locations = "nat", + epiweeks = epirange(201501, 202001) +) \%>\% fetch_classic() } } diff --git a/man/pvt_twitter.Rd b/man/pvt_twitter.Rd index 842c665b..6b77307e 100644 --- a/man/pvt_twitter.Rd +++ b/man/pvt_twitter.Rd @@ -22,8 +22,7 @@ pvt_twitter(auth, locations, dates = NULL, epiweeks = NULL) API docs: https://cmu-delphi.github.io/delphi-epidata/api/twitter.html } \examples{ -\donttest{ -call <- pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) -fetch_tbl(call) +\dontrun{ +pvt_twitter(auth = "yourkey", locations = "CA", epiweeks = epirange(201501, 202001)) \%>\% fetch_tbl() } } diff --git a/man/request_url.Rd b/man/request_url.Rd deleted file mode 100644 index e636e745..00000000 --- a/man/request_url.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/epidatacall.R -\name{request_url} -\alias{request_url} -\title{returns the full request url for the given epidata_call} -\usage{ -request_url(epidata_call, format_type = "classic", fields = NULL) -} -\arguments{ -\item{epidata_call}{and instance of epidata_call} - -\item{format_type}{format to return one of classic,json,csv} - -\item{fields}{filter fields} -} -\value{ -full url -} -\description{ -returns the full request url for the given epidata_call -} diff --git a/man/wiki.Rd b/man/wiki.Rd index c1d4a441..cd1cbd16 100644 --- a/man/wiki.Rd +++ b/man/wiki.Rd @@ -25,7 +25,6 @@ API docs: https://cmu-delphi.github.io/delphi-epidata/api/wiki.html } \examples{ \donttest{ -call <- wiki(articles = "avian_influenza", epiweeks = epirange(201501, 202001)) -fetch_tbl(call) +wiki(articles = "avian_influenza", epiweeks = epirange(201501, 202001)) \%>\% fetch_tbl() } } diff --git a/man/with_base_url.Rd b/man/with_base_url.Rd deleted file mode 100644 index 252a6439..00000000 --- a/man/with_base_url.Rd +++ /dev/null @@ -1,16 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/epidatacall.R -\name{with_base_url} -\alias{with_base_url} -\title{use a different base url} -\usage{ -with_base_url(epidata_call, base_url) -} -\arguments{ -\item{epidata_call}{and instance of epidata_call} - -\item{base_url}{basee url to use} -} -\description{ -use a different base url -} diff --git a/vignettes/covidcast.Rmd b/vignettes/covidcast.Rmd index 4761f0c2..2e8b7c6a 100644 --- a/vignettes/covidcast.Rmd +++ b/vignettes/covidcast.Rmd @@ -28,21 +28,21 @@ epicall <- covidcast_api$sources$`fb-survey`$signals$smoothed_cli$call("nation", ``` ```{r} -epicall %>% fetch_classic() +epicall %>% fetch_tbl() ``` ```{r} -epicall %>% fetch_json() +epicall %>% fetch_classic() ``` ```{r} -epicall %>% fetch_csv() +epicall %>% fetch_json() ``` ```{r} -epicall %>% fetch_df() +epicall %>% fetch_csv() ``` ```{r} -# epicall %>% with_base_url("https://staging.delphi.cmu.edu/epidata/") %>% fetch_df() +# epicall %>% with_base_url("https://staging.delphi.cmu.edu/epidata/") %>% fetch_tbl() ``` diff --git a/vignettes/epidatr.Rmd b/vignettes/epidatr.Rmd index b09ad253..c27907b8 100644 --- a/vignettes/epidatr.Rmd +++ b/vignettes/epidatr.Rmd @@ -26,22 +26,23 @@ library("magrittr") epicall <- covidcast("fb-survey", "smoothed_cli", "day", "nation", epirange(20210405, 20210410), "us") ``` + ```{r} -epicall %>% fetch_classic() +epicall %>% fetch_tbl() ``` ```{r} -epicall %>% fetch_json() +epicall %>% fetch_classic() ``` ```{r} -epicall %>% fetch_csv() +epicall %>% fetch_json() ``` ```{r} -epicall %>% fetch_df() +epicall %>% fetch_csv() ``` ```{r} -# epicall %>% with_base_url("https://staging.delphi.cmu.edu/epidata/") %>% fetch_df() +# epicall %>% with_base_url("https://staging.delphi.cmu.edu/epidata/") %>% fetch_tbl() ```