-
Notifications
You must be signed in to change notification settings - Fork 5
add covidcast meta wrapper #12
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
196ffee
feat: start with covidcast meta infos
sgratzl 1eaf8e3
feat: covidcast meta api
sgratzl 8fdf14e
feat: data frame on the fly and print
sgratzl 8106e78
fix: vignette title
sgratzl 4b37caf
refactor: change importFrom to where they belong
sgratzl 5a2513a
fix: vignette title
sgratzl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
|
||
parse_signal <- function(signal, base_url) { | ||
class(signal) <- c("covidcast_data_signal", class(signal)) | ||
signal$key <- paste(signal$source, signal$signal, sep = ":") | ||
|
||
#' | ||
#' fetch covidcast data | ||
#' | ||
#' @param data_source data source to fetch | ||
#' @param signals data source to fetch | ||
#' @param time_type data source to fetch | ||
#' @param time_values data source to fetch | ||
#' @param geo_type geo_type to fetch | ||
#' @param geo_values data source to fetch | ||
#' @param as_of data source to fetch | ||
#' @param issues data source to fetch | ||
#' @param lag data source to fetch | ||
#' @return an instance of epidata_call | ||
signal$call <- function(geo_type, | ||
geo_values, | ||
time_values, | ||
...) { | ||
epicall <- covidcast( | ||
signal$source, signal$signal, signal$time_type, geo_type, | ||
time_values, geo_values, ... | ||
) | ||
epicall$base_url <- base_url | ||
epicall | ||
} | ||
r <- list() | ||
r[[signal$signal]] <- signal | ||
r | ||
} | ||
|
||
print.covidcast_data_signal <- function(signal, ...) { | ||
print(signal$name) | ||
print(signal$key) | ||
print(signal$short_description) | ||
} | ||
|
||
parse_source <- function(source, base_url) { | ||
class(source) <- c("covidcast_data_source", class(source)) | ||
signals <- do.call(c, lapply(source$signals, parse_signal, base_url = base_url)) | ||
class(signals) <- c("covidcast_data_signal_list", class(signals)) | ||
source$signals <- signals | ||
r <- list() | ||
r[[source$source]] <- source | ||
r | ||
} | ||
|
||
|
||
as.data.frame.covidcast_data_signal_list <- function(signals, ...) { | ||
as.data.frame(do.call(rbind, lapply(signals, function(x) { | ||
sub <- x[c( | ||
"source", | ||
"signal", | ||
"name", | ||
"active", | ||
"short_description", | ||
"description", | ||
"time_type", | ||
"time_label", | ||
"value_label", | ||
"format", | ||
"category", | ||
"high_values_are", | ||
"is_smoothed", | ||
"is_weighted", | ||
"is_cumulative", | ||
"has_stderr", | ||
"has_sample_size" | ||
)] | ||
sub$geo_types <- paste0(names(x$geo_types), collapse = ",") | ||
sub | ||
})), row.names = sapply(signals, function(x) { | ||
x$key | ||
}), ...) | ||
} | ||
|
||
|
||
print.covidcast_data_source <- function(source, ...) { | ||
print(source$name, ...) | ||
print(source$source, ...) | ||
print(source$description, ...) | ||
signals <- as.data.frame(source$signals) | ||
print(signals[, c("signal", "name", "short_description")], ...) | ||
} | ||
|
||
#' | ||
#' creates the covidcast epidata helper | ||
#' | ||
#' @param base_url optional alternative base url | ||
#' @importFrom httr RETRY stop_for_status content | ||
#' @importFrom jsonlite fromJSON | ||
#' @importFrom rlang abort | ||
#' @return an instance of covidcast_epidata | ||
#' | ||
#' @export | ||
covidcast_epidata <- function(base_url = global_base_url) { | ||
url <- join_url(base_url, "covidcast/meta") | ||
res <- do_request(url, list()) | ||
|
||
httr::stop_for_status(res) | ||
r <- httr::content(res, "text", encoding = "UTF-8") | ||
meta <- jsonlite::fromJSON(r, simplifyVector = FALSE) | ||
|
||
sources <- do.call(c, lapply(meta, parse_source, base_url = base_url)) | ||
class(sources) <- c("covidcast_data_source_list", class(sources)) | ||
|
||
all_signals <- do.call(c, lapply(sources, function(x) { | ||
l <- c(x$signals) | ||
names(l) <- paste(x$source, names(l), sep = ":") | ||
l | ||
})) | ||
class(all_signals) <- c("covidcast_data_signal_list", class(all_signals)) | ||
structure( | ||
list( | ||
sources = sources, | ||
signals = all_signals | ||
), | ||
class = "covidcast_epidata" | ||
) | ||
} | ||
|
||
|
||
as.data.frame.covidcast_data_source_list <- function(sources, ...) { | ||
as.data.frame(do.call(rbind, lapply(sources, function(x) { | ||
sub <- x[c( | ||
"source", "name", "description", "reference_signal", "license" | ||
)] | ||
sub$signals <- paste0(sapply(x$signals, function(y) { | ||
y$signal | ||
}), collapse = ",") | ||
sub | ||
})), row.names = sapply(sources, function(x) { | ||
x$source | ||
}), ...) | ||
} | ||
|
||
print.covidcast_epidata <- function(epidata, ...) { | ||
print("COVIDcast Epidata Fetcher") | ||
print("Sources:") | ||
sources <- as.data.frame(epidata$sources) | ||
print(sources[1:5, c("source", "name")], ...) | ||
if (nrow(sources) > 5) { | ||
print(paste0((nrow(sources) - 5), " more...")) | ||
} | ||
print("Signals") | ||
signals <- as.data.frame(epidata$signals) | ||
print(signals[1:5, c("source", "signal", "name")], ...) | ||
if (nrow(signals) > 5) { | ||
print(paste0((nrow(signals) - 5), " more...")) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
join_url <- function(url, endpoint) { | ||
if (url[length(url)] != "/") { | ||
url <- paste0(url, "/") | ||
} | ||
paste0(url, endpoint) | ||
} | ||
|
||
#' | ||
#' performs the request | ||
#' @importFrom httr RETRY | ||
do_request <- function(url, params) { | ||
sgratzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# don't retry in case of certain status codes | ||
res <- httr::RETRY("GET", url, | ||
query = params, http_headers, | ||
terminate_on = c(400, 401, 403, 405, 414, 500) | ||
) | ||
if (res$status_code == 414) { | ||
res <- httr::RETRY("POST", url, | ||
body = params, encode = "form", http_headers, | ||
terminate_on = c(400, 401, 403, 405, 414, 500) | ||
) | ||
} | ||
res | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
title: "COVIDcast API Client" | ||
author: "Delphi Group" | ||
date: "`r Sys.Date()`" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{COVIDcast API Client} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
\usepackage[utf8]{inputenc} | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
# Delphi Epidata R API Client | ||
|
||
```{r libraries} | ||
# devtools::install_url("https://github.com/cmu-delphi/delphi-epidata-r/releases/latest/download/delphi-epidata.tar.gz") | ||
# install.packages('delphi.epidata') | ||
library('delphi.epidata') | ||
library('magrittr') | ||
``` | ||
|
||
|
||
```{r} | ||
covidcast_api <- covidcast_epidata() | ||
epicall <- covidcast_api$sources$`fb-survey`$signals$smoothed_cli$call("nation", "us", epirange(20210405, 20210410)) | ||
``` | ||
|
||
```{r} | ||
epicall %>% fetch_classic() | ||
``` | ||
|
||
```{r} | ||
epicall %>% fetch_json() | ||
``` | ||
|
||
```{r} | ||
epicall %>% fetch_csv() | ||
``` | ||
|
||
```{r} | ||
epicall %>% fetch_df() | ||
``` | ||
|
||
```{r} | ||
# epicall %>% with_base_url("https://staging.delphi.cmu.edu/epidata/") %>% fetch_df() | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.