Skip to content

Combine timetype wiki twitter #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- `get_api_key` no longer reads from R options and only uses environment variables (#217).
- Fix documentation related to CRAN submission.
- Fix some errors from passing "" as a key.
- `pvt_twitter` and `pub_wiki` now use `time_type` and `time_values` args instead of mutually exclusive `dates` and `epiweeks` (#236). This matches the interface of the `pub_covidcast` endpoint.
- All endpoints now support the use of "\*" as a wildcard to fetch all dates or epiweeks (#234).

# epidatr 1.0.0
Expand Down
69 changes: 49 additions & 20 deletions R/endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,10 @@ pub_covid_hosp_facility <- function(
cli::cli_warn(coercion_msg, class = "epidatr__epirange_week_coercion")
collection_weeks <- reformat_epirange(collection_weeks, to_type = "day")
# Single week date.
} else if ((test_integerish(collection_weeks) || test_character(collection_weeks)) &&
nchar(collection_weeks) == 6) {
} else if (
(test_integerish(collection_weeks) || test_character(collection_weeks)) &&
nchar(collection_weeks) == 6
) {
cli::cli_warn(coercion_msg, class = "epidatr__single_week_coercion")
collection_weeks <- parse_api_week(collection_weeks)
}
Expand Down Expand Up @@ -2094,15 +2096,17 @@ pvt_sensors <- function(
#' pvt_twitter(
#' auth = Sys.getenv("SECRET_API_AUTH_TWITTER"),
#' locations = "CA",
#' epiweeks = epirange(201501, 202001)
#' time_type = "week",
#' time_values = epirange(201501, 202001)
#' )
#' }
#' @param auth string. Restricted access key (not the same as API key).
#' @param locations character. Locations 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 fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`.
#' @return [`tibble::tibble`]
#' @keywords endpoint
Expand All @@ -2111,21 +2115,31 @@ pvt_twitter <- function(
auth,
locations,
...,
dates = NULL,
epiweeks = NULL,
time_type = c("day", "week"),
time_values = "*",
fetch_args = fetch_args_list()) {
rlang::check_dots_empty()

time_type <- match.arg(time_type)
Copy link
Contributor

@dsweber2 dsweber2 Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure this is necessary, or else pub_covidcast is missing something important, since it doesn't do this. I would maybe make a function for this bit of logic if it is necessary, since it (should?) be included 3 times.

(I mean this and if statements below)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's necessary because the default value is c("day", "week"), but you can only have one time_type. match.arg() takes the first if the default is not overridden. It's a common convention in R that if the argument can be one of several string values, you make the default list all of them, and match.arg() takes care of picking the first (if none is provided) or ensuring the provided value is valid otherwise.

pub_covidcast() doesn't set a default for this argument, so it's not strictly necessary. However, pub_covidcast() could do match.arg(time_type, c("day", "week")) to enforce that the type must be one or the other but not both.

if (time_type == "day") {
dates <- time_values
epiweeks <- NULL
dates <- get_wildcard_equivalent_dates(dates, "day")
} else {
dates <- NULL
epiweeks <- time_values
epiweeks <- get_wildcard_equivalent_dates(epiweeks, "week")
}

assert_character_param("auth", auth, len = 1)
assert_character_param("locations", locations)
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)
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 Expand Up @@ -2163,13 +2177,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 @@ -2179,24 +2198,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_values
epiweeks <- NULL
dates <- get_wildcard_equivalent_dates(dates, "day")
} else {
dates <- NULL
epiweeks <- time_values
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.

14 changes: 8 additions & 6 deletions man/pvt_twitter.Rd

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

61 changes: 58 additions & 3 deletions tests/testthat/test-endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,27 @@ test_that("basic_epidata_call", {
expect_no_error(pvt_twitter(
auth = "yourkey",
locations = "CA",
epiweeks = epirange(201501, 202001),
time_type = "week",
time_values = epirange(201501, 202001),
fetch_args = fetch_args_list(dry_run = TRUE)
) %>% request_url())
expect_no_error(pvt_twitter(
auth = "yourkey",
locations = "CA",
time_type = "day",
time_values = epirange(20150101, 20200101),
fetch_args = fetch_args_list(dry_run = TRUE)
) %>% 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())
expect_no_error(pub_wiki(
articles = "avian_influenza",
time_type = "day",
time_values = epirange(20150101, 20200101),
fetch_args = fetch_args_list(dry_run = TRUE)
) %>% request_url())
})
Expand Down Expand Up @@ -322,6 +337,44 @@ test_that("endoints accept wildcard for date parameter", {
))
expect_identical(call$params$epiweeks$from, 100001)
expect_identical(call$params$epiweeks$to, 300001)

expect_no_error(call <- pvt_twitter(
auth = "yourkey",
locations = "CA",
time_type = "week",
time_values = "*",
fetch_args = fetch_args_list(dry_run = TRUE)
))
expect_identical(call$params$epiweeks$from, 100001)
expect_identical(call$params$epiweeks$to, 300001)

expect_no_error(call <- pvt_twitter(
auth = "yourkey",
locations = "CA",
time_type = "day",
time_values = "*",
fetch_args = fetch_args_list(dry_run = TRUE)
))
expect_identical(call$params$dates$from, 10000101)
expect_identical(call$params$dates$to, 30000101)

expect_no_error(call <- pub_wiki(
articles = "avian_influenza",
time_type = "week",
time_values = "*",
fetch_args = fetch_args_list(dry_run = TRUE)
))
expect_identical(call$params$epiweeks$from, 100001)
expect_identical(call$params$epiweeks$to, 300001)

expect_no_error(call <- pub_wiki(
articles = "avian_influenza",
time_type = "day",
time_values = "*",
fetch_args = fetch_args_list(dry_run = TRUE)
))
expect_identical(call$params$dates$from, 10000101)
expect_identical(call$params$dates$to, 30000101)
})

test_that("endpoints fail when given args via dots", {
Expand Down Expand Up @@ -413,13 +466,15 @@ test_that("endpoints fail when given args via dots", {
pvt_twitter(
auth = "yourkey",
locations = "CA",
date_range = epirange(201501, 202001)
time_type = "week",
time_range = epirange(201501, 202001)
),
regexp = dots_error
)
expect_error(
pub_wiki(
articles = "avian_influenza",
time_type = "week",
date_range = epirange(201501, 202001)
),
regexp = dots_error
Expand Down
10 changes: 8 additions & 2 deletions 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 Expand Up @@ -373,7 +378,8 @@ API docs: <https://cmu-delphi.github.io/delphi-epidata/api/twitter.html>
pvt_twitter(
auth = Sys.getenv("SECRET_API_AUTH_TWITTER"),
locations = "nat",
epiweeks = epirange(200301, 202105)
time_type = "week",
time_values = epirange(200301, 202105)
)
```

Expand Down