Skip to content

Commit

Permalink
combine epiweek and date into time_type and time_value for pub_wiki
Browse files Browse the repository at this point in the history
This makes the pub_wiki interfaces better match that of
the other endpoints. Also build in time_values default of "*".
  • Loading branch information
nmdefries committed Jan 5, 2024
1 parent f656700 commit 71de7d5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
33 changes: 24 additions & 9 deletions R/endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -2172,13 +2172,18 @@ pvt_twitter <- function(
#'
#' @examples
#' \dontrun{
#' pub_wiki(articles = "avian_influenza", epiweeks = epirange(201501, 201601))
#' pub_wiki(
#' articles = "avian_influenza",
#' time_type = "week",
#' time_values = epirange(201501, 201601)
#' )
#' }
#' @param articles character. Articles to fetch.
#' @param ... not used for values, forces later arguments to bind by name
#' @param dates [`timeset`]. Dates to fetch. Mutually exclusive with `epiweeks`.
#' @param epiweeks [`timeset`]. Epiweeks to fetch. Mutually exclusive with
#' `dates`.
#' @param time_type string. The temporal resolution of the data (either "day" or
#' "week", depending on signal).
#' @param time_values [`timeset`]. Dates or epiweeks to fetch. Defaults to all
#' ("*") 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()`.
Expand All @@ -2188,24 +2193,34 @@ pvt_twitter <- function(
pub_wiki <- function(
articles,
...,
dates = NULL,
epiweeks = NULL,
time_type = c("day", "week"),
time_values = "*",
hours = NULL,
language = "en",
fetch_args = fetch_args_list()) {
rlang::check_dots_empty()

time_type <- match.arg(time_type)
if (time_type == "day") {
dates <- time_value
epiweeks <- NULL
dates <- get_wildcard_equivalent_dates(dates, "day")
} else {
dates <- NULL
epiweeks <- time_value
epiweeks <- get_wildcard_equivalent_dates(epiweeks, "week")
}

assert_character_param("articles", articles)
assert_character_param("time_type", time_type, len = 1)
assert_timeset_param("time_values", time_values)
assert_timeset_param("dates", dates, required = FALSE)
assert_timeset_param("epiweeks", epiweeks, required = FALSE)
assert_integerish_param("hours", hours, required = FALSE)
assert_character_param("language", language, len = 1, required = FALSE)
dates <- parse_timeset_input(dates)
epiweeks <- parse_timeset_input(epiweeks)

if (!xor(is.null(dates), is.null(epiweeks))) {
stop("exactly one of `dates` and `epiweeks` is required")
}
time_field <- if (!is.null(dates)) {
create_epidata_field_info("date", "date")
} else {
Expand Down
17 changes: 11 additions & 6 deletions man/pub_wiki.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/testthat/test-endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ test_that("basic_epidata_call", {
) %>% request_url())
expect_no_error(pub_wiki(
articles = "avian_influenza",
epiweeks = epirange(201501, 202001),
time_type = "week",
time_values = epirange(201501, 202001),
fetch_args = fetch_args_list(dry_run = TRUE)
) %>% request_url())
})
Expand Down Expand Up @@ -243,6 +244,7 @@ test_that("endpoints fail when given args via dots", {
expect_error(
pub_wiki(
articles = "avian_influenza",
time_type = "week",
date_range = epirange(201501, 202001)
),
regexp = dots_error
Expand Down
7 changes: 6 additions & 1 deletion vignettes/signal-discovery.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,12 @@ pub_paho_dengue(regions = "ca", epiweeks = epirange(200201, 202319))
API docs: <https://cmu-delphi.github.io/delphi-epidata/api/wiki.html>

```{r, eval = FALSE}
pub_wiki(language = "en", articles = "influenza", epiweeks = epirange(202001, 202319))
pub_wiki(
language = "en",
articles = "influenza",
time_type = "week",
time_values = epirange(202001, 202319)
)
```

### Private methods
Expand Down

0 comments on commit 71de7d5

Please sign in to comment.