Skip to content

Commit 2657bed

Browse files
authored
Merge pull request #236 from cmu-delphi/ndefries/combine-timetype-wiki-twitter
Combine timetype wiki twitter
2 parents d277802 + 3ae82e2 commit 2657bed

File tree

6 files changed

+135
-37
lines changed

6 files changed

+135
-37
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- `get_api_key` no longer reads from R options and only uses environment variables (#217).
2121
- Fix documentation related to CRAN submission.
2222
- Fix some errors from passing "" as a key.
23+
- `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.
2324
- All endpoints now support the use of "\*" as a wildcard to fetch all dates or epiweeks (#234).
2425

2526
# epidatr 1.0.0

R/endpoints.R

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ pub_covid_hosp_facility <- function(
209209
cli::cli_warn(coercion_msg, class = "epidatr__epirange_week_coercion")
210210
collection_weeks <- reformat_epirange(collection_weeks, to_type = "day")
211211
# Single week date.
212-
} else if ((test_integerish(collection_weeks) || test_character(collection_weeks)) &&
213-
nchar(collection_weeks) == 6) {
212+
} else if (
213+
(test_integerish(collection_weeks) || test_character(collection_weeks)) &&
214+
nchar(collection_weeks) == 6
215+
) {
214216
cli::cli_warn(coercion_msg, class = "epidatr__single_week_coercion")
215217
collection_weeks <- parse_api_week(collection_weeks)
216218
}
@@ -2094,15 +2096,17 @@ pvt_sensors <- function(
20942096
#' pvt_twitter(
20952097
#' auth = Sys.getenv("SECRET_API_AUTH_TWITTER"),
20962098
#' locations = "CA",
2097-
#' epiweeks = epirange(201501, 202001)
2099+
#' time_type = "week",
2100+
#' time_values = epirange(201501, 202001)
20982101
#' )
20992102
#' }
21002103
#' @param auth string. Restricted access key (not the same as API key).
21012104
#' @param locations character. Locations to fetch.
21022105
#' @param ... not used for values, forces later arguments to bind by name
2103-
#' @param dates [`timeset`]. Dates to fetch. Mutually exclusive with `epiweeks`.
2104-
#' @param epiweeks [`timeset`]. Epiweeks to fetch. Mutually exclusive with
2105-
#' `dates`.
2106+
#' @param time_type string. The temporal resolution of the data (either "day" or
2107+
#' "week", depending on signal).
2108+
#' @param time_values [`timeset`]. Dates or epiweeks to fetch. Defaults to all
2109+
#' ("*") dates.
21062110
#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`.
21072111
#' @return [`tibble::tibble`]
21082112
#' @keywords endpoint
@@ -2111,21 +2115,31 @@ pvt_twitter <- function(
21112115
auth,
21122116
locations,
21132117
...,
2114-
dates = NULL,
2115-
epiweeks = NULL,
2118+
time_type = c("day", "week"),
2119+
time_values = "*",
21162120
fetch_args = fetch_args_list()) {
21172121
rlang::check_dots_empty()
21182122

2123+
time_type <- match.arg(time_type)
2124+
if (time_type == "day") {
2125+
dates <- time_values
2126+
epiweeks <- NULL
2127+
dates <- get_wildcard_equivalent_dates(dates, "day")
2128+
} else {
2129+
dates <- NULL
2130+
epiweeks <- time_values
2131+
epiweeks <- get_wildcard_equivalent_dates(epiweeks, "week")
2132+
}
2133+
21192134
assert_character_param("auth", auth, len = 1)
21202135
assert_character_param("locations", locations)
2136+
assert_character_param("time_type", time_type, len = 1)
2137+
assert_timeset_param("time_values", time_values)
21212138
assert_timeset_param("dates", dates, required = FALSE)
21222139
assert_timeset_param("epiweeks", epiweeks, required = FALSE)
21232140
dates <- parse_timeset_input(dates)
21242141
epiweeks <- parse_timeset_input(epiweeks)
21252142

2126-
if (!xor(is.null(dates), is.null(epiweeks))) {
2127-
stop("exactly one of `dates` and `epiweeks` is required")
2128-
}
21292143
time_field <- if (!is.null(dates)) {
21302144
create_epidata_field_info("date", "date")
21312145
} else {
@@ -2163,13 +2177,18 @@ pvt_twitter <- function(
21632177
#'
21642178
#' @examples
21652179
#' \dontrun{
2166-
#' pub_wiki(articles = "avian_influenza", epiweeks = epirange(201501, 201601))
2180+
#' pub_wiki(
2181+
#' articles = "avian_influenza",
2182+
#' time_type = "week",
2183+
#' time_values = epirange(201501, 201601)
2184+
#' )
21672185
#' }
21682186
#' @param articles character. Articles to fetch.
21692187
#' @param ... not used for values, forces later arguments to bind by name
2170-
#' @param dates [`timeset`]. Dates to fetch. Mutually exclusive with `epiweeks`.
2171-
#' @param epiweeks [`timeset`]. Epiweeks to fetch. Mutually exclusive with
2172-
#' `dates`.
2188+
#' @param time_type string. The temporal resolution of the data (either "day" or
2189+
#' "week", depending on signal).
2190+
#' @param time_values [`timeset`]. Dates or epiweeks to fetch. Defaults to all
2191+
#' ("*") dates.
21732192
#' @param language string. Language to fetch.
21742193
#' @param hours integer. Optionally, the hours to fetch.
21752194
#' @param fetch_args [`fetch_args`]. Additional arguments to pass to `fetch()`.
@@ -2179,24 +2198,34 @@ pvt_twitter <- function(
21792198
pub_wiki <- function(
21802199
articles,
21812200
...,
2182-
dates = NULL,
2183-
epiweeks = NULL,
2201+
time_type = c("day", "week"),
2202+
time_values = "*",
21842203
hours = NULL,
21852204
language = "en",
21862205
fetch_args = fetch_args_list()) {
21872206
rlang::check_dots_empty()
21882207

2208+
time_type <- match.arg(time_type)
2209+
if (time_type == "day") {
2210+
dates <- time_values
2211+
epiweeks <- NULL
2212+
dates <- get_wildcard_equivalent_dates(dates, "day")
2213+
} else {
2214+
dates <- NULL
2215+
epiweeks <- time_values
2216+
epiweeks <- get_wildcard_equivalent_dates(epiweeks, "week")
2217+
}
2218+
21892219
assert_character_param("articles", articles)
2220+
assert_character_param("time_type", time_type, len = 1)
2221+
assert_timeset_param("time_values", time_values)
21902222
assert_timeset_param("dates", dates, required = FALSE)
21912223
assert_timeset_param("epiweeks", epiweeks, required = FALSE)
21922224
assert_integerish_param("hours", hours, required = FALSE)
21932225
assert_character_param("language", language, len = 1, required = FALSE)
21942226
dates <- parse_timeset_input(dates)
21952227
epiweeks <- parse_timeset_input(epiweeks)
21962228

2197-
if (!xor(is.null(dates), is.null(epiweeks))) {
2198-
stop("exactly one of `dates` and `epiweeks` is required")
2199-
}
22002229
time_field <- if (!is.null(dates)) {
22012230
create_epidata_field_info("date", "date")
22022231
} else {

man/pub_wiki.Rd

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pvt_twitter.Rd

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-endpoints.R

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,27 @@ test_that("basic_epidata_call", {
137137
expect_no_error(pvt_twitter(
138138
auth = "yourkey",
139139
locations = "CA",
140-
epiweeks = epirange(201501, 202001),
140+
time_type = "week",
141+
time_values = epirange(201501, 202001),
142+
fetch_args = fetch_args_list(dry_run = TRUE)
143+
) %>% request_url())
144+
expect_no_error(pvt_twitter(
145+
auth = "yourkey",
146+
locations = "CA",
147+
time_type = "day",
148+
time_values = epirange(20150101, 20200101),
141149
fetch_args = fetch_args_list(dry_run = TRUE)
142150
) %>% request_url())
143151
expect_no_error(pub_wiki(
144152
articles = "avian_influenza",
145-
epiweeks = epirange(201501, 202001),
153+
time_type = "week",
154+
time_values = epirange(201501, 202001),
155+
fetch_args = fetch_args_list(dry_run = TRUE)
156+
) %>% request_url())
157+
expect_no_error(pub_wiki(
158+
articles = "avian_influenza",
159+
time_type = "day",
160+
time_values = epirange(20150101, 20200101),
146161
fetch_args = fetch_args_list(dry_run = TRUE)
147162
) %>% request_url())
148163
})
@@ -322,6 +337,44 @@ test_that("endoints accept wildcard for date parameter", {
322337
))
323338
expect_identical(call$params$epiweeks$from, 100001)
324339
expect_identical(call$params$epiweeks$to, 300001)
340+
341+
expect_no_error(call <- pvt_twitter(
342+
auth = "yourkey",
343+
locations = "CA",
344+
time_type = "week",
345+
time_values = "*",
346+
fetch_args = fetch_args_list(dry_run = TRUE)
347+
))
348+
expect_identical(call$params$epiweeks$from, 100001)
349+
expect_identical(call$params$epiweeks$to, 300001)
350+
351+
expect_no_error(call <- pvt_twitter(
352+
auth = "yourkey",
353+
locations = "CA",
354+
time_type = "day",
355+
time_values = "*",
356+
fetch_args = fetch_args_list(dry_run = TRUE)
357+
))
358+
expect_identical(call$params$dates$from, 10000101)
359+
expect_identical(call$params$dates$to, 30000101)
360+
361+
expect_no_error(call <- pub_wiki(
362+
articles = "avian_influenza",
363+
time_type = "week",
364+
time_values = "*",
365+
fetch_args = fetch_args_list(dry_run = TRUE)
366+
))
367+
expect_identical(call$params$epiweeks$from, 100001)
368+
expect_identical(call$params$epiweeks$to, 300001)
369+
370+
expect_no_error(call <- pub_wiki(
371+
articles = "avian_influenza",
372+
time_type = "day",
373+
time_values = "*",
374+
fetch_args = fetch_args_list(dry_run = TRUE)
375+
))
376+
expect_identical(call$params$dates$from, 10000101)
377+
expect_identical(call$params$dates$to, 30000101)
325378
})
326379

327380
test_that("endpoints fail when given args via dots", {
@@ -413,13 +466,15 @@ test_that("endpoints fail when given args via dots", {
413466
pvt_twitter(
414467
auth = "yourkey",
415468
locations = "CA",
416-
date_range = epirange(201501, 202001)
469+
time_type = "week",
470+
time_range = epirange(201501, 202001)
417471
),
418472
regexp = dots_error
419473
)
420474
expect_error(
421475
pub_wiki(
422476
articles = "avian_influenza",
477+
time_type = "week",
423478
date_range = epirange(201501, 202001)
424479
),
425480
regexp = dots_error

vignettes/signal-discovery.Rmd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,12 @@ pub_paho_dengue(regions = "ca", epiweeks = epirange(200201, 202319))
282282
API docs: <https://cmu-delphi.github.io/delphi-epidata/api/wiki.html>
283283

284284
```{r, eval = FALSE}
285-
pub_wiki(language = "en", articles = "influenza", epiweeks = epirange(202001, 202319))
285+
pub_wiki(
286+
language = "en",
287+
articles = "influenza",
288+
time_type = "week",
289+
time_values = epirange(202001, 202319)
290+
)
286291
```
287292

288293
### Private methods
@@ -373,7 +378,8 @@ API docs: <https://cmu-delphi.github.io/delphi-epidata/api/twitter.html>
373378
pvt_twitter(
374379
auth = Sys.getenv("SECRET_API_AUTH_TWITTER"),
375380
locations = "nat",
376-
epiweeks = epirange(200301, 202105)
381+
time_type = "week",
382+
time_values = epirange(200301, 202105)
377383
)
378384
```
379385

0 commit comments

Comments
 (0)