Skip to content

Commit

Permalink
Merge pull request #23 from socdataR/update_docs
Browse files Browse the repository at this point in the history
send_query, update intro vignette
  • Loading branch information
jtr13 authored Mar 15, 2024
2 parents 40b90ef + ad19d69 commit cc34ce6
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 327 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Type: Package
Title: User Friendly Access to CDC Wonder API
Version: 0.0.0.9000
Authors@R: c(
person("Joyce", "Robbins", email="[email protected]",
person("Joyce", "Robbins", email="[email protected]",
role = c("cre", "aut")))
Description: Simplifies the process of formulating CDC Wonder API query
requests and interpreting query responses in order to obtain federal
Expand Down
32 changes: 16 additions & 16 deletions R/send_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
#'
#' Based on https://github.com/hrbrmstr/wondr/blob/master/R/wondr.r
#'
#' @param filename .xml query list
#' @param database_id CDC WONDER database ID, if NULL parsed from filename
#' @param add_accept if \code{TRUE} (default) accept CDC Wonder API data use restrictions
#' @param db Indicate the database by code
#'
#' @param fn \code{.xml} query list exported from CDC Wonder web interface
#'
#' @param agree if \code{TRUE} (default) accept CDC Wonder API data use restrictions
#' @export
#' @examples \dontrun{
#' send_query("data-raw/D66_Defaults.xml")
#' send_query("testquery.xml", database_id = "D76")
#' send_query("D66", "data-raw/D66_Defaults.xml")
#' send_query("D76", "testquery.xml")
#' }
send_query <- function(filename, database_id = NULL, add_accept = TRUE) {
query <- xml2::read_xml(filename)
if (add_accept) {
accept <- read_xml("<parameter> <name>accept_datause_restrictions
#'
send_query <- function(db, fn, agree = TRUE) {
if (!agree) stop("You must agree to CDC terms.")

query <- xml2::read_xml(fn)
accept <- xml2::read_xml("<parameter> <name>accept_datause_restrictions
</name> <value>true</value> </parameter>")
xml2::xml_add_child(query, accept, .where = 0)
}
if (is.null(database_id)) database_id <- stringr::str_extract(filename, "D[0-9]+")
if (is.na(database_id)) stop ("The database ID must be contained in the filename or set as the database_id parameter.")
index <- purrr::map(dbnamelookup, ~which(.x == database_id)) %>% unlist()
if (length(index) == 0) stop (database_id, " is not available.")
xml2::xml_add_child(query, accept, .where = 0)

res <- httr::POST(sprintf("https://wonder.cdc.gov/controller/datarequest/%s", database_id),
res <- httr::POST(sprintf("https://wonder.cdc.gov/controller/datarequest/%s", db),
body=list(request_xml=query),
encode = "form")
out <- httr::content(res, as="text")
Expand All @@ -38,3 +37,4 @@ send_query <- function(filename, database_id = NULL, add_accept = TRUE) {
xml2::read_xml(out) %>% make_query_table()
}
}

Loading

0 comments on commit cc34ce6

Please sign in to comment.