forked from CoronaNetDataScience/CoronaNetR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f947f3c
Showing
10 changed files
with
485 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,3 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^README\.Rmd$ |
This file contains 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,4 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata |
This file contains 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,20 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: Default | ||
SaveWorkspace: Default | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: UTF-8 | ||
|
||
RnwWeave: Sweave | ||
LaTeX: pdfLaTeX | ||
|
||
AutoAppendNewline: Yes | ||
StripTrailingWhitespace: Yes | ||
|
||
BuildType: Package | ||
PackageUseDevtools: Yes | ||
PackageInstallArgs: --no-multiarch --with-keep.source |
This file contains 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,32 @@ | ||
Package: CoronaNet | ||
Type: Package | ||
Title: API Access to CoronaNet Data | ||
Version: 0.2.0 | ||
Authors@R: | ||
person(given = "Timothy", | ||
family = "A. Model", | ||
role = c("aut", "cre"), | ||
email = "[email protected]") | ||
Maintainer: CoronaNet Research Project <[email protected]> | ||
Description: CoronaNet is a database on government responses to the coronavirus. | ||
To date, this dataset provides the most comprehensive and granular | ||
documentation of such government policies in the world, capturing data for | ||
18 broad policy categories alongside many other dimensions, including the | ||
initiator, target, and timing of a policy. This package offers efficient | ||
and user-friendly R access to the CoronaNet data. | ||
License: MIT | ||
Depends: | ||
R (>= 2.10) | ||
Imports: | ||
httr, | ||
RPostgres, | ||
knitr, | ||
jsonlite | ||
Encoding: UTF-8 | ||
Language: en-US | ||
LazyData: true | ||
URL: https://www.coronanet-project.org/, https://github.com/CoronaNetDataScience/CoronaNet | ||
BugReports: https://github.com/CoronaNetDataScience/CoronaNet/issues | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.1.2 | ||
VignetteBuilder: knitr |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 CoronaNet Data Science | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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 @@ | ||
exportPattern("^[[:alpha:]]+") |
This file contains 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,295 @@ | ||
#' Get CoronaNet Event Data | ||
#' | ||
#' @param countries A character vector of country name(s), e.g., c("Yemen", "Saudi Arabia"). "All" is used as the default. | ||
#' @param type A character vector of policy types, e.g., c("Lockdown", "Curfew"). "All" is used as the default. See https://www.coronanet-project.org/taxonomy.html? for a list of policy types. | ||
#' @param type_sub_cat A character vector of policy types, e.g., c("Self-testing", "Drive-in testing centers"). "All" is used as the default. See https://www.coronanet-project.org/taxonomy.html? for a list of policy subtypes and their related policy types. | ||
#' @param columns A character vector specifying the minimum set of columns of data to retrieve. Defaults to record/policy ID, dates, | ||
#' policy targets, policy type and sub-type, and description | ||
#' @param additional_columns By default NULL. Select additional columns to include with the query. | ||
#' @param from A character vector for the earliest start date, e.g., "2019-12-31". | ||
#' @param to A character vector for the last end date, e.g., "2019-06-01". | ||
#' @param include_no_end_date TRUE/FALSE - whether to include policy records that do not yet have an end date. | ||
#' By default set to false (this is a lot of records). | ||
#' | ||
#' @return A dataframe | ||
#' | ||
#' @examples get_event(countries = "All", type = "All", type_sub_cat = "All", from = "2019-12-31", to = "2027-07-01") | ||
|
||
get_event <- function(countries = "All", | ||
type = "All", | ||
type_sub_cat = "All", | ||
default_columns = c("record_id","policy_id", | ||
"entry_type","update_type", | ||
"update_level","update_level_var", | ||
"date_announced", | ||
"date_start","date_end","date_end_spec", | ||
"country","init_country_level","province", | ||
"city","type","type_sub_cat","description"), | ||
additional_columns = NULL, | ||
from = "2019-12-31", | ||
to = "2027-07-01", | ||
include_no_end_date=FALSE) { | ||
|
||
# Errors/Warnings ---- | ||
|
||
if(length(type) == 1 & | ||
any(type %in% | ||
c("Internal Border Restrictions", "Lockdown", "Anti-Disinformation Measures", "Other Policy Not Listed Above", "Declaration of Emergency")) & | ||
!any(type_sub_cat %in% "All")) { | ||
|
||
stop("ERROR: This policy type has no subtypes; `type_sub_cat` should be specified as `All` with this policy type") | ||
|
||
} | ||
|
||
date_filter <- paste0("date_start=gte.", | ||
from, | ||
"&date_end=lte.", | ||
to) | ||
|
||
columns <- paste0("select=",paste0(default_columns,additional_columns,collapse=",")) | ||
|
||
prod_query <- paste0(c(date_filter,columns), collapse="&") | ||
|
||
cor_query <- GET(paste0("postgrest-1572524110.us-east-2.elb.amazonaws.com/public_release_allvars?", | ||
prod_query), | ||
add_headers(Accept="text/csv")) | ||
|
||
coronanet <- content(cor_query,type="text/csv",encoding="UTF-8") | ||
|
||
# Return all-country coronanet data | ||
return(coronanet) | ||
|
||
# All Countries ---- | ||
|
||
# If all countries... | ||
# if(any(countries %in% "All")) { | ||
# | ||
# # ... and all policy types | ||
# if(any(type %in% "All")) { | ||
# | ||
# if(any(type_sub_cat %in% "All")) { | ||
# | ||
# # Create SQL statement with filtered public_release table | ||
# | ||
# if(include_no_end_date) { | ||
# | ||
# allcountry_call <- paste0("?or=(and(date_start.gte.", | ||
# from, | ||
# ",date_end.lte.", | ||
# to, | ||
# "),date_end.is.null)" | ||
# ) | ||
# | ||
# } else { | ||
# | ||
# | ||
# allcountry_call <- "" | ||
# | ||
# } | ||
# | ||
# | ||
# # Fetch all-country data | ||
# | ||
# | ||
# } | ||
# | ||
# if(!any(type_sub_cat %in% "All")) { | ||
# | ||
# # Create SQL statement with filtered public_release table | ||
# allcountry_call <- paste0("SELECT * FROM public_release WHERE date_start >= '", | ||
# # Insert minimum 'from' date | ||
# from, | ||
# # Insert maximum 'to' date | ||
# "' AND (date_end <= '", | ||
# to, | ||
# "' OR date_end IS NULL)", | ||
# # Insert policy subtype | ||
# "' AND type_sub_cat IN ", | ||
# paste0("(", paste0(sprintf("'%s'", type_sub_cat), collapse = ", "), ")") | ||
# ) | ||
# | ||
# # Fetch all-country data | ||
# coronanet <- DBI::dbGetQuery(public_con, allcountry_call) | ||
# | ||
# # Return all-country coronanet data | ||
# return(coronanet) | ||
# | ||
# } | ||
# | ||
# } | ||
# | ||
# # ... but not all policy types | ||
# if(!any(type %in% "All")){ | ||
# | ||
# if(any(type_sub_cat %in% "All")) { | ||
# | ||
# # Create SQL statement with filtered public_release table | ||
# allcountry_call <- paste0("SELECT * FROM public_release WHERE date_start >= '", | ||
# # Insert minimum 'from' date | ||
# from, | ||
# # Insert maximum 'to' date | ||
# "' AND (date_end <= '", | ||
# to, | ||
# "' OR date_end IS NULL)", | ||
# # Insert policy type(s) | ||
# " AND type IN ", | ||
# paste0("(", paste0(sprintf("'%s'", type), collapse = ", "), ")") | ||
# ) | ||
# | ||
# # Fetch all-country data | ||
# coronanet <- DBI::dbGetQuery(public_con, allcountry_call) | ||
# | ||
# # Return all-country coronanet data | ||
# return(coronanet) | ||
# | ||
# } | ||
# | ||
# if(!any(type_sub_cat %in% "All")) { | ||
# | ||
# # Create SQL statement with filtered public_release table | ||
# allcountry_call <- paste0("SELECT * FROM public_release WHERE date_start >= '", | ||
# # Insert minimum 'from' date | ||
# from, | ||
# # Insert maximum 'to' date | ||
# "' AND (date_end <= '", | ||
# to, | ||
# "' OR date_end IS NULL)", | ||
# # Insert policy type(s) | ||
# " AND type IN ", | ||
# paste0("(", paste0(sprintf("'%s'", type), collapse = ", "), ")"), | ||
# # Insert policy subtype | ||
# "' AND type_sub_cat IN ", | ||
# paste0("(", paste0(sprintf("'%s'", type_sub_cat), collapse = ", "), ")") | ||
# ) | ||
# | ||
# # Fetch all-country data | ||
# coronanet <- DBI::dbGetQuery(public_con, allcountry_call) | ||
# | ||
# # Return all-country coronanet data | ||
# return(coronanet) | ||
# | ||
# } | ||
# | ||
# } | ||
# | ||
# } | ||
# | ||
# # Subsetted Countries ---- | ||
# | ||
# # If not all countries... | ||
# if(!any(countries %in% "All")) { | ||
# | ||
# # ... but all policy types | ||
# if(any(type %in% "All")) { | ||
# | ||
# if(any(type_sub_cat %in% "All")) { | ||
# | ||
# # Create SQL statement with filtered public_release table | ||
# country_call <- paste0("SELECT * FROM public_release WHERE country IN ", | ||
# # Insert country/multiple countries | ||
# paste0("(", paste0(sprintf("'%s'", countries), collapse = ", "), ")"), | ||
# " AND date_start >= '", | ||
# # Insert minimum 'from' date | ||
# from, | ||
# # Insert maximum 'to' date | ||
# "' AND (date_end <= '", | ||
# to, | ||
# "' OR date_end IS NULL)" | ||
# ) | ||
# | ||
# # Fetch single-country data | ||
# coronanet_country <- DBI::dbGetQuery(public_con, country_call) | ||
# | ||
# # Return country data | ||
# return(coronanet_country) | ||
# | ||
# } | ||
# | ||
# if(!any(type_sub_cat %in% "All")) { | ||
# | ||
# # Create SQL statement with filtered public_release table | ||
# country_call <- paste0("SELECT * FROM public_release WHERE country IN ", | ||
# # Insert country/multiple countries | ||
# paste0("(", paste0(sprintf("'%s'", countries), collapse = ", "), ")"), | ||
# " AND date_start >= '", | ||
# # Insert minimum 'from' date | ||
# from, | ||
# # Insert maximum 'to' date | ||
# "' AND (date_end <= '", | ||
# to, | ||
# "' OR date_end IS NULL)", | ||
# # Insert policy subtype | ||
# "' AND type_sub_cat IN ", | ||
# paste0("(", paste0(sprintf("'%s'", type_sub_cat), collapse = ", "), ")") | ||
# ) | ||
# | ||
# # Fetch single-country data | ||
# coronanet_country <- DBI::dbGetQuery(public_con, country_call) | ||
# | ||
# # Return country data | ||
# return(coronanet_country) | ||
# | ||
# } | ||
# | ||
# # ... and not all policy types | ||
# if(!any(type %in% "All")) { | ||
# | ||
# if(any(type_sub_cat %in% "All")) { | ||
# | ||
# # Create SQL statement with filtered public_release table | ||
# country_call <- paste0("SELECT * FROM public_release WHERE country IN ", | ||
# # Insert country or multiple countries | ||
# paste0("(", paste0(sprintf("'%s'", countries), collapse = ", "), ")"), | ||
# " AND date_start >= '", | ||
# # Insert minimum 'from' date | ||
# from, | ||
# # Insert maximum 'to' date | ||
# "' AND (date_end <= '", | ||
# to, | ||
# "' OR date_end IS NULL)", | ||
# # Insert policy type(s) | ||
# " AND type IN ", | ||
# paste0("(", paste0(sprintf("'%s'", type), collapse = ", "), ")") | ||
# ) | ||
# | ||
# # Fetch single-country data | ||
# coronanet_country <- DBI::dbGetQuery(public_con, country_call) | ||
# | ||
# # Return country data | ||
# return(coronanet_country) | ||
# | ||
# } | ||
# | ||
# | ||
# if(!any(type_sub_cat %in% "All")) { | ||
# | ||
# # Create SQL statement with filtered public_release table | ||
# country_call <- paste0("SELECT * FROM public_release WHERE country IN ", | ||
# # Insert country or multiple countries | ||
# paste0("(", paste0(sprintf("'%s'", countries), collapse = ", "), ")"), | ||
# " AND date_start >= '", | ||
# # Insert minimum 'from' date | ||
# from, | ||
# # Insert maximum 'to' date | ||
# "' AND (date_end <= '", | ||
# to, | ||
# "' OR date_end IS NULL)", | ||
# # Insert policy type(s) | ||
# " AND type IN ", | ||
# paste0("(", paste0(sprintf("'%s'", type), collapse = ", "), ")"), | ||
# # Insert policy subtype | ||
# "' AND type_sub_cat IN ", | ||
# paste0("(", paste0(sprintf("'%s'", type_sub_cat), collapse = ", "), ")") | ||
# ) | ||
# | ||
# # Fetch single-country data | ||
# coronanet_country <- DBI::dbGetQuery(public_con, country_call) | ||
# | ||
# # Return country data | ||
# return(coronanet_country) | ||
# | ||
# } | ||
# } | ||
# } | ||
# } | ||
} |
Oops, something went wrong.