├── .Rbuildignore ├── .github ├── CONTRIBUTING.md ├── issue_template.md └── pull_request_template.md ├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── DESCRIPTION ├── LICENSE ├── Makefile ├── NAMESPACE ├── NEWS.md ├── R ├── api_limits.R ├── as_search.R ├── cf_candidate_details.R ├── cf_candidate_leaders.R ├── cf_candidate_new.R ├── cf_candidate_search.R ├── cf_candidate_state.R ├── cg_billscosponsor.R ├── cg_memberappear.R ├── cg_memberbioroles.R ├── cg_memberbystatedistrict.R ├── cg_memberlist.R ├── cg_membersleaving.R ├── cg_membersponsorcompare.R ├── cg_membervotecompare.R ├── cg_membervotepositions.R ├── cg_newmembers.R ├── cg_rollcallvote.R ├── geo_search.R ├── rtimes-package.R └── zzz.R ├── README.Rmd ├── README.md ├── cran-comments.md ├── inst ├── ignore │ └── as_search.R └── vign │ ├── nyt_civil_rights.Rmd │ ├── nyt_civil_rights.md │ ├── rtimes_vignette.Rmd │ └── rtimes_vignette.md ├── man-roxygen ├── articlesearch.R ├── finance.R ├── geographic.R ├── nyt.R ├── nytcgkey.R └── propubkey.R ├── man ├── api_limits.Rd ├── as_search.Rd ├── cf_candidate_details.Rd ├── cf_candidate_leaders.Rd ├── cf_candidate_new.Rd ├── cf_candidate_search.Rd ├── cf_candidate_state.Rd ├── cg_billscosponsor.Rd ├── cg_memberappear.Rd ├── cg_memberbioroles.Rd ├── cg_memberbystatedistrict.Rd ├── cg_membersleaving.Rd ├── cg_memberslist.Rd ├── cg_membersponsorcompare.Rd ├── cg_membervotecompare.Rd ├── cg_membervotepositions.Rd ├── cg_newmembers.Rd ├── cg_rollcallvote.Rd ├── geo_search.Rd └── rtimes-package.Rd ├── rtimes.Rproj ├── tests ├── fixtures │ ├── as_search.yml │ ├── as_search_facet_dates_fl.yml │ ├── as_search_fq.yml │ ├── geo_search.yml │ └── geo_search_params.yml ├── test-all.R └── testthat │ ├── helper-rtimes.R │ ├── test-api_limits.R │ ├── test-as_search.R │ └── test-geo_search.R ├── tools ├── propublica.jpg └── unnamed-chunk-9-1.png └── vignettes ├── figure └── unnamed-chunk-9-1.png ├── nyt_civil_rights.Rmd └── rtimes_vignette.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | vignettes/figure 3 | inst/vign 4 | .travis.yml 5 | .gitignore 6 | man-roxygen/ 7 | ^.*\.Rproj$ 8 | ^\.Rproj\.user$ 9 | cran-comments.md 10 | README.Rmd 11 | ^CODE_OF_CONDUCT\.md$ 12 | ^cran-comments\.md$ 13 | .github 14 | 15 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # CONTRIBUTING # 2 | 3 | ### Bugs? 4 | 5 | * Submit an issue on the [Issues page](https://github.com/ropengov/rtimes/issues) 6 | 7 | ### Issues and Pull Requests 8 | 9 | If you are considering a pull request, you may want to open an issue first to discuss with the maintainer(s). 10 | 11 | ### Code contributions 12 | 13 | * Fork this repo to your GitHub account 14 | * Clone your version on your account down to your machine from your account, e.g,. `git clone https://github.com//rtimes.git` 15 | * Make sure to track progress upstream (i.e., on our version of `rtimes` at `ropengov/rtimes`) by doing `git remote add upstream https://github.com/ropengov/rtimes.git`. Before making changes make sure to pull changes in from upstream by doing either `git fetch upstream` then merge later or `git pull upstream` to fetch and merge in one step 16 | * Make your changes (bonus points for making changes on a new feature branch - see for how to contribute by branching, making changes, then submitting a pull request) 17 | * Push up to your account 18 | * Submit a pull request to home base (likely master branch, but check to make sure) at `ropengov/rtimes` 19 | 20 | ### Prefer to Email? 21 | 22 | Email the person listed as maintainer in the `DESCRIPTION` file of this repo. 23 | 24 | Though note that private discussions over email don't help others - of course email is totally warranted if it's a sensitive problem of any kind. 25 | 26 | ### Thanks for contributing! 27 | -------------------------------------------------------------------------------- /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
Session Info 6 | 7 | ```r 8 | 9 | ``` 10 |
11 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ## Description 8 | 9 | 10 | ## Related Issue 11 | 14 | 15 | ## Example 16 | 18 | 19 | 21 | 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .Rapp.history 3 | .Rproj.user 4 | .Rhistory 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: r 2 | sudo: false 3 | cache: packages 4 | 5 | r_github_packages: 6 | - jimhester/covr 7 | 8 | after_success: 9 | - Rscript -e 'covr::codecov()' 10 | 11 | notifications: 12 | email: 13 | on_success: change 14 | on_failure: change 15 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who 4 | contribute through reporting issues, posting feature requests, updating documentation, 5 | submitting pull requests or patches, and other activities. 6 | 7 | We are committed to making participation in this project a harassment-free experience for 8 | everyone, regardless of level of experience, gender, gender identity and expression, 9 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 10 | 11 | Examples of unacceptable behavior by participants include the use of sexual language or 12 | imagery, derogatory comments or personal attacks, trolling, public or private harassment, 13 | insults, or other unprofessional conduct. 14 | 15 | Project maintainers have the right and responsibility to remove, edit, or reject comments, 16 | commits, code, wiki edits, issues, and other contributions that are not aligned to this 17 | Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed 18 | from the project team. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by 21 | opening an issue or contacting one or more of the project maintainers. 22 | 23 | This Code of Conduct is adapted from the Contributor Covenant 24 | (http:contributor-covenant.org), version 1.0.0, available at 25 | http://contributor-covenant.org/version/1/0/0/ 26 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: rtimes 2 | Title: Client for New York Times 'APIs' 3 | Description: Interface to Congress, Campaign Finance, Article Search, and 4 | Geographic 'APIs' from the New York Times () 5 | and 'ProPublica' (). This client 6 | covers a subset of the New York Times and 'ProPublica' 'APIs'. 7 | Version: 0.5.2.9210 8 | License: MIT + file LICENSE 9 | Authors@R: c(person("Scott", "Chamberlain", role = c("aut", "cre"), 10 | email = "myrmecocystus@gmail.com", comment = c(ORCID="0000-0003-1444-9135")), 11 | person("rOpenSci", role = "fnd", comment = "https://ropensci.org")) 12 | URL: https://github.com/ropengov/rtimes 13 | BugReports: https://github.com/ropengov/rtimes/issues 14 | VignetteBuilder: knitr 15 | Roxygen: list(markdown = TRUE) 16 | Imports: 17 | utils, 18 | crul (>= 0.3.8), 19 | jsonlite, 20 | dplyr, 21 | tibble, 22 | data.table 23 | Suggests: 24 | roxygen2 (>= 6.0.1), 25 | knitr, 26 | testthat, 27 | lubridate, 28 | ggplot2, 29 | scales, 30 | grid, 31 | vcr 32 | RoxygenNote: 6.1.1 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2018 2 | COPYRIGHT HOLDER: Scott Chamberlain 3 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PACKAGE := $(shell grep '^Package:' DESCRIPTION | sed -E 's/^Package:[[:space:]]+//') 2 | RSCRIPT = Rscript --no-init-file 3 | 4 | all: move rmd2md 5 | 6 | move: 7 | cp inst/vign/rtimes_vignette.md vignettes;\ 8 | cp inst/vign/nyt_civil_rights.md vignettes 9 | 10 | rmd2md: 11 | cd vignettes;\ 12 | mv rtimes_vignette.md rtimes_vignette.Rmd;\ 13 | mv nyt_civil_rights.md nyt_civil_rights.Rmd 14 | 15 | test: 16 | ${RSCRIPT} -e 'library(methods); devtools::test()' 17 | 18 | roxygen: 19 | @mkdir -p man 20 | ${RSCRIPT} -e "library(methods); devtools::document()" 21 | 22 | install: 23 | R CMD INSTALL . 24 | 25 | build: 26 | R CMD build . 27 | 28 | check: build 29 | _R_CHECK_CRAN_INCOMING_=FALSE R CMD check --as-cran --no-manual `ls -1tr ${PACKAGE}*gz | tail -n1` 30 | @rm -f `ls -1tr ${PACKAGE}*gz | tail -n1` 31 | @rm -rf ${PACKAGE}.Rcheck 32 | 33 | clean: 34 | rm -f src/*.o src/*.so 35 | 36 | attributes: 37 | ${RSCRIPT} -e 'library(methods); Rcpp::compileAttributes()' 38 | 39 | README.md: README.Rmd 40 | ${RSCRIPT} -e "library(methods); knitr::knit('$<')" 41 | sed -i.bak 's/[[:space:]]*$$//' README.md 42 | rm -f $@.bak 43 | 44 | # No real targets! 45 | .PHONY: all test roxygen install 46 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(api_limits) 4 | export(as_search) 5 | export(cf_candidate_details) 6 | export(cf_candidate_leaders) 7 | export(cf_candidate_new) 8 | export(cf_candidate_state) 9 | export(cg_billscosponsor) 10 | export(cg_memberappear) 11 | export(cg_memberbioroles) 12 | export(cg_memberbystatedistrict) 13 | export(cg_membersleaving) 14 | export(cg_memberslist) 15 | export(cg_membersponsorcompare) 16 | export(cg_membervotecompare) 17 | export(cg_membervotepositions) 18 | export(cg_newmembers) 19 | export(cg_rollcallvote) 20 | export(geo_search) 21 | importFrom(crul,HttpClient) 22 | importFrom(dplyr,bind_rows) 23 | importFrom(jsonlite,fromJSON) 24 | importFrom(utils,setTxtProgressBar) 25 | importFrom(utils,txtProgressBar) 26 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | rtimes 0.5.0 2 | =============== 3 | 4 | ### MINOR IMPROVEMENTS 5 | 6 | * Now using `crul` as the HTTP client under the hood (#16) 7 | * Updated docs with links to new API docs for all 4 APIs (#17) 8 | * Moved to using markdown docs - though all those in templated 9 | `man/` still in old style (#18) 10 | 11 | 12 | 13 | rtimes 0.4.0 14 | =============== 15 | 16 | ### NEW FEATURES 17 | 18 | * `as_search` gains new parameter `all_results` to internally 19 | paginate to get all results. In addition, user can optionally 20 | have the function attempt to flatten completely the output 21 | data.frame, but sometimes may fail so isn't turned on by 22 | default (#15) 23 | 24 | ### MINOR IMPROVEMENTS 25 | 26 | * Now API keys have to be stored as environment variables instead 27 | of options (#12) 28 | * Campaign and Congress APIs moved to Propublica. Updated base URLs, etc 29 | for the move, but many routes still not covered. (#13) 30 | * Replace `dplyr::rbind_all` with `dplyr::bind_rows` (#14) 31 | 32 | 33 | rtimes 0.3.0 34 | =============== 35 | 36 | ### NEW FEATURES 37 | 38 | * released to CRAN 39 | -------------------------------------------------------------------------------- /R/api_limits.R: -------------------------------------------------------------------------------- 1 | #' API limits 2 | #' 3 | #' Check NYT API limits and current usage. 4 | #' 5 | #' @param api Which API to check, either "as" or "geo". 6 | #' @param key Your New York Times API key; pass in, or loads from .Renviron as 7 | #' \code{NYTIMES_AS_KEY}. 8 | #' 9 | #' @details 10 | #' Check the API daily limit and how many calls have already been consumed. 11 | #' Each check consumes one API call by itself. 12 | #' 13 | #' @export 14 | #' @references 15 | #' 16 | #' 17 | #' @examples 18 | #' \dontrun{ 19 | #' api_limits("as") 20 | #' api_limits("geo") 21 | #' } 22 | api_limits <- function(api = "as", key = NULL) { 23 | if (!api %in% c("as", "geo")) { 24 | stop("api_limits() only works for NYT article search and geo APIs ('as', 'geo')") 25 | } 26 | 27 | api_url <- switch(api, 28 | as = "search/v2/articlesearch.json", 29 | geo = "semantic/v2/geocodes/query.json") 30 | api_key <- switch(api, 31 | as = check_key(key, "NYTIMES_API_KEY"), 32 | geo = check_key(key, "NYTIMES_API_KEY")) 33 | 34 | client_url <- paste0(t_base(), api_url) 35 | head_path <- switch(api, 36 | as = sprintf("svc/%s?api-key=%s", api_url, api_key), 37 | geo = sprintf("svc/%s?api-key=%s&country_code=EE", api_url, api_key)) 38 | 39 | cli <- crul::HttpClient$new(url = client_url, opts = list(), headers = list()) 40 | ans <- cli$head(path = head_path) 41 | ans$raise_for_status() 42 | 43 | resp_headers <- ans$response_headers 44 | out <- list( 45 | limit_per_day = as.numeric(resp_headers$`x-ratelimit-limit-day`), 46 | remaining_today = as.numeric(resp_headers$`x-ratelimit-remaining-day`) 47 | ) 48 | out[["used_today"]] <- out[["limit_per_day"]] - out[["remaining_today"]] 49 | out 50 | } 51 | -------------------------------------------------------------------------------- /R/as_search.R: -------------------------------------------------------------------------------- 1 | #' Article search 2 | #' 3 | #' @export 4 | #' @template articlesearch 5 | #' @references 6 | #' , 7 | #' 8 | #' @examples \dontrun{ 9 | #' # basic search - copyright, metadata, data, and facet slots 10 | #' (x <- as_search(q="bailout", begin_date = "20081001", 11 | #' end_date = '20081005', callopts = list(verbose = TRUE))) 12 | #' x$copyright 13 | #' x$meta 14 | #' x$data 15 | #' x$facet 16 | #' Sys.sleep(1) 17 | #' 18 | #' as_search(q="money", fq = 'The New York Times', fl = c('word_count', 19 | #' 'snippet', 'headline')) 20 | #' Sys.sleep(1) 21 | #' x <- as_search(q="bailout", hl = TRUE) 22 | #' x$data$snippet 23 | #' Sys.sleep(1) 24 | #' 25 | #' # all results 26 | #' (x <- as_search(q="bailout", begin_date = "20081001", 27 | #' end_date = '20081003', all_results = TRUE)) 28 | #' x$meta 29 | #' x$data 30 | #' Sys.sleep(1) 31 | #' 32 | #' # facetting 33 | #' as_search(q="bailout", facet_field = 'section_name', begin_date = "20081001", 34 | #' end_date = '20081201') 35 | #' Sys.sleep(1) 36 | #' ## with facet filtering 37 | #' as_search(q="bailout", facet_field = 'section_name', begin_date = "20081001", 38 | #' end_date = '20081201', facet_filter = TRUE) 39 | #' 40 | #' # curl options 41 | #' x <- as_search(q="bailout", begin_date = "20081001", 42 | #' end_date = '20081005', callopts = list(verbose = TRUE)) 43 | #' } 44 | `as_search` <- function( 45 | q, fq=NULL, sort=NULL, begin_date=NULL, end_date=NULL, 46 | key = NULL, fl = NULL, hl = FALSE, page = 0, all_results = FALSE, 47 | try_flatten = FALSE, facet_field = NULL, facet_filter = NULL, 48 | sleep = 2, ..., callopts = list()) { 49 | 50 | if (!is.null(begin_date)) { 51 | if (is.null(end_date)) 52 | end_date = Sys.Date() 53 | if (inherits(begin_date, "POSIXt") ) 54 | begin_date = format(begin_date, "%Y%m%d") 55 | if (inherits(end_date, "POSIXt") ) 56 | end_date = format(end_date, "%Y%m%d") 57 | } 58 | if (!is.null(fl)) fl = paste(fl, collapse = ",") 59 | if (!is.null(facet_field)) { 60 | if (length(facet_field)) { 61 | if (length(facet_field) > 5) stop("Only 5 facets allowed") 62 | facet_field <- paste(facet_field, collapse = ",") 63 | } 64 | } 65 | if (!is.logical(hl)) stop("hl parameter must be logical") 66 | hl <- if (hl) 'true' else NULL 67 | args <- rc(list(q = q, fq = fq, fl = fl, sort = sort, hl = hl, page = page, 68 | begin_date = begin_date, end_date = end_date, 69 | `api-key` = check_key(key, "NYTIMES_API_KEY"), 70 | facet_field = facet_field, facet_filter = facet_filter)) 71 | res <- rtimes_GET(paste0(t_base(), "search/v2/articlesearch.json"), 72 | c(args, ...), TRUE, callopts) 73 | 74 | if (all_results && res$response$meta$hits > 10) { 75 | hits <- res$response$meta$hits 76 | # pgs <- 1:floor(hits/10) 77 | pgs <- seq_len(floor(hits/10)) 78 | out <- list() 79 | pb <- txtProgressBar(min = 0, max = length(pgs), initial = 0, style = 3) 80 | on.exit(close(pb)) 81 | for (i in seq_along(pgs)) { 82 | Sys.sleep(sleep) 83 | setTxtProgressBar(pb, i) 84 | args$page <- pgs[i] 85 | out[[i]] <- rtimes_GET(paste0(t_base(), "search/v2/articlesearch.json"), 86 | c(args, ...), TRUE, callopts) 87 | } 88 | 89 | dat <- bind(lapply(c(list(res), out), function(z) z$response$docs)) 90 | } else { 91 | dat <- res$response$docs 92 | } 93 | 94 | if (try_flatten) { 95 | # completely flatten dataframe 96 | dat$.id <- seq_len(NROW(dat)) 97 | todo <- which(vapply(dat, class, "") == "list") 98 | databin <- list() 99 | for (i in seq_along(todo)) { 100 | tmp <- dat[, todo[[i]]] 101 | clzz <- unique(vapply(tmp, class, character(1))) 102 | if (all("list" %in% clzz)) { 103 | tmp[vapply(tmp, length, 1) == 0] <- NA_character_ 104 | dat[[todo[[i]]]] <- unlist(tmp) 105 | } else if ("data.frame" %in% clzz) { 106 | ncol_ <- vapply(tmp, NCOL, 1) 107 | if (any(ncol_ > 0)) { 108 | col_names <- paste(names(todo)[i], names(tmp[which.max(ncol_)][[1]]), 109 | sep = "_") 110 | z <- lapply(tmp, function(w) { 111 | if (NCOL(w) > 0 && inherits(w, "data.frame")) { 112 | stats::setNames( 113 | w, 114 | paste(names(todo)[i], names(w), sep = "_") 115 | ) 116 | } else { 117 | df <- tibble::as_tibble(t(rep(NA_character_, times = max(ncol_)))) 118 | stats::setNames(df, col_names) 119 | } 120 | }) 121 | zdat <- bind(z, idcol = TRUE) 122 | databin[[i]] <- zdat 123 | } 124 | } else { 125 | databin[[i]] <- todo[[i]] 126 | } 127 | } 128 | 129 | # remove old columns 130 | if (length(databin)) { 131 | for (i in seq_along(todo)) { 132 | if (!is.null(databin[[i]])) { 133 | dat <- merge(dat, databin[[i]], by = ".id") 134 | } 135 | dat[[ names(todo)[i] ]] <- NULL 136 | } 137 | } 138 | dat$.id <- NULL 139 | } 140 | 141 | # tibblize 142 | dat <- tibble::as_tibble(dat) 143 | 144 | list(copyright = cright(), meta = tibble::as_tibble(res$response$meta), 145 | data = dat, facets = res$response$facet) 146 | } 147 | 148 | bind <- function(x, ...) { 149 | (xx <- data.table::setDF( 150 | data.table::rbindlist(x, use.names = TRUE, fill = TRUE, ...) 151 | )) 152 | } 153 | -------------------------------------------------------------------------------- /R/cf_candidate_details.R: -------------------------------------------------------------------------------- 1 | #' Campaign finance - candidate details 2 | #' 3 | #' @export 4 | #' @template finance 5 | #' @template nyt 6 | #' @param fec_id FEC id 7 | #' @references 8 | #' @family campaign-finance 9 | #' @examples \dontrun{ 10 | #' cf_candidate_details(campaign_cycle = 2008, fec_id = 'P80003338') 11 | #' cf_candidate_details(campaign_cycle = 2012, fec_id = 'P80003338') 12 | #' } 13 | `cf_candidate_details` <- function(campaign_cycle=NULL, fec_id=NULL, key=NULL, ...) { 14 | url <- sprintf("%s/%s/candidates/%s.json", cf_base(), campaign_cycle, fec_id) 15 | res <- rtimes_GET(url, list(), FALSE, list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 16 | list(status = res$status, copyright = res$copyright, 17 | data = do_data_frame(res, "other_cycles")) 18 | } 19 | -------------------------------------------------------------------------------- /R/cf_candidate_leaders.R: -------------------------------------------------------------------------------- 1 | #' Campaign finance - candidate leaders 2 | #' 3 | #' @export 4 | #' @template finance 5 | #' @template nyt 6 | #' @param category Category. Specify one of these categories: 7 | #' \itemize{ 8 | #' \item Candidate Loan `candidate-loan` 9 | #' \item Contribution Total `contribution-total` 10 | #' \item Debts Owed `debts-owed` 11 | #' \item Disbursements Total `disbursements-total` 12 | #' \item End Cash `end-cash` 13 | #' \item Individual Total `individual-total` 14 | #' \item PAC Total `pac-total` 15 | #' \item Receipts Total `receipts-total` 16 | #' \item Refund Total `refund-total` 17 | #' } 18 | #' @references 19 | #' @family campaign-finance 20 | #' @examples \dontrun{ 21 | #' cf_candidate_leaders(campaign_cycle = 2010, category='end-cash') 22 | #' cf_candidate_leaders(campaign_cycle = 2008, category='receipts-total') 23 | #' } 24 | 25 | `cf_candidate_leaders` <- function(campaign_cycle=NULL, category=NULL, key=NULL, ...) { 26 | url <- sprintf("%s/%s/candidates/leaders/%s.json", cf_base(), campaign_cycle, category) 27 | res <- rtimes_GET(url, list(), FALSE, list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 28 | df <- to_df(res$results) 29 | list(status = res$status, copyright = res$copyright, meta = NULL, data = df) 30 | } 31 | -------------------------------------------------------------------------------- /R/cf_candidate_new.R: -------------------------------------------------------------------------------- 1 | #' Campaign finance - get new candidates 2 | #' 3 | #' @export 4 | #' @template finance 5 | #' @template nyt 6 | #' @references 7 | #' @family campaign-finance 8 | #' @examples \dontrun{ 9 | #' cf_candidate_new(campaign_cycle = 2016) 10 | #' cf_candidate_new(campaign_cycle = 2015) 11 | #' cf_candidate_new(campaign_cycle = 2014) 12 | #' } 13 | 14 | `cf_candidate_new` <- function(campaign_cycle=NULL, key=NULL, ...) { 15 | url <- sprintf("%s/%s/candidates/new.json", cf_base(), campaign_cycle) 16 | res <- rtimes_GET(url, list(), FALSE, 17 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 18 | dat <- lapply(res$results[[1]]$other_cycles, function(z) { 19 | if (length(z$bill) == 0) z$bill <- NULL 20 | as.list(unlist(z, recursive = TRUE)) 21 | }) 22 | df <- to_df(dat) 23 | list(status = res$status, copyright = res$copyright, 24 | meta = do_data_frame(res, "other_cycles"), data = df) 25 | } 26 | -------------------------------------------------------------------------------- /R/cf_candidate_search.R: -------------------------------------------------------------------------------- 1 | #' Campaign finance - candidate search 2 | #' 3 | #' @keywords internal 4 | #' @template finance 5 | #' @template nyt 6 | #' @param query Last name of a candidate 7 | #' @references 8 | #' @family campaign-finance 9 | #' @examples \dontrun{ 10 | #' cf_candidate_search(campaign_cycle = 2016, query='Wilson') 11 | #' cf_candidate_search(campaign_cycle = 2008, query='obama') 12 | #' } 13 | 14 | `cf_candidate_search` <- function(campaign_cycle=NULL, query=NULL, key = NULL, ...) { 15 | url <- sprintf("%s/%s/candidates/search.json", cf_base(), campaign_cycle) 16 | args <- rc(list(query = query)) 17 | res <- rtimes_GET(url, args, FALSE, list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 18 | tmp <- pop(res, "results") 19 | tmp$data <- dplyr::bind_rows(lapply(res$results, function(x){ 20 | x[sapply(x, is.null)] <- NA 21 | data.frame(x, stringsAsFactors = FALSE) 22 | })) 23 | tmp 24 | } 25 | -------------------------------------------------------------------------------- /R/cf_candidate_state.R: -------------------------------------------------------------------------------- 1 | #' Campaign finance - candidates from a state 2 | #' 3 | #' @export 4 | #' @template finance 5 | #' @template nyt 6 | #' @param state (character) Two-letter state abbreviation. required. 7 | #' @param chamber (character) house or senate. required. 8 | #' @param district (character) Specify the district number. Use 1 for states 9 | #' with a single representative. (House requests only - districts with 10 | #' Senate requests will be ignored.). required. 11 | #' @references 12 | #' @family campaign-finance 13 | #' @examples \dontrun{ 14 | #' cf_candidate_state(campaign_cycle = 2014, state = "TX") 15 | #' cf_candidate_state(campaign_cycle = 2012, state = "NM") 16 | #' cf_candidate_state(campaign_cycle = 2016, state = "OR", chamber = "senate") 17 | #' cf_candidate_state(campaign_cycle = 2016, state = "CA", 18 | #' chamber = "house", district = 2) 19 | #' } 20 | 21 | `cf_candidate_state` <- function(campaign_cycle, state, chamber = NULL, 22 | district = NULL, key = NULL, ...) { 23 | 24 | url <- sprintf("%s/%s/races/%s", cf_base(), campaign_cycle, 25 | state) 26 | if (!is.null(chamber)) url <- file.path(url, chamber) 27 | if (!is.null(district)) { 28 | if (is.null(chamber)) { 29 | stop("if 'district' given, 'chamber' must also be given") 30 | } 31 | url <- file.path(url, district) 32 | } 33 | url <- paste0(url, ".json") 34 | res <- rtimes_GET(url, list(), FALSE, 35 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 36 | dat <- lapply(res$results[[1]]$other_cycles, function(z) { 37 | if (length(z$bill) == 0) z$bill <- NULL 38 | as.list(unlist(z, recursive = TRUE)) 39 | }) 40 | df <- to_df(dat) 41 | list(status = res$status, copyright = res$copyright, 42 | meta = do_data_frame(res, "other_cycles"), data = df) 43 | } 44 | -------------------------------------------------------------------------------- /R/cg_billscosponsor.R: -------------------------------------------------------------------------------- 1 | #' Get bill cosponsorship data for a particular member. 2 | #' 3 | #' @export 4 | #' 5 | #' @template propubkey 6 | #' @param memberid The member's unique ID number (alphanumeric). To find a 7 | #' member's ID number, get the list of members for the appropriate House 8 | #' or Senate. You can also use the Biographical Directory of the United 9 | #' States Congress to get a member's ID. In search results, each member's 10 | #' name is linked to a record by index ID (e.g., 11 | #' http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 12 | #' Use the index ID as member-id in your request. 13 | #' @param type One of 'cosponsored' (the 20 bills most recently cosponsored 14 | #' by member-id) or 'withdrawn' (the 20 most recently withdrawn 15 | #' cosponsorships for member-id). 16 | #' @return List of new members of he current Congress. 17 | #' @references Congress API docs 18 | #' 19 | #' @family congress 20 | #' @examples \dontrun{ 21 | #' cg_billscosponsor(memberid='B001260', type='cosponsored') 22 | #' } 23 | 24 | `cg_billscosponsor` <- function(memberid = NULL, type = NULL, key = NULL, ...) { 25 | url <- sprintf("%s/members/%s/bills/%s.json", cg_base(), memberid, type) 26 | res <- rtimes_GET(url, list(), FALSE, 27 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 28 | res$results[[1]]$bills <- 29 | lapply(res$results[[1]]$bills, rc) 30 | dat <- tibble::as_data_frame(rbind_all_df(res$results[[1]]$bills)) 31 | meta <- tibble::as_data_frame(pop(res$results[[1]], "bills")) 32 | list(copyright = cright(), meta = meta, data = dat) 33 | } 34 | -------------------------------------------------------------------------------- /R/cg_memberappear.R: -------------------------------------------------------------------------------- 1 | #' Get information about a particular member's appearances on the House or 2 | #' Senate floor. 3 | #' 4 | #' @export 5 | #' @template propubkey 6 | #' @param memberid The member's unique ID number (alphanumeric). To find a 7 | #' member's ID number, get the list of members for the appropriate House 8 | #' or Senate. You can also use the Biographical Directory of the United 9 | #' States Congress to get a member's ID. In search results, each member's 10 | #' name is linked to a record by index ID (e.g., 11 | #' http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 12 | #' Use the index ID as member-id in your request. 13 | #' @return Get information about a particular member's appearances on the 14 | #' House or Senate floor. 15 | #' @references Congress API docs 16 | #' 17 | #' @family congress 18 | #' @examples \dontrun{ 19 | #' cg_memberappear(memberid='S001181') 20 | #' } 21 | cg_memberappear <- function(memberid = NULL, key = NULL, ...) { 22 | url <- sprintf("%s/members/%s/floor_appearances.json", cg_base(), memberid) 23 | res <- rtimes_GET(url, list(), FALSE, list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 24 | dat <- tibble::as_data_frame(rbind_all_df(res$results[[1]]$appearances)) 25 | meta <- tibble::as_data_frame(res$results[[1]][c('member_id','name','api_uri','num_results')]) 26 | list(copyright = cright(), meta = meta, data = dat) 27 | } 28 | -------------------------------------------------------------------------------- /R/cg_memberbioroles.R: -------------------------------------------------------------------------------- 1 | #' @title Member bio roles 2 | #' 3 | #' @description Get biographical and Congressional role information for a particular member of Congress. 4 | #' 5 | #' @export 6 | #' @template propubkey 7 | #' @param memberid The member's unique ID number (alphanumeric). To find a 8 | #' member's ID number, get the list of members for the appropriate House 9 | #' or Senate. You can also use the Biographical Directory of the United 10 | #' States Congress to get a member's ID. In search results, each member's 11 | #' name is linked to a record by index ID (e.g., 12 | #' http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 13 | #' Use the index ID as member-id in your request. 14 | #' @return List of members of a particular chamber in a particular Congress. 15 | #' @references Congress API docs 16 | #' 17 | #' @family congress 18 | #' @examples \dontrun{ 19 | #' cg_memberbioroles(memberid = 'S001181') 20 | #' } 21 | `cg_memberbioroles` <- function(memberid = NULL, key = NULL, ...) { 22 | url <- sprintf("%s/members/%s.json", cg_base(), memberid) 23 | res <- rtimes_GET(url, list(), FALSE, 24 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 25 | dat <- lapply(res$results[[1]]$roles, function(z) { 26 | if (length(z$committees) == 0) { 27 | list(meta = tibble::as_data_frame(null_to_na(pop(z, "committees"))), 28 | data = tibble::data_frame()) 29 | } else { 30 | list(meta = tibble::as_data_frame(null_to_na(pop(z, "committees"))), 31 | data = tibble::as_data_frame(rbind_all_df(z$committees))) 32 | } 33 | }) 34 | meta <- pop(res$results[[1]], "roles") 35 | meta <- meta[!duplicated(names(meta))] 36 | meta <- tibble::as_data_frame(meta) 37 | list(copyright = cright(), meta = meta, data = dat) 38 | } 39 | -------------------------------------------------------------------------------- /R/cg_memberbystatedistrict.R: -------------------------------------------------------------------------------- 1 | #' Get a list of the most recent new members of the current Congress. 2 | #' 3 | #' @export 4 | #' @template propubkey 5 | #' @param chamber One of 'house' or 'senate'. 6 | #' @param state Limits the list of members by state; two-letter state code (e.g., CA). 7 | #' @param district Limits the list of members by district (House only). If you specify 8 | #' a district, you must also specify a state. If the district number you 9 | #' specify is higher than the total number of districts for that state, 10 | #' a 404 response will be returned. 11 | #' @return List of new members of he current Congress. 12 | #' @references Congress API docs 13 | #' 14 | #' @family congress 15 | #' @examples \dontrun{ 16 | #' cg_memberbystatedistrict(chamber='senate', state='NH') 17 | #' cg_memberbystatedistrict(chamber='senate', state='CA') 18 | #' cg_memberbystatedistrict(chamber='senate', state='OR') 19 | #' cg_memberbystatedistrict(chamber='house', state='OR', district=1) 20 | #' } 21 | `cg_memberbystatedistrict` <- function(chamber = NULL, state = NULL, 22 | district = NULL, key = NULL, ...) { 23 | url2 <- paste(paste0(cg_base(), "members/"), chamber, '/', state, '/', 24 | district, 'current.json', sep = '') 25 | if (is.null(district)) { 26 | url <- sprintf("%s/members/%s/%s/current.json", cg_base(), chamber, state) 27 | } else { 28 | stopifnot(!is.null(state)) 29 | url <- sprintf("%s/members/%s/%s/%s/current.json", cg_base(), chamber, state, district) 30 | } 31 | res <- rtimes_GET(url, list(), FALSE, 32 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 33 | dat <- tibble::as_data_frame(rbind_all_df(res$results[[1]]$bills)) 34 | meta <- tibble::as_data_frame(pop(res$results[[1]], "bills")) 35 | list(copyright = cright(), meta = meta, data = dat) 36 | } 37 | -------------------------------------------------------------------------------- /R/cg_memberlist.R: -------------------------------------------------------------------------------- 1 | #' Get a list of members of a particular chamber in a particular Congress 2 | #' 3 | #' @export 4 | #' @template propubkey 5 | #' @param congress_no The number of the Congress during which the members served. 6 | #' @param chamber One of 'house' or 'senate. 7 | #' @param state Limits the list of members by state; two-letter state code (e.g., CA). 8 | #' @param district Limits the list of members by district (House only). If you specify 9 | #' a district, you must also specify a state. If the district number you 10 | #' specify is higher than the total number of districts for that state, 11 | #' a 404 response will be returned. 12 | #' @return List of members of a particular chamber in a particular Congress. 13 | #' @references Congress API docs 14 | #' 15 | #' @family congress 16 | #' @examples \dontrun{ 17 | #' cg_memberslist(congress_no = 112, chamber = 'senate', state = "TX") 18 | #' cg_memberslist(112, 'senate', 'NH') 19 | #' cg_memberslist(110, 'senate', 'NH') 20 | #' } 21 | `cg_memberslist` <- function(congress_no = NULL, chamber = NULL, state = NULL, 22 | district = NULL, key = NULL, ...) { 23 | 24 | url <- sprintf("%s/%s/%s/members.json", cg_base(), congress_no, chamber) 25 | args <- rc(list(state = state, district = district)) 26 | res <- rtimes_GET(url, args, FALSE, 27 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 28 | df <- tibble::as_data_frame(to_df(res$results[[1]]$members)) 29 | list(status = res$status, copyright = res$copyright, 30 | meta = do_data_frame(res), data = df) 31 | } 32 | -------------------------------------------------------------------------------- /R/cg_membersleaving.R: -------------------------------------------------------------------------------- 1 | #' Get a list of members who have left the Senate or House or have announced plans to do so. 2 | #' 3 | #' @export 4 | #' @template propubkey 5 | #' @param congress_no The number of the Congress during which the members served. 6 | #' @param chamber One of 'house' or 'senate. 7 | #' @return List of new members of he current Congress. 8 | #' @references Congress API docs 9 | #' 10 | #' @family congress 11 | #' @examples \dontrun{ 12 | #' cg_membersleaving(congress_no = 112, chamber = 'house') 13 | #' } 14 | cg_membersleaving <- function(congress_no = NULL, chamber = NULL, key = NULL, ...) { 15 | url <- sprintf('%s/%s/%s/members/leaving.json', cg_base(), congress_no, chamber) 16 | res <- rtimes_GET(url, list(), FALSE, 17 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 18 | df <- tibble::as_data_frame(to_df(res$results[[1]]$members)) 19 | list(status = res$status, copyright = res$copyright, 20 | meta = do_data_frame(res), data = df) 21 | } 22 | -------------------------------------------------------------------------------- /R/cg_membersponsorcompare.R: -------------------------------------------------------------------------------- 1 | #' Compare bill sponsorship between two members who served in the same Congress 2 | #' and chamber. 3 | #' 4 | #' @export 5 | #' @template propubkey 6 | #' @param memberid_1 The member's unique ID number (alphanumeric). To find a 7 | #' member's ID number, get the list of members for the appropriate House 8 | #' or Senate. You can also use the Biographical Directory of the United 9 | #' States Congress to get a member's ID. In search results, each member's 10 | #' name is linked to a record by index ID (e.g., 11 | #' http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 12 | #' Use the index ID as member-id in your request. 13 | #' @param memberid_2 See description for memberid_1. 14 | #' @param congress_no The number of the Congress during which the members served. 15 | #' @param chamber One of 'house' or 'senate. 16 | #' @return Compare bill sponsorship between two members who served in the same 17 | #' Congress and chamber. 18 | #' @references Congress API docs 19 | #' 20 | #' @family congress 21 | #' @examples \dontrun{ 22 | #' cg_membersponsorcompare(memberid_1 = 'S001181', memberid_2 = 'A000368', 23 | #' congress_no = 112, chamber = 'senate') 24 | #' } 25 | cg_membersponsorcompare <- function(memberid_1 = NULL, memberid_2 = NULL, 26 | congress_no = NULL, chamber = NULL, key = NULL, ...) { 27 | 28 | url <- sprintf('%s/members/%s/bills/%s/%s/%s.json', cg_base(), memberid_1, memberid_2, 29 | congress_no, chamber) 30 | res <- rtimes_GET(url, list(), FALSE, 31 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 32 | df <- tibble::as_data_frame(to_df(res$results[[1]]$bills)) 33 | list(status = res$status, copyright = res$copyright, 34 | meta = do_data_frame(res, "bills"), data = df) 35 | } 36 | -------------------------------------------------------------------------------- /R/cg_membervotecompare.R: -------------------------------------------------------------------------------- 1 | #' Get a list of members who have left the Senate or House or have announced plans to do so. 2 | #' 3 | #' @export 4 | #' @template propubkey 5 | #' @param memberid_1,memberid_2 The member's unique ID number (alphanumeric). To find a 6 | #' member's ID number, get the list of members for the appropriate House 7 | #' or Senate. You can also use the Biographical Directory of the United 8 | #' States Congress to get a member's ID. In search results, each member's 9 | #' name is linked to a record by index ID (e.g., 10 | #' http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 11 | #' Use the index ID as member-id in your request. 12 | #' @param congress_no The number of the Congress during which the members served. 13 | #' @param chamber One of 'house' or 'senate. 14 | #' @return List of new members of he current Congress. 15 | #' @references Congress API docs 16 | #' 17 | #' @family congress 18 | #' @examples \dontrun{ 19 | #' cg_membervotecompare(memberid_1 = 'S001181', memberid_2 = 'A000368', 20 | #' congress_no = 112, chamber = 'senate') 21 | #' } 22 | cg_membervotecompare <- function(memberid_1 = NULL, memberid_2 = NULL, 23 | congress_no = NULL, chamber = NULL, key = NULL, ...) { 24 | 25 | url <- sprintf('%s/members/%s/votes/%s/%s/%s.json', cg_base(), memberid_1, 26 | memberid_2, congress_no, chamber) 27 | res <- rtimes_GET(url, list(), FALSE, 28 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 29 | df <- tibble::as_data_frame(res$results[[1]]) 30 | list(status = res$status, copyright = res$copyright, meta = NULL, data = df) 31 | } 32 | -------------------------------------------------------------------------------- /R/cg_membervotepositions.R: -------------------------------------------------------------------------------- 1 | #' Get a list of members who have left the Senate or House or have announced 2 | #' plans to do so. 3 | #' 4 | #' @export 5 | #' @template propubkey 6 | #' @param memberid The member's unique ID number (alphanumeric). To find a 7 | #' member's ID number, get the list of members for the appropriate House 8 | #' or Senate. You can also use the Biographical Directory of the United 9 | #' States Congress to get a member's ID. In search results, each member's 10 | #' name is linked to a record by index ID (e.g., 11 | #' http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 12 | #' Use the index ID as member-id in your request. 13 | #' @return List of new members of he current Congress. 14 | #' @references Congress API docs 15 | #' 16 | #' @family congress 17 | #' @examples \dontrun{ 18 | #' cg_membervotepositions('S001181') 19 | #' } 20 | cg_membervotepositions <- function(memberid = NULL, key = NULL, ...) { 21 | url <- sprintf("%s/members/%s/votes.json", cg_base(), memberid) 22 | res <- rtimes_GET(url, list(), FALSE, 23 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 24 | dat <- lapply(res$results[[1]]$votes, function(z) { 25 | if (length(z$bill) == 0) z$bill <- NULL 26 | as.list(unlist(z, recursive = TRUE)) 27 | }) 28 | df <- tibble::as_data_frame(to_df(dat)) 29 | list(status = res$status, copyright = res$copyright, 30 | meta = do_data_frame(res, "votes"), data = df) 31 | } 32 | -------------------------------------------------------------------------------- /R/cg_newmembers.R: -------------------------------------------------------------------------------- 1 | #' Get a list of the most recent new members of the current Congress. 2 | #' 3 | #' @export 4 | #' @template propubkey 5 | #' @return List of new members of he current Congress. 6 | #' @references Congress API docs 7 | #' 8 | #' @family congress 9 | #' @examples \dontrun{ 10 | #' cg_newmembers() 11 | #' } 12 | cg_newmembers <- function(key = NULL, ...) { 13 | url <- file.path(cg_base(), "members/new.json") 14 | res <- rtimes_GET(url, list(), FALSE, 15 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 16 | df <- tibble::as_data_frame(to_df(res$results[[1]]$members)) 17 | list(status = res$status, copyright = res$copyright, 18 | meta = do_data_frame(res), data = df) 19 | } 20 | -------------------------------------------------------------------------------- /R/cg_rollcallvote.R: -------------------------------------------------------------------------------- 1 | #' Get a specific roll-call vote, including a complete list of member positions. 2 | #' 3 | #' @export 4 | #' @template propubkey 5 | #' @param congress_no The number of the Congress during which the members served. 6 | #' @param chamber One of 'house' or 'senate. 7 | #' @param session_no 1, 2, or special session number (For a detailed list of 8 | #' Congressional sessions, see 9 | #' http://www.senate.gov/reference/resources/pdf/congresses2.pdf). 10 | #' @param rollcall_no Integer. To get roll-call numbers, see the official sites 11 | #' of the US Senate 12 | #' (http://www.senate.gov/pagelayout/legislative/a_three_sections_with_teasers/votes.htm), 13 | #' and US House (http://artandhistory.house.gov/house_history/index.aspx). 14 | #' @return Get a specific roll-call vote, including a complete list of member 15 | #' positions. A list with metadata about the bill, and vote results. 16 | #' @references Congress API docs 17 | #' 18 | #' @family congress 19 | #' @examples \dontrun{ 20 | #' cg_rollcallvote(congress_no = 105, chamber = 'house', session_no = 2, 21 | #' rollcall_no = 38) 22 | #' } 23 | cg_rollcallvote <- function(congress_no = NULL, chamber = NULL, session_no = NULL, 24 | rollcall_no = NULL, key = NULL, ...) { 25 | 26 | url <- sprintf('%s/%s/%s/sessions/%s/votes/%s.json', cg_base(), congress_no, 27 | chamber, session_no, rollcall_no) 28 | res <- rtimes_GET(url, list(), FALSE, 29 | list(...), add_key(check_key(key, "PROPUBLICA_API_KEY"))) 30 | res$results$votes$vote$positions <- 31 | lapply(res$results$votes$vote$positions, rc) 32 | dat <- tibble::as_data_frame(rbind_all_df(res$results$votes$vote$positions)) 33 | meta <- tibble::as_data_frame(res$results$votes$vote[c('congress', 'session', 34 | 'chamber', 'roll_call', 35 | 'question', 'description', 'vote_type', 'date', 'time', 'result')]) 36 | votes <- data.table::setDF(data.table::rbindlist( 37 | res$results$votes$vote[c('democratic', 'republican', 38 | 'independent', 'total')], use.names = TRUE, 39 | fill = TRUE, idcol = "category")) 40 | list(copyright = cright(), 41 | bill_info = res$results$votes$vote$bill, 42 | meta = meta, 43 | votes = votes) 44 | } 45 | -------------------------------------------------------------------------------- /R/geo_search.R: -------------------------------------------------------------------------------- 1 | #' Geographic search NYTimes API 2 | #' 3 | #' @export 4 | #' @template geographic 5 | #' @param key your New York Times API key; pass in, or loads from .Rprofile as 6 | #' `nytimes_api_key`, or from .Renviron as `NYTIMES_API_KEY` 7 | #' @param ... Curl options (debugging tools mostly) passed to 8 | #' [crul::HttpClient] 9 | #' @references , 10 | #' 11 | #' @details BEWARE: the docs are a hot mess - the README page has examples that 12 | #' include parameters that are not in their list of accepted query 13 | #' parameters. Some query parameter that used to work don't work now. There 14 | #' seems to be no way to get a response from them. So good luck. 15 | #' @examples \dontrun{ 16 | #' geo_search(country_code = 'US', verbose = TRUE) 17 | #' geo_search(feature_class='P', country_code='US', population='50000_') 18 | #' 19 | #' # curl options 20 | #' geo_search(country_code = 'US', verbose = TRUE) 21 | #' } 22 | 23 | `geo_search` <- function(name = NULL, latitude = NULL, longitude = NULL, elevation = NULL, 24 | population = NULL, country_code = NULL, country_name = NULL, admin_code1 = NULL, 25 | admin_name1 = NULL, admin_code2 = NULL, admin_name2 = NULL, admin_code3 = NULL, 26 | admin_name3 = NULL, admin_code4 = NULL, admin_name4 = NULL, feature_class = NULL, 27 | feature_class_name = NULL, feature_code_name = NULL, time_zone_id = NULL, dst_offset = NULL, 28 | gmt_offset = NULL, bounding_box = NULL, nearby = NULL, offset = NULL, limit=100, key=NULL, ...) { 29 | 30 | nearby <- nnlcol(nearby) 31 | bounding_box <- nnlcol(bounding_box) 32 | args <- rc(list(name=name,latitude=latitude,longitude=longitude,elevation=elevation, 33 | population=population,country_code=country_code,country_name=country_name, 34 | admin_code1=admin_code1,admin_name1=admin_name1, 35 | admin_code2=admin_code2,admin_name2=admin_name2,admin_code3=admin_code3, 36 | admin_name3=admin_name3,admin_code4=admin_code4, 37 | admin_name4=admin_name4,feature_class=feature_class,feature_class_name=feature_class_name, 38 | feature_code_name=feature_code_name, 39 | time_zone_id=time_zone_id,dst_offset=dst_offset,gmt_offset=gmt_offset, 40 | bounding_box=bounding_box,nearby=nearby, 41 | offset=offset, perpage=limit, `api-key`=check_key(key))) 42 | 43 | res <- rtimes_GET(paste0(t_base(), "semantic/v2/geocodes/query.json"), args, 44 | TRUE, list(...)) 45 | list( 46 | copyright = cright(), 47 | meta = meta(res), 48 | data = tibble::as_data_frame(res$results) 49 | ) 50 | } 51 | -------------------------------------------------------------------------------- /R/rtimes-package.R: -------------------------------------------------------------------------------- 1 | #' rtimes 2 | #' 3 | #' Interface to the Congress and Campaign Finance APIs from Propublica, and 4 | #' the Article Search and Geographic 'APIs' from the New York Times. 5 | #' 6 | #' Backstory is that the Congress and Campaign finance APIs used to be part of 7 | #' NYTimes, but were taken over by Propublica. 8 | #' 9 | #' @section rtimes API: 10 | #' Functions that wrap these sets of APIs are prefixed by two letter 11 | #' acronyms fo reach API endpoint + the function name itself as for example 12 | #' `cg` + `fxn` 13 | #' 14 | #' \itemize{ 15 | #' \item as - for the Article Search API (Docs: 16 | #' ) 17 | #' \item geo - for the Geographic API (Docs: 18 | #' ) 19 | #' \item cg - for the Congress API (Docs: 20 | #' ) 21 | #' \item cf - for the Campaign Finance API (Docs: 22 | #' ) 23 | #' } 24 | #' 25 | #' See the vignette for help. 26 | #' 27 | #' @section Authentication: 28 | #' Get your own API keys for NYTimes APIs at 29 | #' - you can use one API key for 30 | #' both the article search and geo NYTimes APIs. 31 | #' 32 | #' Get your Propublica API key for Congress and Campaign Finance APIs at either 33 | #' or 34 | #' - as far 35 | #' as I know, you can use the same key for both APIs 36 | #' 37 | #' We set up the functions so that you can put the key in your `.Renviron` 38 | #' file, which will be called on startup of R, and then you don't have to enter 39 | #' your API key for each run of a function. Add entries for an R session like 40 | #' 41 | #' \itemize{ 42 | #' \item `Sys.setenv(NYTIMES_API_KEY = "YOURKEYHERE")` 43 | #' \item `Sys.setenv(PROPUBLICA_API_KEY = "YOURKEYHERE")` 44 | #' } 45 | #' 46 | #' Or set them across sessions by putting entries in your `.Renviron` file 47 | #' like 48 | #' 49 | #' \itemize{ 50 | #' \item `NYTIMES_API_KEY=` 51 | #' \item `PROPUBLICA_API_KEY=` 52 | #' } 53 | #' 54 | #' You can also pass in your key in a function call, but be careful not to 55 | #' expose your keys in code committed to public repositories. If you do pass 56 | #' in a function call, use e.g., `Sys.getenv("NYTIMES_API_KEY")` 57 | #' 58 | #' @section Rate limits: 59 | #' Rate limits vary for the different APIs: 60 | #' 61 | #' \itemize{ 62 | #' \item Article Search API: 1/sec, 1,000/day 63 | #' \item Geographic API: 5/sec, 1,000/day 64 | #' \item Congress API: 2/sec, 5,000/day 65 | #' \item Campaign Finance API: 50/sec, 5,000/day 66 | #' } 67 | #' 68 | #' @importFrom crul HttpClient 69 | #' @importFrom jsonlite fromJSON 70 | #' @importFrom dplyr bind_rows 71 | #' @importFrom utils setTxtProgressBar txtProgressBar 72 | #' @name rtimes-package 73 | #' @aliases rtimes 74 | #' @docType package 75 | #' @author Scott Chamberlain \email{myrmecocystus@@gmail.com} 76 | #' @keywords package 77 | NULL 78 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | rc <- function(x) Filter(Negate(is.null), x) 2 | 3 | nnlcol <- function(x) if (!is.null(x)) paste(x, collapse = ",") else NULL 4 | nnlna <- function(x) if (!is.null(x)) paste(x, collapse = ",") else NA 5 | 6 | t_base <- function() "https://api.nytimes.com/svc/" 7 | 8 | p_base <- function() "https://api.propublica.org/" 9 | cf_base <- function(version = "v1") { 10 | paste0(p_base(), "campaign-finance/", version) 11 | } 12 | cg_base <- function(version = "v1") paste0(p_base(), "congress/", version) 13 | add_key <- function(x) list(`X-API-Key` = x) 14 | 15 | rtimes_GET <- function(url, args, parse = TRUE, curlopts = list(), 16 | headers = list()) { 17 | cli <- crul::HttpClient$new(url = url, opts = curlopts, headers = headers) 18 | ans <- cli$get(query = args) 19 | ans$raise_for_status() 20 | if (parse) return(jsonlite::fromJSON(ans$parse("UTF-8"), TRUE, 21 | flatten = TRUE)) 22 | jsonlite::fromJSON(ans$parse("UTF-8"), FALSE) 23 | } 24 | 25 | meta <- function(x){ 26 | data.frame(pop(x, c("results", "copyright")), stringsAsFactors = FALSE) 27 | } 28 | 29 | as_meta <- function(x){ 30 | tibble::as_data_frame(x$response$meta) 31 | } 32 | 33 | cright <- function() 34 | "Copyright (c) 2015 The New York Times Company. All Rights Reserved." 35 | 36 | pluck <- function(x, name, type) { 37 | if (missing(type)) { 38 | lapply(x, "[[", name) 39 | } else { 40 | vapply(x, "[[", name, FUN.VALUE = type) 41 | } 42 | } 43 | 44 | pop <- function(x, namez) { 45 | getnames <- names(x)[!names(x) %in% namez] 46 | x[ getnames ] 47 | } 48 | 49 | check_key <- function(x, y = "NYTIMES_API_KEY"){ 50 | tmp <- if (is.null(x)) Sys.getenv(y, "") else x 51 | if (tmp == "") stop("need an API key for ", y, call. = FALSE) else tmp 52 | } 53 | 54 | do_data_frame <- function(x, z = "members") { 55 | tmp <- pop(x$results[[1]], z) 56 | tmp <- as.list(unlist(tmp, TRUE)) 57 | tibble::as_data_frame(tmp) 58 | } 59 | 60 | rbind_all_df <- function(x) { 61 | tmpdf <- dplyr::bind_rows(lapply(x, data.frame, stringsAsFactors = FALSE)) 62 | tmpdf$label <- names(x) 63 | tmpdf 64 | } 65 | 66 | to_df <- function(x) { 67 | dplyr::bind_rows(lapply(x, function(x) { 68 | x[sapply(x, is.null)] <- NA 69 | data.frame(x, stringsAsFactors = FALSE) 70 | })) 71 | } 72 | 73 | null_to_na <- function(y) { 74 | y[sapply(y, is.null)] <- NA 75 | y 76 | } 77 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | rtimes 2 | ====== 3 | 4 | ```{r echo=FALSE} 5 | knitr::opts_chunk$set( 6 | comment = "#>", 7 | collapse = TRUE, 8 | warning = FALSE, 9 | message = FALSE 10 | ) 11 | ``` 12 | 13 | [![Project Status: Abandoned – the project has been abandoned and the author(s) do not intend on continuing development.](https://www.repostatus.org/badges/latest/abandoned.svg)](https://www.repostatus.org/#abandoned) 14 | 15 | `rtimes` has been abandoned - get in touch if you want to take over maintainance. 16 | 17 | ## Meta 18 | 19 | * Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms. 20 | * Maintainer: Scott Chamberlain 21 | * License: MIT 22 | * Report any problems in the [Issues Tracker](https://github.com/ropengov/rtimes/issues), or just fork and submit changes, etc. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rtimes 2 | ====== 3 | 4 | [![Project Status: Abandoned – the project has been abandoned and the author(s) do not intend on continuing development.](https://www.repostatus.org/badges/latest/abandoned.svg)](https://www.repostatus.org/#abandoned) 5 | 6 | `rtimes` has been abandoned - get in touch if you want to take over maintainance. 7 | 8 | ## Meta 9 | 10 | * Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms. 11 | * Maintainer: Scott Chamberlain 12 | * License: MIT 13 | * Report any problems in the [Issues Tracker](https://github.com/ropengov/rtimes/issues), or just fork and submit changes, etc. 14 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Test environments 2 | 3 | * local OS X install, R 3.4.1 4 | * ubuntu 12.04 (on travis-ci), R 3.4.1 5 | * win-builder (devel and release) 6 | 7 | ## R CMD check results 8 | 9 | 0 errors | 0 warnings | 1 note 10 | 11 | ## Reverse dependencies 12 | 13 | There are no reverse dependencies. 14 | 15 | --- 16 | 17 | This version implements a few minor improvements. 18 | 19 | Thanks! 20 | Scott Chamberlain 21 | -------------------------------------------------------------------------------- /inst/ignore/as_search.R: -------------------------------------------------------------------------------- 1 | #' Search articles 2 | #' 3 | #' @export 4 | #' @template articlesearch 5 | #' @references 6 | #' \url{http://developer.nytimes.com/docs/read/article_search_api_v2} 7 | #' @examples \dontrun{ 8 | #' as_search(q="bailout", begin_date = "20081001", end_date = '20081201') 9 | #' as_search(q="bailout", facet_field = 'section_name', begin_date = "20081001", 10 | #' end_date = '20081201', fl = 'word_count') 11 | #' as_search(q="money", fq = 'The New York Times') 12 | #' as_search(q="money", fq = 'news_desk:("Sports" "Foreign")') 13 | #' as_search(q="bailout", hl = TRUE) 14 | #' 15 | #' library('httr') 16 | #' as_search("iowa caucus", callopts = verbose()) 17 | #' } 18 | `as_search` <- function(q, fq=NULL, sort=NULL, begin_date=NULL, end_date=NULL, 19 | key = NULL, fl = NULL, hl = FALSE, 20 | page = 0, facet_field = NULL, facet_filter = NULL, ..., callopts=list()) { 21 | 22 | if(!is.null(begin_date)) { 23 | if(is.null(end_date)) 24 | end_date = Sys.Date() 25 | if(inherits(begin_date, "POSIXt") ) 26 | begin_date = format(begin_date, "%Y%m%d") 27 | if(inherits(end_date, "POSIXt") ) 28 | end_date = format(end_date, "%Y%m%d") 29 | } 30 | if(!is.null(fl)) fl = paste(fl, collapse = ",") 31 | if(!is.null(facet_field)){ 32 | if(length(facet_field)) { 33 | if(length(facet_field) > 5) 34 | stop("Only 5 facets allowed") 35 | facet_field = paste(facet_field, collapse = ",") 36 | } 37 | } 38 | if(!is.logical(hl)) stop("hl parameter must be logical") 39 | if(hl){ hl = 'true' } else { hl = NULL } 40 | args <- rc(list(q=q, fl=fl, fq=fq, sort=sort, hl=hl, page=page, 41 | begin_date=begin_date, end_date=end_date, 42 | `api-key` = check_key(key, "NYTIMES_AS_KEY"), 43 | facet_field=facet_field, facet_filter=facet_filter)) 44 | res <- rtimes_GET(paste0(t_base(), "search/v2/articlesearch.json"), c(args, ...), FALSE, callopts, ...) 45 | dat <- if(is.null(fl)) lapply(res$response$docs, structure, class="as_search") else res$response 46 | list(copyright=cright(), meta=as_meta(res), data=dat) 47 | } 48 | 49 | #' @export 50 | print.as_search <- function(x, ...){ 51 | cat("", x$headline$main, "\n", sep = "") 52 | cat(" Type: ", x$type_of_material, "\n", sep = "") 53 | cat(" Published: ", x$pub_date, "\n", sep = "") 54 | cat(" Word count: ", x$word_count, "\n", sep = "") 55 | cat(" URL: ", x$web_url, "\n", sep = "") 56 | cat(" Snippet: ", x$snippet, "\n", sep = "") 57 | } 58 | -------------------------------------------------------------------------------- /inst/vign/nyt_civil_rights.Rmd: -------------------------------------------------------------------------------- 1 | 6 | 7 | ```{r echo=FALSE} 8 | knitr::opts_chunk$set( 9 | comment = "#>", 10 | collapse = TRUE, 11 | warning = FALSE, 12 | message = FALSE, 13 | fig.path = "../../tools/" 14 | ) 15 | ``` 16 | 17 | Use case - Civil Rights Coverage in NYT over time 18 | ================================================= 19 | 20 | This is an example use of `rtimes` from [Gaurav Soodoku](http://gbytes.gsood.com/). Find the original code [at GitHub](https://github.com/soodoku/nyt-civil-rights). 21 | 22 | This example is a smaller subset of the analysis in the original code, just looking at one phrase: _Affirmative Action_ 23 | 24 | ## Installation 25 | 26 | ```{r install, eval=FALSE} 27 | install.packages("rtimes") 28 | ``` 29 | 30 | ```{r} 31 | library("rtimes") 32 | ``` 33 | 34 | ## Authentication 35 | 36 | First, get an API key for the Article Search API at [developer.nytimes.com/apps/register](http://developer.nytimes.com/apps/register). Then set the key in R: 37 | 38 | ```{r eval=FALSE} 39 | Sys.setenv(NYTIMES_AS_KEY = '') 40 | ``` 41 | 42 | ## Query the NYTimes Article Search API 43 | 44 | Set dates. Here, one query per year from 1881 to 1985 45 | 46 | ```{r} 47 | dates <- format(seq(as.Date("1880/1/1"), as.Date("2000/1/1"), by = "5 years"), "%Y%m%d") 48 | ``` 49 | 50 | Make a data.frame to put data in to 51 | 52 | ```{r} 53 | results <- data.frame(startDate = NA, endDate = NA, afam = NA) 54 | ``` 55 | 56 | Loop through dates 57 | 58 | ```{r cache=FALSE} 59 | for (i in seq_len(length(dates) - 1)) { 60 | Sys.sleep(1) 61 | counts <- NA 62 | counts <- as_search(q = "affirmative action", 63 | begin_date = dates[i], 64 | end_date = dates[i + 1])$meta$hits[1] 65 | results[i, ] <- c(dates[i], dates[i + 1], counts) 66 | } 67 | ``` 68 | 69 | ## Plot results 70 | 71 | Load packages 72 | 73 | ```{r} 74 | library("lubridate") 75 | library("ggplot2") 76 | library("scales") 77 | library("grid") 78 | ``` 79 | 80 | Convert dates to class `date` 81 | 82 | ```{r} 83 | results$startDate <- as.Date(as.character(results$startDate), format = "%Y%m%d") 84 | results$afam <- as.numeric(results$afam) 85 | ``` 86 | 87 | Plot mentions of _Affirmative Action_ 88 | 89 | ```{r fig.width=10, tidy=FALSE} 90 | ggplot(results, aes(x = startDate, y = afam)) + 91 | geom_point(size = 1, color = "#42C4C7") + 92 | geom_smooth(method = "gam", formula = y ~ s(x), aes(group = 1), 93 | size = 1, colour = "#777777", alpha = 0.05, se = FALSE) + 94 | ylab("No. Articles Containing the phrase 'Affirmative Action'") + 95 | xlab("Year") + 96 | scale_x_date(breaks = pretty_breaks(n = 10)) + 97 | theme_grey(base_size = 18) 98 | ``` 99 | -------------------------------------------------------------------------------- /inst/vign/nyt_civil_rights.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | Use case - Civil Rights Coverage in NYT over time 10 | ================================================= 11 | 12 | This is an example use of `rtimes` from [Gaurav Soodoku](http://gbytes.gsood.com/). Find the original code [at GitHub](https://github.com/soodoku/nyt-civil-rights). 13 | 14 | This example is a smaller subset of the analysis in the original code, just looking at one phrase: _Affirmative Action_ 15 | 16 | ## Installation 17 | 18 | 19 | ```r 20 | install.packages("rtimes") 21 | ``` 22 | 23 | 24 | ```r 25 | library("rtimes") 26 | ``` 27 | 28 | ## Authentication 29 | 30 | First, get an API key for the Article Search API at [developer.nytimes.com/apps/register](http://developer.nytimes.com/apps/register). Then set the key in R: 31 | 32 | 33 | ```r 34 | Sys.setenv(NYTIMES_AS_KEY = '') 35 | ``` 36 | 37 | ## Query the NYTimes Article Search API 38 | 39 | Set dates. Here, one query per year from 1881 to 1985 40 | 41 | 42 | ```r 43 | dates <- format(seq(as.Date("1880/1/1"), as.Date("2000/1/1"), by = "5 years"), "%Y%m%d") 44 | ``` 45 | 46 | Make a data.frame to put data in to 47 | 48 | 49 | ```r 50 | results <- data.frame(startDate = NA, endDate = NA, afam = NA) 51 | ``` 52 | 53 | Loop through dates 54 | 55 | 56 | ```r 57 | for (i in seq_len(length(dates) - 1)) { 58 | Sys.sleep(1) 59 | counts <- NA 60 | counts <- as_search(q = "affirmative action", 61 | begin_date = dates[i], 62 | end_date = dates[i + 1])$meta$hits[1] 63 | results[i, ] <- c(dates[i], dates[i + 1], counts) 64 | } 65 | ``` 66 | 67 | ## Plot results 68 | 69 | Load packages 70 | 71 | 72 | ```r 73 | library("lubridate") 74 | library("ggplot2") 75 | library("scales") 76 | library("grid") 77 | ``` 78 | 79 | Convert dates to class `date` 80 | 81 | 82 | ```r 83 | results$startDate <- as.Date(as.character(results$startDate), format = "%Y%m%d") 84 | results$afam <- as.numeric(results$afam) 85 | ``` 86 | 87 | Plot mentions of _Affirmative Action_ 88 | 89 | 90 | ```r 91 | ggplot(results, aes(x = startDate, y = afam)) + 92 | geom_point(size = 1, color = "#42C4C7") + 93 | geom_smooth(method = "gam", formula = y ~ s(x), aes(group = 1), 94 | size = 1, colour = "#777777", alpha = 0.05, se = FALSE) + 95 | ylab("No. Articles Containing the phrase 'Affirmative Action'") + 96 | xlab("Year") + 97 | scale_x_date(breaks = pretty_breaks(n = 10)) + 98 | theme_grey(base_size = 18) 99 | ``` 100 | 101 | ![plot of chunk unnamed-chunk-9](../../tools/unnamed-chunk-9-1.png) 102 | -------------------------------------------------------------------------------- /inst/vign/rtimes_vignette.Rmd: -------------------------------------------------------------------------------- 1 | 6 | 7 | ```{r echo=FALSE} 8 | knitr::opts_chunk$set( 9 | comment = "#>", 10 | collapse = TRUE, 11 | warning = FALSE, 12 | message = FALSE 13 | ) 14 | ``` 15 | 16 | rtimes vignette - R client for New York Times APIs 17 | ====== 18 | 19 | ## About the package 20 | 21 | `rtimes` is an R package to search and retrieve data from the New York Times congress API. 22 | 23 | Functions in `rtimes` that wrap these APIs are prefixed by two letter acronyms fo reach API + the function name itself, e.g.: `cg` + `fxn` 24 | 25 | * `cg` for the [Congress API](https://projects.propublica.org/api-docs/congress-api/) 26 | * `as` for the [Article Search API](https://developer.nytimes.com/docs/articlesearch-product/1/overview) 27 | * `cf` for the [Campaign Finance API](https://propublica.github.io/campaign-finance-api-docs) 28 | * `geo` for the [Geographic API](https://developer.nytimes.com/docs/geo-product/1/routes/query.json/get) 29 | 30 | Please get your own API keys at https://developer.nytimes.com/ - you can use one API key for all their APIs. 31 | 32 | I set up the functions so that you can put the key in your `.Renviron` file, which will 33 | be called on startup of R, and then you don't have to enter your API key for each run 34 | of a function. Add entries for an R session like 35 | 36 | ``` 37 | Sys.setenv(NYTIMES_API_KEY = "YOURKEYHERE") 38 | Sys.setenv(PROPUBLICA_API_KEY = "YOURKEYHERE") 39 | ``` 40 | 41 | Or set them across sessions by putting entries in your `.Renviron` file like 42 | 43 | ``` 44 | NYTIMES_API_KEY= 45 | PROPUBLICA_API_KEY= 46 | ``` 47 | 48 | You can also pass in your key in a function call, but be careful not to expose your keys in 49 | code committed to public repositories. If you do pass in a function call, use e.g., `Sys.getenv("NYTIMES_API_KEY")`. 50 | 51 | ## Install rtimes 52 | 53 | From CRAN 54 | 55 | ```{r eval=FALSE} 56 | install.packages("rtimes") 57 | ``` 58 | 59 | Development version from GitHub 60 | 61 | ```{r install, eval=FALSE} 62 | install.packages("devtools") 63 | devtools::install_github("ropengov/rtimes") 64 | ``` 65 | 66 | ## Load rtimes 67 | 68 | ```{r load} 69 | library('rtimes') 70 | ``` 71 | 72 | _Note: Member ID S001181 is Jeanne Shaheen_ 73 | 74 | ## Congress API 75 | 76 | ### Bill cosponsorship data for a member 77 | 78 | ```{r cg_billscosponsor} 79 | out <- cg_billscosponsor(memberid='S001181', type='cosponsored') 80 | out$data 81 | ``` 82 | 83 | ### Member appearances 84 | 85 | ```{r cg_memberappear} 86 | out <- cg_memberappear(memberid='S001181') 87 | out$data 88 | ``` 89 | 90 | ## Article search API 91 | 92 | This function in most cases outputs a series of S3 objects, one for each item 93 | found. Each element, an object of class `as_search`, is a summary of a list 94 | of data. You can `unclass()` the object if you want, or index into particular 95 | elements (see egs below). 96 | 97 | Here, we search for _bailout_ between two dates, Oct 1 2008 and Dec 1 2008 98 | 99 | ```{r} 100 | res <- as_search(q="bailout", begin_date = "20081001", end_date = '20081201') 101 | res$copyright 102 | res$meta 103 | res$data 104 | ``` 105 | 106 | ```{r echo=FALSE} 107 | Sys.sleep(3) 108 | ``` 109 | 110 | Another e.g., Search for keyword _money_, within the _Sports_ and _Foreign_ news desks 111 | 112 | ```{r} 113 | res <- as_search(q = "money", fq = 'news_desk:("Sports" "Foreign")') 114 | res$data 115 | ``` 116 | 117 | ## Campaign Finance API 118 | 119 | 120 | Here, we search for campaign details for the 2008 cycle, with FEC ID number P80003338 121 | 122 | ```{r} 123 | cf_candidate_details(campaign_cycle = 2008, fec_id = 'P80003338') 124 | ``` 125 | 126 | ## Geographic API 127 | 128 | The geographic API allows you to do geo based searches of place names. It's built on 129 | the Geonames database. 130 | 131 | Here, we search for results for locations in the US 132 | 133 | ```{r} 134 | geo_search(country_code = 'US') 135 | ``` 136 | -------------------------------------------------------------------------------- /inst/vign/rtimes_vignette.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | rtimes vignette - R client for New York Times APIs 10 | ====== 11 | 12 | ## About the package 13 | 14 | `rtimes` is an R package to search and retrieve data from the New York Times congress API. 15 | 16 | Functions in `rtimes` that wrap these APIs are prefixed by two letter acronyms fo reach API + the function name itself, e.g.: `cg` + `fxn` 17 | 18 | * `cg` for the [Congress API](https://propublica.github.io/congress-api-docs) 19 | * `as` for the [Article Search API](http://developer.nytimes.com/article_search_v2.json) 20 | * `cf` for the [Campaign Finance API](https://propublica.github.io/campaign-finance-api-docs) 21 | * `geo` for the [Geographic API](http://developer.nytimes.com/geo_api_v2.json) 22 | 23 | Please get your own API keys at http://developer.nytimes.com/apps/register - you'll need a different key for each API. 24 | 25 | I set up the functions so that you can put the key in your `.Renviron` file, which will 26 | be called on startup of R, and then you don't have to enter your API key for each run 27 | of a function. Add entries for an R session like 28 | 29 | ``` 30 | Sys.setenv(NYTIMES_GEO_KEY = "YOURKEYHERE") 31 | Sys.setenv(NYTIMES_AS_KEY = "YOURKEYHERE") 32 | Sys.setenv(PROPUBLICA_API_KEY = "YOURKEYHERE") 33 | ``` 34 | 35 | Or set them across sessions by putting entries in your `.Renviron` file like 36 | 37 | ``` 38 | NYTIMES_GEO_KEY= 39 | NYTIMES_AS_KEY= 40 | PROPUBLICA_API_KEY= 41 | ``` 42 | 43 | You can also pass in your key in a function call, but be careful not to expose your keys in 44 | code committed to public repositories. If you do pass in a function call, use e.g., `Sys.getenv("NYTIMES_GEO_KEY")`. 45 | 46 | ## Install rtimes 47 | 48 | From CRAN 49 | 50 | 51 | ```r 52 | install.packages("rtimes") 53 | ``` 54 | 55 | Development version from GitHub 56 | 57 | 58 | ```r 59 | install.packages("devtools") 60 | devtools::install_github("ropengov/rtimes") 61 | ``` 62 | 63 | ## Load rtimes 64 | 65 | 66 | ```r 67 | library('rtimes') 68 | ``` 69 | 70 | _Note: Member ID S001181 is Jeanne Shaheen_ 71 | 72 | ## Congress API 73 | 74 | ### Bill cosponsorship data for a member 75 | 76 | 77 | ```r 78 | out <- cg_billscosponsor(memberid='S001181', type='cosponsored') 79 | out$data 80 | #> # A tibble: 20 x 26 81 | #> congress bill_id bill_type number 82 | #> 83 | #> 1 115 s756-115 s S.756 84 | #> 2 115 s1616-115 s S.1616 85 | #> 3 115 s1738-115 s S.1738 86 | #> 4 115 s581-115 s S.581 87 | #> 5 115 s1710-115 s S.1710 88 | #> 6 115 s1715-115 s S.1715 89 | #> 7 115 s1726-115 s S.1726 90 | #> 8 115 s1730-115 s S.1730 91 | #> 9 115 s1706-115 s S.1706 92 | #> 10 115 s1692-115 s S.1692 93 | #> 11 115 s1693-115 s S.1693 94 | #> 12 115 s1688-115 s S.1688 95 | #> 13 115 sres241-115 sres S.RES.241 96 | #> 14 115 s1675-115 s S.1675 97 | #> 15 115 s1654-115 s S.1654 98 | #> 16 115 sres234-115 sres S.RES.234 99 | #> 17 115 s1640-115 s S.1640 100 | #> 18 115 sres233-115 sres S.RES.233 101 | #> 19 115 s1629-115 s S.1629 102 | #> 20 115 sres230-115 sres S.RES.230 103 | #> # ... with 22 more variables: bill_uri , title , 104 | #> # cosponsored_date , sponsor_title , sponsor_id , 105 | #> # sponsor_name , sponsor_state , sponsor_party , 106 | #> # sponsor_uri , gpo_pdf_uri , congressdotgov_url , 107 | #> # govtrack_url , introduced_date , active , 108 | #> # senate_passage , cosponsors , committees , 109 | #> # primary_subject , summary , summary_short , 110 | #> # latest_major_action_date , latest_major_action 111 | ``` 112 | 113 | ### Member appearances 114 | 115 | 116 | ```r 117 | out <- cg_memberappear(memberid='S001181') 118 | out$data 119 | #> # A tibble: 20 x 5 120 | #> date 121 | #> 122 | #> 1 2010-09-29 123 | #> 2 2010-09-22 124 | #> 3 2010-09-17 125 | #> 4 2010-09-16 126 | #> 5 2010-08-03 127 | #> 6 2010-07-29 128 | #> 7 2010-07-28 129 | #> 8 2010-07-22 130 | #> 9 2010-07-21 131 | #> 10 2010-07-21 132 | #> 11 2010-07-20 133 | #> 12 2010-07-15 134 | #> 13 2010-07-15 135 | #> 14 2010-06-30 136 | #> 15 2010-06-29 137 | #> 16 2010-06-23 138 | #> 17 2010-06-17 139 | #> 18 2010-06-16 140 | #> 19 2010-06-10 141 | #> 20 2010-06-09 142 | #> # ... with 4 more variables: title , url , start_time , 143 | #> # end_time 144 | ``` 145 | 146 | ## Article search API 147 | 148 | This function in most cases outputs a series of S3 objects, one for each item 149 | found. Each element, an object of class `as_search`, is a summary of a list 150 | of data. You can `unclass()` the object if you want, or index into particular 151 | elements (see egs below). 152 | 153 | Here, we search for _bailout_ between two dates, Oct 1 2008 and Dec 1 2008 154 | 155 | 156 | ```r 157 | res <- as_search(q="bailout", begin_date = "20081001", end_date = '20081201') 158 | res$copyright 159 | #> [1] "Copyright (c) 2015 The New York Times Company. All Rights Reserved." 160 | res$meta 161 | #> # A tibble: 1 x 3 162 | #> hits offset time 163 | #> 164 | #> 1 1203 0 8 165 | res$data 166 | #> # A tibble: 10 x 19 167 | #> web_url 168 | #> * 169 | #> 1 https://dealbook.nytimes.com/2008/10/17/nader-displays-new-fervor-on-the-ba 170 | #> 2 https://dealbook.nytimes.com/2008/10/07/its-the-economy-redux/ 171 | #> 3 https://www.nytimes.com/2008/12/01/opinion/l01citigroup.html 172 | #> 4 https://www.nytimes.com/2008/12/01/business/economy/01auto.html 173 | #> 5 https://www.nytimes.com/2008/12/01/business/01tanta.html 174 | #> 6 https://www.nytimes.com/2008/12/01/business/01uaw.html 175 | #> 7 https://www.nytimes.com/2008/12/01/business/economy/01stimulus.html 176 | #> 8 https://www.nytimes.com/2008/11/30/opinion/30sun1.html 177 | #> 9 https://www.nytimes.com/2008/11/30/opinion/30boskin.html 178 | #> 10 https://www.nytimes.com/2008/11/30/business/30dealer.html 179 | #> # ... with 18 more variables: snippet , abstract , source , 180 | #> # multimedia , keywords , pub_date , 181 | #> # document_type , section_name , type_of_material , 182 | #> # `_id` , word_count , score , print_page , 183 | #> # new_desk , headline.main , headline.kicker , 184 | #> # headline.print_headline , byline.original 185 | ``` 186 | 187 | 188 | 189 | Another e.g., Search for keyword _money_, within the _Sports_ and _Foreign_ news desks 190 | 191 | 192 | ```r 193 | res <- as_search(q = "money", fq = 'news_desk:("Sports" "Foreign")') 194 | res$data 195 | #> # A tibble: 10 x 19 196 | #> web_url 197 | #> * 198 | #> 1 https://www.nytimes.com/2017/06/16/world/asia/1mdb-malaysia-najib-razak.htm 199 | #> 2 https://www.nytimes.com/2017/07/28/sports/football/giants-odell-beckham-con 200 | #> 3 https://www.nytimes.com/2017/08/04/sports/soccer/neymar-psg-soccer.html 201 | #> 4 https://www.nytimes.com/2017/08/10/sports/soccer/soccer-capital-lure-of-lon 202 | #> 5 https://www.nytimes.com/2017/07/19/world/europe/grenfell-tower-fire-donatio 203 | #> 6 https://www.nytimes.com/2017/07/12/sports/basketball/the-two-men-who-make-m 204 | #> 7 https://www.nytimes.com/2017/06/24/world/africa/angola-luanda-jose-eduardo- 205 | #> 8 https://www.nytimes.com/2017/08/04/sports/soccer/wolverhampton-jorge-mendes 206 | #> 9 https://www.nytimes.com/2017/06/15/sports/soccer/fifa-jorge-luis-arzuaga-pl 207 | #> 10 https://www.nytimes.com/2017/06/26/sports/tennis/itf-pro-circuit-wozniak-st 208 | #> # ... with 18 more variables: snippet , print_page , 209 | #> # source , multimedia , keywords , pub_date , 210 | #> # document_type , new_desk , section_name , 211 | #> # type_of_material , `_id` , word_count , score , 212 | #> # uri , headline.main , headline.print_headline , 213 | #> # headline.kicker , byline.original 214 | ``` 215 | 216 | ## Campaign Finance API 217 | 218 | 219 | Here, we search for campaign details for the 2008 cycle, with FEC ID number P80003338 220 | 221 | 222 | ```r 223 | cf_candidate_details(campaign_cycle = 2008, fec_id = 'P80003338') 224 | #> $status 225 | #> [1] "OK" 226 | #> 227 | #> $copyright 228 | #> [1] "Copyright (c) 2017 ProPublica Inc. All Rights Reserved." 229 | #> 230 | #> $data 231 | #> # A tibble: 1 x 24 232 | #> id name party 233 | #> 234 | #> 1 P80003338 OBAMA, BARACK DEM 235 | #> # ... with 21 more variables: fec_uri , committee , 236 | #> # mailing_address , mailing_city , mailing_state , 237 | #> # mailing_zip , status , total_receipts , 238 | #> # total_from_individuals , total_from_pacs , 239 | #> # total_contributions , candidate_loans , 240 | #> # total_disbursements , begin_cash , end_cash , 241 | #> # total_refunds , debts_owed , date_coverage_from , 242 | #> # date_coverage_to , independent_expenditures , 243 | #> # coordinated_expenditures 244 | ``` 245 | 246 | ## Geographic API 247 | 248 | The geographic API allows you to do geo based searches of place names. It's built on 249 | the Geonames database. 250 | 251 | Here, we search for results for locations in the US 252 | 253 | 254 | ```r 255 | geo_search(country_code = 'US') 256 | #> $copyright 257 | #> [1] "Copyright (c) 2015 The New York Times Company. All Rights Reserved." 258 | #> 259 | #> $meta 260 | #> status num_results 261 | #> 1 OK 100 262 | #> 263 | #> $data 264 | #> # A tibble: 100 x 27 265 | #> concept_id concept_name geocode_id geoname_id 266 | #> * 267 | #> 1 22456 Abilene (Tex) 4500 4669635 268 | #> 2 22460 Abingdon (Va) 7752 4743815 269 | #> 3 22480 Acadia National Park (Me) 4504 4956449 270 | #> 4 22508 Adirondack Mountains (NY) 1488 5106772 271 | #> 5 22548 Alabama 364 4829764 272 | #> 6 22556 Alaska 292 5879092 273 | #> 7 22564 Albany (NY) 464 5106834 274 | #> 8 22572 Albuquerque (NM) 1596 5454711 275 | #> 9 22576 Alcatraz (San Francisco) 7772 5322901 276 | #> 10 22580 Alexandria (Va) 4008 4744091 277 | #> # ... with 90 more rows, and 23 more variables: name , 278 | #> # latitude , longitude , elevation , population , 279 | #> # country_code , country_name , admin_code1 , 280 | #> # admin_code2 , admin_code3 , admin_code4 , 281 | #> # admin_name1 , admin_name2 , admin_name3 , 282 | #> # admin_name4 , feature_class , feature_code , 283 | #> # feature_code_name , time_zone_id , dst_offset , 284 | #> # gmt_offset , geocodes_created , geocodes_updated 285 | ``` 286 | -------------------------------------------------------------------------------- /man-roxygen/articlesearch.R: -------------------------------------------------------------------------------- 1 | #' @param q Search query term. Search is performed on the article body, 2 | #' headline and byline. 3 | #' @param fq Filtered search query using standard Lucene syntax. The filter 4 | #' query can be specified with or without a limiting field: label. See 5 | #' \code{fq_fields} for the filter query fields. 6 | #' @param sort (character) Default NULL. One of newest or oldest . By default, 7 | #' search results are sorted by their relevance to the query term (q). Use the 8 | #' sort parameter to sort by pub_date. 9 | #' @param begin_date Begin date - Restricts responses to results with publication 10 | #' dates of the date specified or later. In the form YYYYMMDD 11 | #' @param end_date End date - Restricts responses to results with publication 12 | #' dates of the date specified or earlier. In the form YYYYMMDD 13 | #' @param fl (character) Vector of fields to return. default: all fields 14 | #' @param hl (logical) Highlight or not, default: \code{FALSE} 15 | #' @param page Page number. The value of page corresponds to a set of 10 16 | #' results (it does not indicate the starting number of the result set). For 17 | #' example, page=0 corresponds to records 0-9. To return records 10-19, set 18 | #' page to 1, not 10. 19 | #' @param facet_field (character) Specifies the sets of facet values to include 20 | #' in the facets array at the end of response, which collects the facet values 21 | #' from all the search results. By default no facet fields will be returned. 22 | #' See Details for options. 23 | #' @param facet_filter (logical) Fields to facet on, as vector. When set to 24 | #' \code{TRUE}, facet counts will respect any applied filters (fq, date range, 25 | #' etc.) in addition to the main query term. To filter facet counts, specifying 26 | #' at least one facet_field is required. 27 | #' @param key your New York Times API key; pass in, or loads from .Renviron as 28 | #' \code{NYTIMES_KEY}. See \code{\link{rtimes-package}} for info on rate 29 | #' limits. 30 | #' @param ... Futher args pass into query 31 | #' @param callopts Curl options (debugging tools mostly) passed to 32 | #' \code{\link[crul]{HttpClient}} 33 | #' @param all_results (logical) return all results. Default: \code{FALSE} 34 | #' Internally, we do paging for you. We get first up to 10 results, then if 35 | #' there's more we page through results to get all of them. 36 | #' @param try_flatten (logical) attempt to completely flatten data.frame, 37 | #' which may fail in some cases as its hard to account for all edge cases. 38 | #' Default: \code{FALSE} 39 | #' @param sleep (integer) seconds to sleep between successive requests, only 40 | #' used when \code{all_results=TRUE} 41 | #' 42 | #' @details 43 | #' \code{fl} parameter options are: 44 | #' \itemize{ 45 | #' \item web_url 46 | #' \item snippet 47 | #' \item lead_paragraph 48 | #' \item abstract 49 | #' \item print_page 50 | #' \item blog 51 | #' \item source 52 | #' \item multimedia 53 | #' \item headline 54 | #' \item keywords 55 | #' \item pub_date 56 | #' \item document_type 57 | #' \item news_desk 58 | #' \item byline 59 | #' \item type_of_material 60 | #' \item _id 61 | #' \item word_count 62 | #' } 63 | #' 64 | #' \code{facet_field} param options are: 65 | #' \itemize{ 66 | #' \item section_name 67 | #' \item document_type 68 | #' \item type_of_material 69 | #' \item source 70 | #' \item day_of_week 71 | #' } 72 | #' 73 | -------------------------------------------------------------------------------- /man-roxygen/finance.R: -------------------------------------------------------------------------------- 1 | #' @param campaign_cycle An even-numbdered year in YYYY format. Presidential data: 2 | #' 2008-present; Congressional data: 2000-present 3 | #' @param key (character) your Propublica API key; pass in, or loads from .Renviron as 4 | #' \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} for info on rate limits. 5 | -------------------------------------------------------------------------------- /man-roxygen/geographic.R: -------------------------------------------------------------------------------- 1 | #' @param name (character) A displayable name for the specified place. 2 | #' @param latitude (numeric) The latitude of the specified place. 3 | #' @param longitude (numeric) The longitude of the specified place. 4 | #' @param elevation (character) The elevation of the specified place, in meters. 5 | #' @param population (numeric) Te population of the specified place. 6 | #' @param country_code (character) The country code, as given in ISO-3611, of the specified place. 7 | #' @param country_name (character) The country name of the specified place. 8 | #' @param admin_code1 (character) A code for the first level administrative division containing 9 | #' the specified place. In the case of US locations this will be the containing state's postal 10 | #' code. All geonames admin_code1 records can be found at 11 | #' http://download.geonames.org/export/dump/admin1Codes.txt. 12 | #' @param admin_name1 (character) The name of the first level administrative division containing 13 | #' the specified place. In the case of US locations this will be the full name of the containing 14 | #' State. 15 | #' @param admin_code2 (character) A code for the second level administrative division containing 16 | #' the specified place. In the case of US locations this will be a code for the containing county. 17 | #' All geonames admin_code2 records can be found at 18 | #' http://download.geonames.org/export/dump/admin2Codes.txt 19 | #' @param admin_name2 (character) The name of the second level administrative division containing 20 | #' the specified place. In the case of US locations this will be the name of the containing county. 21 | #' @param admin_code3 (character) A code for the third level administrative division containing the 22 | #' specified place. This is infrequently used and never used for US locations. 23 | #' @param admin_name3 (character) The name of the third level administrative division containing 24 | #' the specified place. This is infrequently used and never used for US locations. 25 | #' @param admin_code4 (character) A code for the fourth level administrative division containing 26 | #' the specified place. This is infrequently used and never used for US locations. 27 | #' @param admin_name4 (character) The name of the fourth level administrative division containing 28 | #' the specified place. This is infrequently used and never used for US locations. 29 | #' @param feature_class (character) The high-level feature class of the location. The feature 30 | #' classes are as follows: 31 | #' \itemize{ 32 | #' \item A - Administrative Boundary Features 33 | #' \item H - Hydrographic Features 34 | #' \item L - Area Features 35 | #' \item P - Populated Place Features 36 | #' \item R - Road / Railroad Features 37 | #' \item S - Spot Features 38 | #' \item T - Hypsographic Features 39 | #' \item U - Undersea Features 40 | #' \item V - Vegetation Features 41 | #' } 42 | #' @param feature_class_name (character) See list of name values associated with feature_class 43 | #' above. 44 | #' @param feature_code_name (character) A code for the type of feature represented by this geonames 45 | #' record. The complete list of feature codes can be found at 46 | #' http://download.geonames.org/export/dump/featureCodes_en.txt. 47 | #' @param time_zone_id (character) The time zone containing this geonames record. 48 | #' @param dst_offset (numeric) The offset from GMT during daylight savings time. 49 | #' @param gmt_offset (numeric) The offset from GMT during standard time. 50 | #' @param bounding_box (numeric) Vector of coordinates in the following format , 51 | #' ,,. For example: 52 | #' c(39.985417852135356,-93.18850617968747,37.84003257271992,-95.94607453906247) 53 | #' @param nearby (numeric) A latitude longitude pair (e.g. 41.9,12.5). When provided with this 54 | #' parameter, the geocodes API will return the 20 results that are geographically nearest to the 55 | #' specified latitude/longitude pair. 56 | #' @param offset (numeric) The offset in the results. BEWARE - this parameter doesn't work now 57 | #' (as of 2014-10-21). 58 | #' @param limit (numeric) If not specified 20 results are returned. Otherwise the specified number 59 | #' of results or a maximum of 20 results are returned. BEWARE - this parameter doesn't work now 60 | #' (as of 2015-08-10). 61 | -------------------------------------------------------------------------------- /man-roxygen/nyt.R: -------------------------------------------------------------------------------- 1 | #' @param ... Curl options (debugging tools mostly) passed to 2 | #' \code{\link[crul]{HttpClient}} 3 | -------------------------------------------------------------------------------- /man-roxygen/nytcgkey.R: -------------------------------------------------------------------------------- 1 | #' @param ... Curl options (debugging tools mostly) passed to 2 | #' \code{\link[crul]{HttpClient}} 3 | #' @param key your New York Times API key; pass in, or loads from .Renviron 4 | #' as \code{NYTIMES_CG_KEY}. See \code{\link{rtimes-package}} for info on 5 | #' rate limits. 6 | -------------------------------------------------------------------------------- /man-roxygen/propubkey.R: -------------------------------------------------------------------------------- 1 | #' @param ... Curl options (debugging tools mostly) passed to 2 | #' \code{\link[crul]{HttpClient}} 3 | #' @param key (character) your Propublica API key; pass in, or loads from 4 | #' .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 5 | #' for info on rate limits. 6 | -------------------------------------------------------------------------------- /man/api_limits.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/api_limits.R 3 | \name{api_limits} 4 | \alias{api_limits} 5 | \title{API limits} 6 | \usage{ 7 | api_limits(api = "as", key = NULL) 8 | } 9 | \arguments{ 10 | \item{api}{Which API to check, either "as" or "geo".} 11 | 12 | \item{key}{Your New York Times API key; pass in, or loads from .Renviron as 13 | \code{NYTIMES_AS_KEY}.} 14 | } 15 | \description{ 16 | Check NYT API limits and current usage. 17 | } 18 | \details{ 19 | Check the API daily limit and how many calls have already been consumed. 20 | Each check consumes one API call by itself. 21 | } 22 | \examples{ 23 | \dontrun{ 24 | api_limits("as") 25 | } 26 | } 27 | \references{ 28 | \url{https://developer.nytimes.com/faq#9} 29 | } 30 | -------------------------------------------------------------------------------- /man/as_search.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/as_search.R 3 | \name{as_search} 4 | \alias{as_search} 5 | \title{Article search} 6 | \usage{ 7 | as_search(q, fq = NULL, sort = NULL, begin_date = NULL, 8 | end_date = NULL, key = NULL, fl = NULL, hl = FALSE, page = 0, 9 | all_results = FALSE, try_flatten = FALSE, facet_field = NULL, 10 | facet_filter = NULL, sleep = 2, ..., callopts = list()) 11 | } 12 | \arguments{ 13 | \item{q}{Search query term. Search is performed on the article body, 14 | headline and byline.} 15 | 16 | \item{fq}{Filtered search query using standard Lucene syntax. The filter 17 | query can be specified with or without a limiting field: label. See 18 | \code{fq_fields} for the filter query fields.} 19 | 20 | \item{sort}{(character) Default NULL. One of newest or oldest . By default, 21 | search results are sorted by their relevance to the query term (q). Use the 22 | sort parameter to sort by pub_date.} 23 | 24 | \item{begin_date}{Begin date - Restricts responses to results with publication 25 | dates of the date specified or later. In the form YYYYMMDD} 26 | 27 | \item{end_date}{End date - Restricts responses to results with publication 28 | dates of the date specified or earlier. In the form YYYYMMDD} 29 | 30 | \item{key}{your New York Times API key; pass in, or loads from .Renviron as 31 | \code{NYTIMES_KEY}. See \code{\link{rtimes-package}} for info on rate 32 | limits.} 33 | 34 | \item{fl}{(character) Vector of fields to return. default: all fields} 35 | 36 | \item{hl}{(logical) Highlight or not, default: \code{FALSE}} 37 | 38 | \item{page}{Page number. The value of page corresponds to a set of 10 39 | results (it does not indicate the starting number of the result set). For 40 | example, page=0 corresponds to records 0-9. To return records 10-19, set 41 | page to 1, not 10.} 42 | 43 | \item{all_results}{(logical) return all results. Default: \code{FALSE} 44 | Internally, we do paging for you. We get first up to 10 results, then if 45 | there's more we page through results to get all of them.} 46 | 47 | \item{try_flatten}{(logical) attempt to completely flatten data.frame, 48 | which may fail in some cases as its hard to account for all edge cases. 49 | Default: \code{FALSE}} 50 | 51 | \item{facet_field}{(character) Specifies the sets of facet values to include 52 | in the facets array at the end of response, which collects the facet values 53 | from all the search results. By default no facet fields will be returned. 54 | See Details for options.} 55 | 56 | \item{facet_filter}{(logical) Fields to facet on, as vector. When set to 57 | \code{TRUE}, facet counts will respect any applied filters (fq, date range, 58 | etc.) in addition to the main query term. To filter facet counts, specifying 59 | at least one facet_field is required.} 60 | 61 | \item{sleep}{(integer) seconds to sleep between successive requests, only 62 | used when \code{all_results=TRUE}} 63 | 64 | \item{...}{Futher args pass into query} 65 | 66 | \item{callopts}{Curl options (debugging tools mostly) passed to 67 | \code{\link[crul]{HttpClient}}} 68 | } 69 | \description{ 70 | Article search 71 | } 72 | \details{ 73 | \code{fl} parameter options are: 74 | \itemize{ 75 | \item web_url 76 | \item snippet 77 | \item lead_paragraph 78 | \item abstract 79 | \item print_page 80 | \item blog 81 | \item source 82 | \item multimedia 83 | \item headline 84 | \item keywords 85 | \item pub_date 86 | \item document_type 87 | \item news_desk 88 | \item byline 89 | \item type_of_material 90 | \item _id 91 | \item word_count 92 | } 93 | 94 | \code{facet_field} param options are: 95 | \itemize{ 96 | \item section_name 97 | \item document_type 98 | \item type_of_material 99 | \item source 100 | \item day_of_week 101 | } 102 | } 103 | \examples{ 104 | \dontrun{ 105 | # basic search - copyright, metadata, data, and facet slots 106 | (x <- as_search(q="bailout", begin_date = "20081001", 107 | end_date = '20081005', callopts = list(verbose = TRUE))) 108 | x$copyright 109 | x$meta 110 | x$data 111 | x$facet 112 | Sys.sleep(1) 113 | 114 | as_search(q="money", fq = 'The New York Times', fl = c('word_count', 115 | 'snippet', 'headline')) 116 | Sys.sleep(1) 117 | x <- as_search(q="bailout", hl = TRUE) 118 | x$data$snippet 119 | Sys.sleep(1) 120 | 121 | # all results 122 | (x <- as_search(q="bailout", begin_date = "20081001", 123 | end_date = '20081003', all_results = TRUE)) 124 | x$meta 125 | x$data 126 | Sys.sleep(1) 127 | 128 | # facetting 129 | as_search(q="bailout", facet_field = 'section_name', begin_date = "20081001", 130 | end_date = '20081201') 131 | Sys.sleep(1) 132 | ## with facet filtering 133 | as_search(q="bailout", facet_field = 'section_name', begin_date = "20081001", 134 | end_date = '20081201', facet_filter = TRUE) 135 | 136 | # curl options 137 | x <- as_search(q="bailout", begin_date = "20081001", 138 | end_date = '20081005', callopts = list(verbose = TRUE)) 139 | } 140 | } 141 | \references{ 142 | \url{http://developer.nytimes.com}, 143 | \url{https://developer.nytimes.com/docs/articlesearch-product/1/overview} 144 | } 145 | -------------------------------------------------------------------------------- /man/cf_candidate_details.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cf_candidate_details.R 3 | \name{cf_candidate_details} 4 | \alias{cf_candidate_details} 5 | \title{Campaign finance - candidate details} 6 | \usage{ 7 | cf_candidate_details(campaign_cycle = NULL, fec_id = NULL, 8 | key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{campaign_cycle}{An even-numbdered year in YYYY format. Presidential data: 12 | 2008-present; Congressional data: 2000-present} 13 | 14 | \item{fec_id}{FEC id} 15 | 16 | \item{key}{(character) your Propublica API key; pass in, or loads from .Renviron as 17 | \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} for info on rate limits.} 18 | 19 | \item{...}{Curl options (debugging tools mostly) passed to 20 | \code{\link[crul]{HttpClient}}} 21 | } 22 | \description{ 23 | Campaign finance - candidate details 24 | } 25 | \examples{ 26 | \dontrun{ 27 | cf_candidate_details(campaign_cycle = 2008, fec_id = 'P80003338') 28 | cf_candidate_details(campaign_cycle = 2012, fec_id = 'P80003338') 29 | } 30 | } 31 | \references{ 32 | \url{http://propublica.github.io/campaign-finance-api-docs} 33 | } 34 | \seealso{ 35 | Other campaign-finance: \code{\link{cf_candidate_leaders}}, 36 | \code{\link{cf_candidate_new}}, 37 | \code{\link{cf_candidate_search}}, 38 | \code{\link{cf_candidate_state}} 39 | } 40 | \concept{campaign-finance} 41 | -------------------------------------------------------------------------------- /man/cf_candidate_leaders.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cf_candidate_leaders.R 3 | \name{cf_candidate_leaders} 4 | \alias{cf_candidate_leaders} 5 | \title{Campaign finance - candidate leaders} 6 | \usage{ 7 | cf_candidate_leaders(campaign_cycle = NULL, category = NULL, 8 | key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{campaign_cycle}{An even-numbdered year in YYYY format. Presidential data: 12 | 2008-present; Congressional data: 2000-present} 13 | 14 | \item{category}{Category. Specify one of these categories: 15 | \itemize{ 16 | \item Candidate Loan \code{candidate-loan} 17 | \item Contribution Total \code{contribution-total} 18 | \item Debts Owed \code{debts-owed} 19 | \item Disbursements Total \code{disbursements-total} 20 | \item End Cash \code{end-cash} 21 | \item Individual Total \code{individual-total} 22 | \item PAC Total \code{pac-total} 23 | \item Receipts Total \code{receipts-total} 24 | \item Refund Total \code{refund-total} 25 | }} 26 | 27 | \item{key}{(character) your Propublica API key; pass in, or loads from .Renviron as 28 | \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} for info on rate limits.} 29 | 30 | \item{...}{Curl options (debugging tools mostly) passed to 31 | \code{\link[crul]{HttpClient}}} 32 | } 33 | \description{ 34 | Campaign finance - candidate leaders 35 | } 36 | \examples{ 37 | \dontrun{ 38 | cf_candidate_leaders(campaign_cycle = 2010, category='end-cash') 39 | cf_candidate_leaders(campaign_cycle = 2008, category='receipts-total') 40 | } 41 | } 42 | \references{ 43 | \url{http://propublica.github.io/campaign-finance-api-docs} 44 | } 45 | \seealso{ 46 | Other campaign-finance: \code{\link{cf_candidate_details}}, 47 | \code{\link{cf_candidate_new}}, 48 | \code{\link{cf_candidate_search}}, 49 | \code{\link{cf_candidate_state}} 50 | } 51 | \concept{campaign-finance} 52 | -------------------------------------------------------------------------------- /man/cf_candidate_new.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cf_candidate_new.R 3 | \name{cf_candidate_new} 4 | \alias{cf_candidate_new} 5 | \title{Campaign finance - get new candidates} 6 | \usage{ 7 | cf_candidate_new(campaign_cycle = NULL, key = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{campaign_cycle}{An even-numbdered year in YYYY format. Presidential data: 11 | 2008-present; Congressional data: 2000-present} 12 | 13 | \item{key}{(character) your Propublica API key; pass in, or loads from .Renviron as 14 | \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} for info on rate limits.} 15 | 16 | \item{...}{Curl options (debugging tools mostly) passed to 17 | \code{\link[crul]{HttpClient}}} 18 | } 19 | \description{ 20 | Campaign finance - get new candidates 21 | } 22 | \examples{ 23 | \dontrun{ 24 | cf_candidate_new(campaign_cycle = 2016) 25 | cf_candidate_new(campaign_cycle = 2015) 26 | cf_candidate_new(campaign_cycle = 2014) 27 | } 28 | } 29 | \references{ 30 | \url{http://propublica.github.io/campaign-finance-api-docs} 31 | } 32 | \seealso{ 33 | Other campaign-finance: \code{\link{cf_candidate_details}}, 34 | \code{\link{cf_candidate_leaders}}, 35 | \code{\link{cf_candidate_search}}, 36 | \code{\link{cf_candidate_state}} 37 | } 38 | \concept{campaign-finance} 39 | -------------------------------------------------------------------------------- /man/cf_candidate_search.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cf_candidate_search.R 3 | \name{cf_candidate_search} 4 | \alias{cf_candidate_search} 5 | \title{Campaign finance - candidate search} 6 | \usage{ 7 | cf_candidate_search(campaign_cycle = NULL, query = NULL, key = NULL, 8 | ...) 9 | } 10 | \arguments{ 11 | \item{campaign_cycle}{An even-numbdered year in YYYY format. Presidential data: 12 | 2008-present; Congressional data: 2000-present} 13 | 14 | \item{query}{Last name of a candidate} 15 | 16 | \item{key}{(character) your Propublica API key; pass in, or loads from .Renviron as 17 | \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} for info on rate limits.} 18 | 19 | \item{...}{Curl options (debugging tools mostly) passed to 20 | \code{\link[crul]{HttpClient}}} 21 | } 22 | \description{ 23 | Campaign finance - candidate search 24 | } 25 | \examples{ 26 | \dontrun{ 27 | cf_candidate_search(campaign_cycle = 2016, query='Wilson') 28 | cf_candidate_search(campaign_cycle = 2008, query='obama') 29 | } 30 | } 31 | \references{ 32 | \url{http://propublica.github.io/campaign-finance-api-docs} 33 | } 34 | \seealso{ 35 | Other campaign-finance: \code{\link{cf_candidate_details}}, 36 | \code{\link{cf_candidate_leaders}}, 37 | \code{\link{cf_candidate_new}}, 38 | \code{\link{cf_candidate_state}} 39 | } 40 | \concept{campaign-finance} 41 | \keyword{internal} 42 | -------------------------------------------------------------------------------- /man/cf_candidate_state.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cf_candidate_state.R 3 | \name{cf_candidate_state} 4 | \alias{cf_candidate_state} 5 | \title{Campaign finance - candidates from a state} 6 | \usage{ 7 | cf_candidate_state(campaign_cycle, state, chamber = NULL, 8 | district = NULL, key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{campaign_cycle}{An even-numbdered year in YYYY format. Presidential data: 12 | 2008-present; Congressional data: 2000-present} 13 | 14 | \item{state}{(character) Two-letter state abbreviation. required.} 15 | 16 | \item{chamber}{(character) house or senate. required.} 17 | 18 | \item{district}{(character) Specify the district number. Use 1 for states 19 | with a single representative. (House requests only - districts with 20 | Senate requests will be ignored.). required.} 21 | 22 | \item{key}{(character) your Propublica API key; pass in, or loads from .Renviron as 23 | \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} for info on rate limits.} 24 | 25 | \item{...}{Curl options (debugging tools mostly) passed to 26 | \code{\link[crul]{HttpClient}}} 27 | } 28 | \description{ 29 | Campaign finance - candidates from a state 30 | } 31 | \examples{ 32 | \dontrun{ 33 | cf_candidate_state(campaign_cycle = 2014, state = "TX") 34 | cf_candidate_state(campaign_cycle = 2012, state = "NM") 35 | cf_candidate_state(campaign_cycle = 2016, state = "OR", chamber = "senate") 36 | cf_candidate_state(campaign_cycle = 2016, state = "CA", 37 | chamber = "house", district = 2) 38 | } 39 | } 40 | \references{ 41 | \url{http://propublica.github.io/campaign-finance-api-docs} 42 | } 43 | \seealso{ 44 | Other campaign-finance: \code{\link{cf_candidate_details}}, 45 | \code{\link{cf_candidate_leaders}}, 46 | \code{\link{cf_candidate_new}}, 47 | \code{\link{cf_candidate_search}} 48 | } 49 | \concept{campaign-finance} 50 | -------------------------------------------------------------------------------- /man/cg_billscosponsor.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_billscosponsor.R 3 | \name{cg_billscosponsor} 4 | \alias{cg_billscosponsor} 5 | \title{Get bill cosponsorship data for a particular member.} 6 | \usage{ 7 | cg_billscosponsor(memberid = NULL, type = NULL, key = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{memberid}{The member's unique ID number (alphanumeric). To find a 11 | member's ID number, get the list of members for the appropriate House 12 | or Senate. You can also use the Biographical Directory of the United 13 | States Congress to get a member's ID. In search results, each member's 14 | name is linked to a record by index ID (e.g., 15 | http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 16 | Use the index ID as member-id in your request.} 17 | 18 | \item{type}{One of 'cosponsored' (the 20 bills most recently cosponsored 19 | by member-id) or 'withdrawn' (the 20 most recently withdrawn 20 | cosponsorships for member-id).} 21 | 22 | \item{key}{(character) your Propublica API key; pass in, or loads from 23 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 24 | for info on rate limits.} 25 | 26 | \item{...}{Curl options (debugging tools mostly) passed to 27 | \code{\link[crul]{HttpClient}}} 28 | } 29 | \value{ 30 | List of new members of he current Congress. 31 | } 32 | \description{ 33 | Get bill cosponsorship data for a particular member. 34 | } 35 | \examples{ 36 | \dontrun{ 37 | cg_billscosponsor(memberid='B001260', type='cosponsored') 38 | } 39 | } 40 | \references{ 41 | Congress API docs 42 | \url{https://projects.propublica.org/api-docs/congress-api/} 43 | } 44 | \seealso{ 45 | Other congress: \code{\link{cg_memberappear}}, 46 | \code{\link{cg_memberbioroles}}, 47 | \code{\link{cg_memberbystatedistrict}}, 48 | \code{\link{cg_membersleaving}}, 49 | \code{\link{cg_memberslist}}, 50 | \code{\link{cg_membersponsorcompare}}, 51 | \code{\link{cg_membervotecompare}}, 52 | \code{\link{cg_membervotepositions}}, 53 | \code{\link{cg_newmembers}}, 54 | \code{\link{cg_rollcallvote}} 55 | } 56 | \concept{congress} 57 | -------------------------------------------------------------------------------- /man/cg_memberappear.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_memberappear.R 3 | \name{cg_memberappear} 4 | \alias{cg_memberappear} 5 | \title{Get information about a particular member's appearances on the House or 6 | Senate floor.} 7 | \usage{ 8 | cg_memberappear(memberid = NULL, key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{memberid}{The member's unique ID number (alphanumeric). To find a 12 | member's ID number, get the list of members for the appropriate House 13 | or Senate. You can also use the Biographical Directory of the United 14 | States Congress to get a member's ID. In search results, each member's 15 | name is linked to a record by index ID (e.g., 16 | http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 17 | Use the index ID as member-id in your request.} 18 | 19 | \item{key}{(character) your Propublica API key; pass in, or loads from 20 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 21 | for info on rate limits.} 22 | 23 | \item{...}{Curl options (debugging tools mostly) passed to 24 | \code{\link[crul]{HttpClient}}} 25 | } 26 | \value{ 27 | Get information about a particular member's appearances on the 28 | House or Senate floor. 29 | } 30 | \description{ 31 | Get information about a particular member's appearances on the House or 32 | Senate floor. 33 | } 34 | \examples{ 35 | \dontrun{ 36 | cg_memberappear(memberid='S001181') 37 | } 38 | } 39 | \references{ 40 | Congress API docs 41 | \url{https://projects.propublica.org/api-docs/congress-api/} 42 | } 43 | \seealso{ 44 | Other congress: \code{\link{cg_billscosponsor}}, 45 | \code{\link{cg_memberbioroles}}, 46 | \code{\link{cg_memberbystatedistrict}}, 47 | \code{\link{cg_membersleaving}}, 48 | \code{\link{cg_memberslist}}, 49 | \code{\link{cg_membersponsorcompare}}, 50 | \code{\link{cg_membervotecompare}}, 51 | \code{\link{cg_membervotepositions}}, 52 | \code{\link{cg_newmembers}}, 53 | \code{\link{cg_rollcallvote}} 54 | } 55 | \concept{congress} 56 | -------------------------------------------------------------------------------- /man/cg_memberbioroles.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_memberbioroles.R 3 | \name{cg_memberbioroles} 4 | \alias{cg_memberbioroles} 5 | \title{Member bio roles} 6 | \usage{ 7 | cg_memberbioroles(memberid = NULL, key = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{memberid}{The member's unique ID number (alphanumeric). To find a 11 | member's ID number, get the list of members for the appropriate House 12 | or Senate. You can also use the Biographical Directory of the United 13 | States Congress to get a member's ID. In search results, each member's 14 | name is linked to a record by index ID (e.g., 15 | http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 16 | Use the index ID as member-id in your request.} 17 | 18 | \item{key}{(character) your Propublica API key; pass in, or loads from 19 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 20 | for info on rate limits.} 21 | 22 | \item{...}{Curl options (debugging tools mostly) passed to 23 | \code{\link[crul]{HttpClient}}} 24 | } 25 | \value{ 26 | List of members of a particular chamber in a particular Congress. 27 | } 28 | \description{ 29 | Get biographical and Congressional role information for a particular member of Congress. 30 | } 31 | \examples{ 32 | \dontrun{ 33 | cg_memberbioroles(memberid = 'S001181') 34 | } 35 | } 36 | \references{ 37 | Congress API docs 38 | \url{https://projects.propublica.org/api-docs/congress-api/} 39 | } 40 | \seealso{ 41 | Other congress: \code{\link{cg_billscosponsor}}, 42 | \code{\link{cg_memberappear}}, 43 | \code{\link{cg_memberbystatedistrict}}, 44 | \code{\link{cg_membersleaving}}, 45 | \code{\link{cg_memberslist}}, 46 | \code{\link{cg_membersponsorcompare}}, 47 | \code{\link{cg_membervotecompare}}, 48 | \code{\link{cg_membervotepositions}}, 49 | \code{\link{cg_newmembers}}, 50 | \code{\link{cg_rollcallvote}} 51 | } 52 | \concept{congress} 53 | -------------------------------------------------------------------------------- /man/cg_memberbystatedistrict.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_memberbystatedistrict.R 3 | \name{cg_memberbystatedistrict} 4 | \alias{cg_memberbystatedistrict} 5 | \title{Get a list of the most recent new members of the current Congress.} 6 | \usage{ 7 | cg_memberbystatedistrict(chamber = NULL, state = NULL, 8 | district = NULL, key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{chamber}{One of 'house' or 'senate'.} 12 | 13 | \item{state}{Limits the list of members by state; two-letter state code (e.g., CA).} 14 | 15 | \item{district}{Limits the list of members by district (House only). If you specify 16 | a district, you must also specify a state. If the district number you 17 | specify is higher than the total number of districts for that state, 18 | a 404 response will be returned.} 19 | 20 | \item{key}{(character) your Propublica API key; pass in, or loads from 21 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 22 | for info on rate limits.} 23 | 24 | \item{...}{Curl options (debugging tools mostly) passed to 25 | \code{\link[crul]{HttpClient}}} 26 | } 27 | \value{ 28 | List of new members of he current Congress. 29 | } 30 | \description{ 31 | Get a list of the most recent new members of the current Congress. 32 | } 33 | \examples{ 34 | \dontrun{ 35 | cg_memberbystatedistrict(chamber='senate', state='NH') 36 | cg_memberbystatedistrict(chamber='senate', state='CA') 37 | cg_memberbystatedistrict(chamber='senate', state='OR') 38 | cg_memberbystatedistrict(chamber='house', state='OR', district=1) 39 | } 40 | } 41 | \references{ 42 | Congress API docs 43 | \url{https://projects.propublica.org/api-docs/congress-api/} 44 | } 45 | \seealso{ 46 | Other congress: \code{\link{cg_billscosponsor}}, 47 | \code{\link{cg_memberappear}}, 48 | \code{\link{cg_memberbioroles}}, 49 | \code{\link{cg_membersleaving}}, 50 | \code{\link{cg_memberslist}}, 51 | \code{\link{cg_membersponsorcompare}}, 52 | \code{\link{cg_membervotecompare}}, 53 | \code{\link{cg_membervotepositions}}, 54 | \code{\link{cg_newmembers}}, 55 | \code{\link{cg_rollcallvote}} 56 | } 57 | \concept{congress} 58 | -------------------------------------------------------------------------------- /man/cg_membersleaving.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_membersleaving.R 3 | \name{cg_membersleaving} 4 | \alias{cg_membersleaving} 5 | \title{Get a list of members who have left the Senate or House or have announced plans to do so.} 6 | \usage{ 7 | cg_membersleaving(congress_no = NULL, chamber = NULL, key = NULL, 8 | ...) 9 | } 10 | \arguments{ 11 | \item{congress_no}{The number of the Congress during which the members served.} 12 | 13 | \item{chamber}{One of 'house' or 'senate.} 14 | 15 | \item{key}{(character) your Propublica API key; pass in, or loads from 16 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 17 | for info on rate limits.} 18 | 19 | \item{...}{Curl options (debugging tools mostly) passed to 20 | \code{\link[crul]{HttpClient}}} 21 | } 22 | \value{ 23 | List of new members of he current Congress. 24 | } 25 | \description{ 26 | Get a list of members who have left the Senate or House or have announced plans to do so. 27 | } 28 | \examples{ 29 | \dontrun{ 30 | cg_membersleaving(congress_no = 112, chamber = 'house') 31 | } 32 | } 33 | \references{ 34 | Congress API docs 35 | \url{https://projects.propublica.org/api-docs/congress-api/} 36 | } 37 | \seealso{ 38 | Other congress: \code{\link{cg_billscosponsor}}, 39 | \code{\link{cg_memberappear}}, 40 | \code{\link{cg_memberbioroles}}, 41 | \code{\link{cg_memberbystatedistrict}}, 42 | \code{\link{cg_memberslist}}, 43 | \code{\link{cg_membersponsorcompare}}, 44 | \code{\link{cg_membervotecompare}}, 45 | \code{\link{cg_membervotepositions}}, 46 | \code{\link{cg_newmembers}}, 47 | \code{\link{cg_rollcallvote}} 48 | } 49 | \concept{congress} 50 | -------------------------------------------------------------------------------- /man/cg_memberslist.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_memberlist.R 3 | \name{cg_memberslist} 4 | \alias{cg_memberslist} 5 | \title{Get a list of members of a particular chamber in a particular Congress} 6 | \usage{ 7 | cg_memberslist(congress_no = NULL, chamber = NULL, state = NULL, 8 | district = NULL, key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{congress_no}{The number of the Congress during which the members served.} 12 | 13 | \item{chamber}{One of 'house' or 'senate.} 14 | 15 | \item{state}{Limits the list of members by state; two-letter state code (e.g., CA).} 16 | 17 | \item{district}{Limits the list of members by district (House only). If you specify 18 | a district, you must also specify a state. If the district number you 19 | specify is higher than the total number of districts for that state, 20 | a 404 response will be returned.} 21 | 22 | \item{key}{(character) your Propublica API key; pass in, or loads from 23 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 24 | for info on rate limits.} 25 | 26 | \item{...}{Curl options (debugging tools mostly) passed to 27 | \code{\link[crul]{HttpClient}}} 28 | } 29 | \value{ 30 | List of members of a particular chamber in a particular Congress. 31 | } 32 | \description{ 33 | Get a list of members of a particular chamber in a particular Congress 34 | } 35 | \examples{ 36 | \dontrun{ 37 | cg_memberslist(congress_no = 112, chamber = 'senate', state = "TX") 38 | cg_memberslist(112, 'senate', 'NH') 39 | cg_memberslist(110, 'senate', 'NH') 40 | } 41 | } 42 | \references{ 43 | Congress API docs 44 | \url{https://projects.propublica.org/api-docs/congress-api/} 45 | } 46 | \seealso{ 47 | Other congress: \code{\link{cg_billscosponsor}}, 48 | \code{\link{cg_memberappear}}, 49 | \code{\link{cg_memberbioroles}}, 50 | \code{\link{cg_memberbystatedistrict}}, 51 | \code{\link{cg_membersleaving}}, 52 | \code{\link{cg_membersponsorcompare}}, 53 | \code{\link{cg_membervotecompare}}, 54 | \code{\link{cg_membervotepositions}}, 55 | \code{\link{cg_newmembers}}, 56 | \code{\link{cg_rollcallvote}} 57 | } 58 | \concept{congress} 59 | -------------------------------------------------------------------------------- /man/cg_membersponsorcompare.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_membersponsorcompare.R 3 | \name{cg_membersponsorcompare} 4 | \alias{cg_membersponsorcompare} 5 | \title{Compare bill sponsorship between two members who served in the same Congress 6 | and chamber.} 7 | \usage{ 8 | cg_membersponsorcompare(memberid_1 = NULL, memberid_2 = NULL, 9 | congress_no = NULL, chamber = NULL, key = NULL, ...) 10 | } 11 | \arguments{ 12 | \item{memberid_1}{The member's unique ID number (alphanumeric). To find a 13 | member's ID number, get the list of members for the appropriate House 14 | or Senate. You can also use the Biographical Directory of the United 15 | States Congress to get a member's ID. In search results, each member's 16 | name is linked to a record by index ID (e.g., 17 | http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 18 | Use the index ID as member-id in your request.} 19 | 20 | \item{memberid_2}{See description for memberid_1.} 21 | 22 | \item{congress_no}{The number of the Congress during which the members served.} 23 | 24 | \item{chamber}{One of 'house' or 'senate.} 25 | 26 | \item{key}{(character) your Propublica API key; pass in, or loads from 27 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 28 | for info on rate limits.} 29 | 30 | \item{...}{Curl options (debugging tools mostly) passed to 31 | \code{\link[crul]{HttpClient}}} 32 | } 33 | \value{ 34 | Compare bill sponsorship between two members who served in the same 35 | Congress and chamber. 36 | } 37 | \description{ 38 | Compare bill sponsorship between two members who served in the same Congress 39 | and chamber. 40 | } 41 | \examples{ 42 | \dontrun{ 43 | cg_membersponsorcompare(memberid_1 = 'S001181', memberid_2 = 'A000368', 44 | congress_no = 112, chamber = 'senate') 45 | } 46 | } 47 | \references{ 48 | Congress API docs 49 | \url{https://projects.propublica.org/api-docs/congress-api/} 50 | } 51 | \seealso{ 52 | Other congress: \code{\link{cg_billscosponsor}}, 53 | \code{\link{cg_memberappear}}, 54 | \code{\link{cg_memberbioroles}}, 55 | \code{\link{cg_memberbystatedistrict}}, 56 | \code{\link{cg_membersleaving}}, 57 | \code{\link{cg_memberslist}}, 58 | \code{\link{cg_membervotecompare}}, 59 | \code{\link{cg_membervotepositions}}, 60 | \code{\link{cg_newmembers}}, 61 | \code{\link{cg_rollcallvote}} 62 | } 63 | \concept{congress} 64 | -------------------------------------------------------------------------------- /man/cg_membervotecompare.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_membervotecompare.R 3 | \name{cg_membervotecompare} 4 | \alias{cg_membervotecompare} 5 | \title{Get a list of members who have left the Senate or House or have announced plans to do so.} 6 | \usage{ 7 | cg_membervotecompare(memberid_1 = NULL, memberid_2 = NULL, 8 | congress_no = NULL, chamber = NULL, key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{memberid_1, memberid_2}{The member's unique ID number (alphanumeric). To find a 12 | member's ID number, get the list of members for the appropriate House 13 | or Senate. You can also use the Biographical Directory of the United 14 | States Congress to get a member's ID. In search results, each member's 15 | name is linked to a record by index ID (e.g., 16 | http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 17 | Use the index ID as member-id in your request.} 18 | 19 | \item{congress_no}{The number of the Congress during which the members served.} 20 | 21 | \item{chamber}{One of 'house' or 'senate.} 22 | 23 | \item{key}{(character) your Propublica API key; pass in, or loads from 24 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 25 | for info on rate limits.} 26 | 27 | \item{...}{Curl options (debugging tools mostly) passed to 28 | \code{\link[crul]{HttpClient}}} 29 | } 30 | \value{ 31 | List of new members of he current Congress. 32 | } 33 | \description{ 34 | Get a list of members who have left the Senate or House or have announced plans to do so. 35 | } 36 | \examples{ 37 | \dontrun{ 38 | cg_membervotecompare(memberid_1 = 'S001181', memberid_2 = 'A000368', 39 | congress_no = 112, chamber = 'senate') 40 | } 41 | } 42 | \references{ 43 | Congress API docs 44 | \url{https://projects.propublica.org/api-docs/congress-api/} 45 | } 46 | \seealso{ 47 | Other congress: \code{\link{cg_billscosponsor}}, 48 | \code{\link{cg_memberappear}}, 49 | \code{\link{cg_memberbioroles}}, 50 | \code{\link{cg_memberbystatedistrict}}, 51 | \code{\link{cg_membersleaving}}, 52 | \code{\link{cg_memberslist}}, 53 | \code{\link{cg_membersponsorcompare}}, 54 | \code{\link{cg_membervotepositions}}, 55 | \code{\link{cg_newmembers}}, 56 | \code{\link{cg_rollcallvote}} 57 | } 58 | \concept{congress} 59 | -------------------------------------------------------------------------------- /man/cg_membervotepositions.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_membervotepositions.R 3 | \name{cg_membervotepositions} 4 | \alias{cg_membervotepositions} 5 | \title{Get a list of members who have left the Senate or House or have announced 6 | plans to do so.} 7 | \usage{ 8 | cg_membervotepositions(memberid = NULL, key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{memberid}{The member's unique ID number (alphanumeric). To find a 12 | member's ID number, get the list of members for the appropriate House 13 | or Senate. You can also use the Biographical Directory of the United 14 | States Congress to get a member's ID. In search results, each member's 15 | name is linked to a record by index ID (e.g., 16 | http://bioguide.congress.gov/scripts/biodisplay.pl?index=C001041). 17 | Use the index ID as member-id in your request.} 18 | 19 | \item{key}{(character) your Propublica API key; pass in, or loads from 20 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 21 | for info on rate limits.} 22 | 23 | \item{...}{Curl options (debugging tools mostly) passed to 24 | \code{\link[crul]{HttpClient}}} 25 | } 26 | \value{ 27 | List of new members of he current Congress. 28 | } 29 | \description{ 30 | Get a list of members who have left the Senate or House or have announced 31 | plans to do so. 32 | } 33 | \examples{ 34 | \dontrun{ 35 | cg_membervotepositions('S001181') 36 | } 37 | } 38 | \references{ 39 | Congress API docs 40 | \url{https://projects.propublica.org/api-docs/congress-api/} 41 | } 42 | \seealso{ 43 | Other congress: \code{\link{cg_billscosponsor}}, 44 | \code{\link{cg_memberappear}}, 45 | \code{\link{cg_memberbioroles}}, 46 | \code{\link{cg_memberbystatedistrict}}, 47 | \code{\link{cg_membersleaving}}, 48 | \code{\link{cg_memberslist}}, 49 | \code{\link{cg_membersponsorcompare}}, 50 | \code{\link{cg_membervotecompare}}, 51 | \code{\link{cg_newmembers}}, 52 | \code{\link{cg_rollcallvote}} 53 | } 54 | \concept{congress} 55 | -------------------------------------------------------------------------------- /man/cg_newmembers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_newmembers.R 3 | \name{cg_newmembers} 4 | \alias{cg_newmembers} 5 | \title{Get a list of the most recent new members of the current Congress.} 6 | \usage{ 7 | cg_newmembers(key = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{key}{(character) your Propublica API key; pass in, or loads from 11 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 12 | for info on rate limits.} 13 | 14 | \item{...}{Curl options (debugging tools mostly) passed to 15 | \code{\link[crul]{HttpClient}}} 16 | } 17 | \value{ 18 | List of new members of he current Congress. 19 | } 20 | \description{ 21 | Get a list of the most recent new members of the current Congress. 22 | } 23 | \examples{ 24 | \dontrun{ 25 | cg_newmembers() 26 | } 27 | } 28 | \references{ 29 | Congress API docs 30 | \url{https://projects.propublica.org/api-docs/congress-api/} 31 | } 32 | \seealso{ 33 | Other congress: \code{\link{cg_billscosponsor}}, 34 | \code{\link{cg_memberappear}}, 35 | \code{\link{cg_memberbioroles}}, 36 | \code{\link{cg_memberbystatedistrict}}, 37 | \code{\link{cg_membersleaving}}, 38 | \code{\link{cg_memberslist}}, 39 | \code{\link{cg_membersponsorcompare}}, 40 | \code{\link{cg_membervotecompare}}, 41 | \code{\link{cg_membervotepositions}}, 42 | \code{\link{cg_rollcallvote}} 43 | } 44 | \concept{congress} 45 | -------------------------------------------------------------------------------- /man/cg_rollcallvote.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cg_rollcallvote.R 3 | \name{cg_rollcallvote} 4 | \alias{cg_rollcallvote} 5 | \title{Get a specific roll-call vote, including a complete list of member positions.} 6 | \usage{ 7 | cg_rollcallvote(congress_no = NULL, chamber = NULL, 8 | session_no = NULL, rollcall_no = NULL, key = NULL, ...) 9 | } 10 | \arguments{ 11 | \item{congress_no}{The number of the Congress during which the members served.} 12 | 13 | \item{chamber}{One of 'house' or 'senate.} 14 | 15 | \item{session_no}{1, 2, or special session number (For a detailed list of 16 | Congressional sessions, see 17 | http://www.senate.gov/reference/resources/pdf/congresses2.pdf).} 18 | 19 | \item{rollcall_no}{Integer. To get roll-call numbers, see the official sites 20 | of the US Senate 21 | (http://www.senate.gov/pagelayout/legislative/a_three_sections_with_teasers/votes.htm), 22 | and US House (http://artandhistory.house.gov/house_history/index.aspx).} 23 | 24 | \item{key}{(character) your Propublica API key; pass in, or loads from 25 | .Renviron as \code{PROPUBLICA_API_KEY}. See \code{\link{rtimes-package}} 26 | for info on rate limits.} 27 | 28 | \item{...}{Curl options (debugging tools mostly) passed to 29 | \code{\link[crul]{HttpClient}}} 30 | } 31 | \value{ 32 | Get a specific roll-call vote, including a complete list of member 33 | positions. A list with metadata about the bill, and vote results. 34 | } 35 | \description{ 36 | Get a specific roll-call vote, including a complete list of member positions. 37 | } 38 | \examples{ 39 | \dontrun{ 40 | cg_rollcallvote(congress_no = 105, chamber = 'house', session_no = 2, 41 | rollcall_no = 38) 42 | } 43 | } 44 | \references{ 45 | Congress API docs 46 | \url{https://projects.propublica.org/api-docs/congress-api/} 47 | } 48 | \seealso{ 49 | Other congress: \code{\link{cg_billscosponsor}}, 50 | \code{\link{cg_memberappear}}, 51 | \code{\link{cg_memberbioroles}}, 52 | \code{\link{cg_memberbystatedistrict}}, 53 | \code{\link{cg_membersleaving}}, 54 | \code{\link{cg_memberslist}}, 55 | \code{\link{cg_membersponsorcompare}}, 56 | \code{\link{cg_membervotecompare}}, 57 | \code{\link{cg_membervotepositions}}, 58 | \code{\link{cg_newmembers}} 59 | } 60 | \concept{congress} 61 | -------------------------------------------------------------------------------- /man/geo_search.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/geo_search.R 3 | \name{geo_search} 4 | \alias{geo_search} 5 | \title{Geographic search NYTimes API} 6 | \usage{ 7 | geo_search(name = NULL, latitude = NULL, longitude = NULL, 8 | elevation = NULL, population = NULL, country_code = NULL, 9 | country_name = NULL, admin_code1 = NULL, admin_name1 = NULL, 10 | admin_code2 = NULL, admin_name2 = NULL, admin_code3 = NULL, 11 | admin_name3 = NULL, admin_code4 = NULL, admin_name4 = NULL, 12 | feature_class = NULL, feature_class_name = NULL, 13 | feature_code_name = NULL, time_zone_id = NULL, dst_offset = NULL, 14 | gmt_offset = NULL, bounding_box = NULL, nearby = NULL, 15 | offset = NULL, limit = 100, key = NULL, ...) 16 | } 17 | \arguments{ 18 | \item{name}{(character) A displayable name for the specified place.} 19 | 20 | \item{latitude}{(numeric) The latitude of the specified place.} 21 | 22 | \item{longitude}{(numeric) The longitude of the specified place.} 23 | 24 | \item{elevation}{(character) The elevation of the specified place, in meters.} 25 | 26 | \item{population}{(numeric) Te population of the specified place.} 27 | 28 | \item{country_code}{(character) The country code, as given in ISO-3611, of the specified place.} 29 | 30 | \item{country_name}{(character) The country name of the specified place.} 31 | 32 | \item{admin_code1}{(character) A code for the first level administrative division containing 33 | the specified place. In the case of US locations this will be the containing state's postal 34 | code. All geonames admin_code1 records can be found at 35 | http://download.geonames.org/export/dump/admin1Codes.txt.} 36 | 37 | \item{admin_name1}{(character) The name of the first level administrative division containing 38 | the specified place. In the case of US locations this will be the full name of the containing 39 | State.} 40 | 41 | \item{admin_code2}{(character) A code for the second level administrative division containing 42 | the specified place. In the case of US locations this will be a code for the containing county. 43 | All geonames admin_code2 records can be found at 44 | http://download.geonames.org/export/dump/admin2Codes.txt} 45 | 46 | \item{admin_name2}{(character) The name of the second level administrative division containing 47 | the specified place. In the case of US locations this will be the name of the containing county.} 48 | 49 | \item{admin_code3}{(character) A code for the third level administrative division containing the 50 | specified place. This is infrequently used and never used for US locations.} 51 | 52 | \item{admin_name3}{(character) The name of the third level administrative division containing 53 | the specified place. This is infrequently used and never used for US locations.} 54 | 55 | \item{admin_code4}{(character) A code for the fourth level administrative division containing 56 | the specified place. This is infrequently used and never used for US locations.} 57 | 58 | \item{admin_name4}{(character) The name of the fourth level administrative division containing 59 | the specified place. This is infrequently used and never used for US locations.} 60 | 61 | \item{feature_class}{(character) The high-level feature class of the location. The feature 62 | classes are as follows: 63 | \itemize{ 64 | \item A - Administrative Boundary Features 65 | \item H - Hydrographic Features 66 | \item L - Area Features 67 | \item P - Populated Place Features 68 | \item R - Road / Railroad Features 69 | \item S - Spot Features 70 | \item T - Hypsographic Features 71 | \item U - Undersea Features 72 | \item V - Vegetation Features 73 | }} 74 | 75 | \item{feature_class_name}{(character) See list of name values associated with feature_class 76 | above.} 77 | 78 | \item{feature_code_name}{(character) A code for the type of feature represented by this geonames 79 | record. The complete list of feature codes can be found at 80 | http://download.geonames.org/export/dump/featureCodes_en.txt.} 81 | 82 | \item{time_zone_id}{(character) The time zone containing this geonames record.} 83 | 84 | \item{dst_offset}{(numeric) The offset from GMT during daylight savings time.} 85 | 86 | \item{gmt_offset}{(numeric) The offset from GMT during standard time.} 87 | 88 | \item{bounding_box}{(numeric) Vector of coordinates in the following format , 89 | ,,. For example: 90 | c(39.985417852135356,-93.18850617968747,37.84003257271992,-95.94607453906247)} 91 | 92 | \item{nearby}{(numeric) A latitude longitude pair (e.g. 41.9,12.5). When provided with this 93 | parameter, the geocodes API will return the 20 results that are geographically nearest to the 94 | specified latitude/longitude pair.} 95 | 96 | \item{offset}{(numeric) The offset in the results. BEWARE - this parameter doesn't work now 97 | (as of 2014-10-21).} 98 | 99 | \item{limit}{(numeric) If not specified 20 results are returned. Otherwise the specified number 100 | of results or a maximum of 20 results are returned. BEWARE - this parameter doesn't work now 101 | (as of 2015-08-10).} 102 | 103 | \item{key}{your New York Times API key; pass in, or loads from .Rprofile as 104 | \code{nytimes_api_key}, or from .Renviron as \code{NYTIMES_API_KEY}} 105 | 106 | \item{...}{Curl options (debugging tools mostly) passed to 107 | \link[crul:HttpClient]{crul::HttpClient}} 108 | } 109 | \description{ 110 | Geographic search NYTimes API 111 | } 112 | \details{ 113 | BEWARE: the docs are a hot mess - the README page has examples that 114 | include parameters that are not in their list of accepted query 115 | parameters. Some query parameter that used to work don't work now. There 116 | seems to be no way to get a response from them. So good luck. 117 | } 118 | \examples{ 119 | \dontrun{ 120 | geo_search(country_code = 'US', verbose = TRUE) 121 | geo_search(feature_class='P', country_code='US', population='50000_') 122 | 123 | # curl options 124 | geo_search(country_code = 'US', verbose = TRUE) 125 | } 126 | } 127 | \references{ 128 | \url{http://developer.nytimes.com}, 129 | \url{https://developer.nytimes.com/docs/geo-product/1/routes/query.json/get} 130 | } 131 | -------------------------------------------------------------------------------- /man/rtimes-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rtimes-package.R 3 | \docType{package} 4 | \name{rtimes-package} 5 | \alias{rtimes-package} 6 | \alias{rtimes} 7 | \title{rtimes} 8 | \description{ 9 | Interface to the Congress and Campaign Finance APIs from Propublica, and 10 | the Article Search and Geographic 'APIs' from the New York Times. 11 | } 12 | \details{ 13 | Backstory is that the Congress and Campaign finance APIs used to be part of 14 | NYTimes, but were taken over by Propublica. 15 | } 16 | \section{rtimes API}{ 17 | 18 | Functions that wrap these sets of APIs are prefixed by two letter 19 | acronyms fo reach API endpoint + the function name itself as for example 20 | \code{cg} + \code{fxn} 21 | 22 | \itemize{ 23 | \item as - for the Article Search API (Docs: 24 | \url{http://developer.nytimes.com/article_search_v2.json}) 25 | \item geo - for the Geographic API (Docs: 26 | \url{http://developer.nytimes.com/geo_api_v2.json}) 27 | \item cg - for the Congress API (Docs: 28 | \url{https://projects.propublica.org/api-docs/congress-api/}) 29 | \item cf - for the Campaign Finance API (Docs: 30 | \url{https://propublica.github.io/campaign-finance-api-docs/}) 31 | } 32 | 33 | See the vignette for help. 34 | } 35 | 36 | \section{Authentication}{ 37 | 38 | Get your own API keys for NYTimes APIs at 39 | \url{https://developer.nytimes.com/accounts/create} - you can use one API key for 40 | both the article search and geo NYTimes APIs. 41 | 42 | Get your Propublica API key for Congress and Campaign Finance APIs at either 43 | \url{https://www.propublica.org/datastore/api/propublica-congress-api} or 44 | \url{https://www.propublica.org/datastore/api/campaign-finance-api} - as far 45 | as I know, you can use the same key for both APIs 46 | 47 | We set up the functions so that you can put the key in your \code{.Renviron} 48 | file, which will be called on startup of R, and then you don't have to enter 49 | your API key for each run of a function. Add entries for an R session like 50 | 51 | \itemize{ 52 | \item \code{Sys.setenv(NYTIMES_API_KEY = "YOURKEYHERE")} 53 | \item \code{Sys.setenv(PROPUBLICA_API_KEY = "YOURKEYHERE")} 54 | } 55 | 56 | Or set them across sessions by putting entries in your \code{.Renviron} file 57 | like 58 | 59 | \itemize{ 60 | \item \code{NYTIMES_API_KEY=} 61 | \item \code{PROPUBLICA_API_KEY=} 62 | } 63 | 64 | You can also pass in your key in a function call, but be careful not to 65 | expose your keys in code committed to public repositories. If you do pass 66 | in a function call, use e.g., \code{Sys.getenv("NYTIMES_API_KEY")} 67 | } 68 | 69 | \section{Rate limits}{ 70 | 71 | Rate limits vary for the different APIs: 72 | 73 | \itemize{ 74 | \item Article Search API: 1/sec, 1,000/day 75 | \item Geographic API: 5/sec, 1,000/day 76 | \item Congress API: 2/sec, 5,000/day 77 | \item Campaign Finance API: 50/sec, 5,000/day 78 | } 79 | } 80 | 81 | \author{ 82 | Scott Chamberlain \email{myrmecocystus@gmail.com} 83 | } 84 | \keyword{package} 85 | -------------------------------------------------------------------------------- /rtimes.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | 17 | BuildType: Package 18 | PackageUseDevtools: Yes 19 | PackageInstallArgs: --no-multiarch --with-keep.source 20 | PackageCheckArgs: --as-cran 21 | -------------------------------------------------------------------------------- /tests/fixtures/as_search.yml: -------------------------------------------------------------------------------- 1 | http_interactions: 2 | - request: 3 | method: get 4 | uri: https://api.nytimes.com/svc/search/v2/articlesearch.json?q=bailout&page=0&begin_date=20081001&end_date=20081201&api-key=<> 5 | body: 6 | encoding: '' 7 | string: '' 8 | headers: 9 | User-Agent: libcurl/7.54.0 r-curl/3.3 crul/0.7.0 10 | Accept-Encoding: gzip, deflate 11 | Accept: application/json, text/xml, application/xml, */* 12 | response: 13 | status: 14 | status_code: '200' 15 | message: OK 16 | explanation: Request fulfilled, document follows 17 | headers: 18 | status: HTTP/1.1 200 OK 19 | date: Fri, 15 Feb 2019 17:33:59 GMT 20 | content-type: application/json;charset=UTF-8 21 | content-length: '14257' 22 | connection: keep-alive 23 | server: nginx 24 | access-control-allow-headers: Accept, Content-Type, Origin, X-Forwarded-For, 25 | X-Prototype-Version, X-Requested-With 26 | access-control-allow-methods: GET, OPTIONS 27 | access-control-allow-origin: '*' 28 | access-control-expose-headers: Content-Length, X-JSON 29 | body: 30 | encoding: UTF-8 31 | string: '{"status":"OK","copyright":"Copyright (c) 2019 The New York Times Company. 32 | All Rights Reserved.","response":{"docs":[{"web_url":"https://www.nytimes.com/2008/11/07/business/07views.html","snippet":"Governments 33 | should make the banking bailouts so unpleasant that other industries — automakers, 34 | retailers or struggling media groups — will hesitate to request the aid.","lead_paragraph":"Governments 35 | should make the banking bailouts so unpleasant that other industries — automakers, 36 | retailers or struggling media groups — will hesitate to request the aid.","print_page":"2","blog":{},"source":"The 37 | New York Times","multimedia":[],"headline":{"main":"Using Restraint With Bailouts","kicker":"Breakingviews.com","content_kicker":"Breakingviews.com","print_headline":"Using 38 | Restraint\nWith Bailouts","name":null,"seo":null,"sub":"Using Restraint With 39 | Bailouts ;"},"keywords":[{"name":"subject","value":"Subprime Mortgage Crisis","rank":1,"major":null},{"name":"subject","value":"Emergency 40 | Economic Stabilization Act (2008)","rank":2,"major":null},{"name":"organizations","value":"The 41 | Blackstone Group","rank":3,"major":null}],"pub_date":"2008-11-07T00:00:00Z","document_type":"article","news_desk":"Business","section_name":"Business 42 | Day","byline":{"original":"By HUGO DIXON and LAUREN SILVA","person":[{"firstname":"Hugo","middlename":null,"lastname":"DIXON","qualifier":null,"title":null,"role":"reported","organization":"","rank":1},{"firstname":"Lauren","middlename":null,"lastname":"SILVA","qualifier":null,"title":null,"role":"reported","organization":"","rank":2}],"organization":null},"type_of_material":"News","_id":"5479dae338f0d86bf592fedf","word_count":719,"score":0.0},{"web_url":"https://www.nytimes.com/2008/11/14/opinion/14brooks.html","snippet":"The 43 | biggest threat to a healthy economy is the politically powerful capitalists 44 | who use their influence to create a stagnant corporate welfare state.","lead_paragraph":"The 45 | biggest threat to a healthy economy is the politically powerful capitalists 46 | who use their influence to create a stagnant corporate welfare state.","print_page":"33","blog":{},"source":"The 47 | New York Times","multimedia":[{"rank":0,"subtype":"xlarge","caption":null,"credit":null,"type":"image","url":"images/2014/11/01/opinion/brooks-circular/brooks-circular-articleLarge-v2.jpg","height":588,"width":600,"legacy":{"xlargewidth":600,"xlarge":"images/2014/11/01/opinion/brooks-circular/brooks-circular-articleLarge-v2.jpg","xlargeheight":588},"subType":"xlarge","crop_name":null},{"rank":0,"subtype":"wide","caption":null,"credit":null,"type":"image","url":"images/2014/11/01/opinion/brooks-circular/brooks-circular-thumbWide-v5.jpg","height":126,"width":190,"legacy":{"wide":"images/2014/11/01/opinion/brooks-circular/brooks-circular-thumbWide-v5.jpg","widewidth":190,"wideheight":126},"subType":"wide","crop_name":null},{"rank":0,"subtype":"thumbnail","caption":null,"credit":null,"type":"image","url":"images/2014/11/01/opinion/brooks-circular/brooks-circular-thumbStandard-v5.jpg","height":75,"width":75,"legacy":{"thumbnailheight":75,"thumbnail":"images/2014/11/01/opinion/brooks-circular/brooks-circular-thumbStandard-v5.jpg","thumbnailwidth":75},"subType":"thumbnail","crop_name":null}],"headline":{"main":"Bailout 48 | to Nowhere","kicker":"Op-Ed Columnist","content_kicker":"Op-Ed Columnist","print_headline":"Bailout 49 | to Nowhere","name":null,"seo":null,"sub":null},"keywords":[{"name":"subject","value":"Automobiles","rank":1,"major":"N"},{"name":"subject","value":"Subprime 50 | Mortgage Crisis","rank":2,"major":"N"},{"name":"persons","value":"Obama, Barack","rank":3,"major":"N"},{"name":"subject","value":"Lobbying 51 | and Lobbyists","rank":4,"major":"N"},{"name":"subject","value":"United States 52 | Politics and Government","rank":5,"major":"N"},{"name":"organizations","value":"Ford 53 | Motor Co","rank":6,"major":"N"},{"name":"organizations","value":"Chrysler 54 | LLC","rank":7,"major":"N"},{"name":"organizations","value":"General Motors 55 | Corp","rank":8,"major":"N"}],"pub_date":"2008-11-18T18:04:48+0000","document_type":"article","news_desk":"Editorial","section_name":"Opinion","byline":{"original":"By 56 | DAVID BROOKS","person":[{"firstname":"David","middlename":null,"lastname":"BROOKS","qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"Op-Ed","_id":"545db51738f0d877bc740c84","word_count":826,"score":0.0,"uri":"nyt://article/d5c4cfeb-8c78-56a2-a1fc-8b206e4ceae1"},{"web_url":"https://www.nytimes.com/2008/10/05/magazine/05wwln-safire-t.html","snippet":"How 57 | can you be naked wearing shorts?","lead_paragraph":"How can you be naked wearing 58 | shorts?","print_page":"14","blog":{},"source":"The New York Times","multimedia":[],"headline":{"main":"Toxic 59 | Bailout","kicker":"On Language","content_kicker":"On Language","print_headline":"TOXIC 60 | BAILOUT","name":null,"seo":null,"sub":null},"keywords":[{"name":"subject","value":"Language 61 | and Languages","rank":1,"major":null},{"name":"subject","value":"English Language","rank":2,"major":null},{"name":"subject","value":"United 62 | States Economy","rank":3,"major":null},{"name":"subject","value":"Economic 63 | Conditions and Trends","rank":4,"major":null}],"pub_date":"2008-10-05T00:00:00Z","document_type":"article","news_desk":"Magazine","section_name":"Magazine","byline":{"original":"By 64 | WILLIAM SAFIRE","person":[{"firstname":"William","middlename":null,"lastname":"SAFIRE","qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"News","_id":"5478834638f0d854b20b3396","word_count":913,"score":0.0},{"web_url":"https://economix.blogs.nytimes.com/2008/10/28/bailout-tally/","snippet":"ProPublica 65 | has been keeping a list of the banks that have announced participation in 66 | the Treasury''s capital injection plan.","lead_paragraph":"ProPublica has 67 | been keeping an extensive list of the banks that have announced participation 68 | in the Treasury''s capital injection plan. It is called \"Bailout Bucks to 69 | Banks,\" and can be found here.","blog":{},"source":"The New York Times","multimedia":[],"headline":{"main":"Bailout 70 | Tally","kicker":"Economix","content_kicker":null,"print_headline":null,"name":null,"seo":null,"sub":null},"keywords":[{"name":"type_of_material","value":"News","rank":1,"major":null}],"pub_date":"2008-10-28T18:12:45Z","document_type":"blogpost","section_name":"Business 71 | Day","byline":{"original":"By CATHERINE RAMPELL","person":[{"firstname":"Catherine","middlename":null,"lastname":"RAMPELL","qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"Blog","_id":"4fd3925a8eb7c8105d8c4a04","word_count":60,"score":0.0},{"web_url":"https://dealbook.nytimes.com/2008/11/24/hank-greenberg-on-the-philosophy-of-bailouts/","snippet":"The 72 | government''s latest lifeline to Citigroup has reignited debates about the 73 | purpose of bailouts, and, more specifically, on the question of whether they 74 | should hurt a little (or a lot) for the entity that gets bailed out. Breakingviews 75 | is alread...","lead_paragraph":"The government''s latest lifeline to Citigroup 76 | has reignited debates about the purpose of bailouts, and, more specifically, 77 | on the question of whether they should hurt a little (or a lot) for the entity 78 | that gets bailed out.","blog":{},"source":"The New York Times","multimedia":[],"headline":{"main":"Hank 79 | Greenberg on the Philosophy of Bailouts","kicker":"DealBook","content_kicker":null,"print_headline":null,"name":null,"seo":null,"sub":null},"keywords":[{"name":"type_of_material","value":"News","rank":1,"major":null}],"pub_date":"2008-11-24T11:24:18Z","document_type":"blogpost","section_name":"Business 80 | Day","byline":{"original":"By DEALBOOK","person":[{"firstname":null,"middlename":null,"lastname":null,"qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"Blog","_id":"4fd392c88eb7c8105d8c5961","word_count":305,"score":0.0},{"web_url":"https://dealbook.nytimes.com/2008/11/07/using-restraint-with-bailouts/","snippet":"If 81 | banks can get government capital that costs as little as 5 percent a year, 82 | why shouldn''t the automakers, retailers or even struggling media groups? 83 | Unlike banks, such companies weren''t responsible for the current mess. This 84 | argument, Breakingvi...","lead_paragraph":"If banks can get government capital 85 | that costs as little as 5 percent a year, why shouldn''t the automakers, retailers 86 | or even struggling media groups?","blog":{},"source":"The New York Times","multimedia":[],"headline":{"main":"Using 87 | Restraint With Bailouts","kicker":"DealBook","content_kicker":null,"print_headline":null,"name":null,"seo":null,"sub":null},"keywords":[{"name":"type_of_material","value":"News","rank":1,"major":null}],"pub_date":"2008-11-07T06:41:58Z","document_type":"blogpost","section_name":"Business 88 | Day","byline":{"original":"By DEALBOOK","person":[{"firstname":null,"middlename":null,"lastname":null,"qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"Blog","_id":"4fd392688eb7c8105d8c4c48","word_count":367,"score":0.0},{"web_url":"https://www.nytimes.com/2008/11/18/us/politics/18web-sanger.html","snippet":"In 89 | the rush to devise a way to keep the Big Three automakers alive, it might 90 | be worth pausing for a moment to ask what this latest bailout is intended 91 | to accomplish.","lead_paragraph":"In the rush to devise a way to keep the 92 | Big Three automakers alive, it might be worth pausing for a moment to ask 93 | what this latest bailout is intended to accomplish.","blog":{},"source":"The 94 | New York Times","multimedia":[],"headline":{"main":"The Great Bailout Debate","kicker":"On 95 | Washington","content_kicker":"On Washington","print_headline":"The Great Bailout 96 | Debate","name":null,"seo":null,"sub":null},"keywords":[{"name":"subject","value":"United 97 | States Politics and Government","rank":1,"major":"N"},{"name":"subject","value":"Automobiles","rank":2,"major":"N"},{"name":"organizations","value":"House 98 | of Representatives","rank":3,"major":"N"},{"name":"organizations","value":"Senate","rank":4,"major":"N"}],"pub_date":"2008-11-18T15:57:19+0000","document_type":"article","news_desk":"National","section_name":"U.S.","subsectoinName":"Politics","byline":{"original":"By 99 | DAVID E. SANGER","person":[{"firstname":"David","middlename":"E.","lastname":"SANGER","qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"News","_id":"547a3fda38f0d803f445b561","word_count":953,"score":0.0,"uri":"nyt://article/6aa1e56e-0345-5144-bcf4-a7ddc34b16b2"},{"web_url":"https://www.nytimes.com/2008/12/01/opinion/l01citigroup.html","snippet":"To 100 | the Editor:.","lead_paragraph":"To the Editor:.","print_page":"28","blog":{},"source":"The 101 | New York Times","multimedia":[],"headline":{"main":"A Bailout Foretold","kicker":null,"content_kicker":"Letter","print_headline":"A 102 | Bailout Foretold","name":null,"seo":null,"sub":"(1 Letter)"},"keywords":[{"name":"organizations","value":"Citigroup 103 | Incorporated","rank":1,"major":null},{"name":"subject","value":"United States 104 | Economy","rank":2,"major":null},{"name":"subject","value":"Emergency Economic 105 | Stabilization Act (2008)","rank":3,"major":null},{"name":"subject","value":"Banks 106 | and Banking","rank":4,"major":null}],"pub_date":"2008-12-01T00:00:00Z","document_type":"article","news_desk":"Letters","section_name":"Opinion","type_of_material":"Letter","_id":"547aa36e38f0d803f445bdd8","word_count":226,"score":0.0},{"web_url":"https://ideas.blogs.nytimes.com/2008/11/27/how-big-is-the-bailout-this-big/","snippet":"Finance 107 | | The credit crisis bailout is now the largest outlay in American history, 108 | the author of \"Bailout Nation\" writes -- bigger than the Marshall Plan, 109 | the race to the moon, the Iraq war, the Louisiana Purchase, the S&L crisis 110 | and Vietnam war c...","lead_paragraph":"Finance | The credit crisis bailout 111 | is now the largest outlay in American history, the author of \"Bailout Nation\" 112 | writes -- bigger than the Marshall Plan, the race to the moon, the Iraq war, 113 | the Louisiana Purchase, the S&L crisis and Vietnam war combined, and more. 114 | Here are inflation-adjusted numbers. [ritholtz.com]","blog":{},"source":"The 115 | New York Times","multimedia":[],"headline":{"main":"How Big Is the Bailout? 116 | This Big.","kicker":"Idea of the Day","content_kicker":null,"print_headline":null,"name":null,"seo":null,"sub":null},"keywords":[{"name":"type_of_material","value":"News","rank":1,"major":null}],"pub_date":"2008-11-27T04:59:31Z","document_type":"blogpost","section_name":"Week 117 | in Review","byline":{"original":"By TOM KUNTZ","person":[{"firstname":"Tom","middlename":null,"lastname":"KUNTZ","qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"Blog","_id":"4fd392c78eb7c8105d8c5851","word_count":51,"score":0.0},{"web_url":"https://www.nytimes.com/2008/11/20/business/economy/20tarp.html","snippet":"Two 118 | longtime Republican lawmakers were named to the new five-member review panel 119 | set up to monitor the Bush administration’s $700 billion financial rescue 120 | plan.","lead_paragraph":"Two longtime Republican lawmakers were named to the 121 | new five-member review panel set up to monitor the Bush administration’s $700 122 | billion financial rescue plan.","print_page":"4","blog":{},"source":"The New 123 | York Times","multimedia":[],"headline":{"main":"Members of Bailout Panel Are 124 | Set","kicker":null,"content_kicker":null,"print_headline":"Members of Bailout 125 | Panel Are Set","name":null,"seo":"Republican Lawmakers to Serve on Bailout 126 | Panel","sub":null},"keywords":[{"name":"subject","value":"Emergency Economic 127 | Stabilization Act (2008)","rank":1,"major":null},{"name":"subject","value":"United 128 | States Politics and Government","rank":2,"major":null},{"name":"subject","value":"Subprime 129 | Mortgage Crisis","rank":3,"major":null}],"pub_date":"2008-11-20T00:00:00Z","document_type":"article","news_desk":"Business","section_name":"Business 130 | Day","subsectoinName":"Economy","byline":{"original":"By THE NEW YORK TIMES","person":[],"organization":"THE 131 | NEW YORK TIMES"},"type_of_material":"News","_id":"547a513938f0d803f445b6ec","word_count":72,"score":0.0}],"meta":{"hits":1276,"offset":0,"time":10}}}' 132 | recorded_at: 2019-02-15 17:34:00 GMT 133 | recorded_with: vcr/0.2.6, webmockr/0.3.4 134 | -------------------------------------------------------------------------------- /tests/fixtures/as_search_facet_dates_fl.yml: -------------------------------------------------------------------------------- 1 | http_interactions: 2 | - request: 3 | method: get 4 | uri: https://api.nytimes.com/svc/search/v2/articlesearch.json?q=bailout&fl=word_count&page=0&begin_date=20081001&end_date=20081201&api-key=<>&facet_field=section_name 5 | body: 6 | encoding: '' 7 | string: '' 8 | headers: 9 | User-Agent: libcurl/7.54.0 r-curl/3.3 crul/0.7.0 10 | Accept-Encoding: gzip, deflate 11 | Accept: application/json, text/xml, application/xml, */* 12 | response: 13 | status: 14 | status_code: '200' 15 | message: OK 16 | explanation: Request fulfilled, document follows 17 | headers: 18 | status: HTTP/1.1 200 OK 19 | date: Fri, 15 Feb 2019 17:34:01 GMT 20 | content-type: application/json;charset=UTF-8 21 | content-length: '467' 22 | connection: keep-alive 23 | server: nginx 24 | access-control-allow-headers: Accept, Content-Type, Origin, X-Forwarded-For, 25 | X-Prototype-Version, X-Requested-With 26 | access-control-allow-methods: GET, OPTIONS 27 | access-control-allow-origin: '*' 28 | access-control-expose-headers: Content-Length, X-JSON 29 | body: 30 | encoding: UTF-8 31 | string: '{"status":"OK","copyright":"Copyright (c) 2019 The New York Times Company. 32 | All Rights Reserved.","response":{"docs":[{"word_count":719,"score":0.0},{"word_count":826,"score":0.0},{"word_count":913,"score":0.0},{"word_count":60,"score":0.0},{"word_count":305,"score":0.0},{"word_count":367,"score":0.0},{"word_count":953,"score":0.0},{"word_count":226,"score":0.0},{"word_count":51,"score":0.0},{"word_count":72,"score":0.0}],"meta":{"hits":1276,"offset":0,"time":9}}}' 33 | recorded_at: 2019-02-15 17:34:01 GMT 34 | recorded_with: vcr/0.2.6, webmockr/0.3.4 35 | -------------------------------------------------------------------------------- /tests/test-all.R: -------------------------------------------------------------------------------- 1 | library("testthat") 2 | test_check("rtimes") 3 | -------------------------------------------------------------------------------- /tests/testthat/helper-rtimes.R: -------------------------------------------------------------------------------- 1 | # set up vcr 2 | library("vcr") 3 | invisible(vcr::vcr_configure( 4 | dir = "../fixtures", 5 | filter_sensitive_data = list( 6 | "<>" = Sys.getenv("NYTIMES_API_KEY"), 7 | "<>" = Sys.getenv("PROPUBLICA_API_KEY") 8 | ) 9 | )) 10 | -------------------------------------------------------------------------------- /tests/testthat/test-api_limits.R: -------------------------------------------------------------------------------- 1 | context("api_limits") 2 | 3 | # test_that("returns list with limits", { 4 | # vcr::use_cassette("api_limits", { 5 | # expect_is(api_limits("as"), "list") 6 | # expect_is(api_limits("geo"), "list") 7 | # }) 8 | # }) 9 | 10 | # test_that("fails well", { 11 | # skip_on_cran() 12 | 13 | # expect_error(api_limits(key = ""), "need an API key for NYTIMES_AS_KEY") 14 | # expect_error(api_limits(api = "foo"), "only works for") 15 | # }) 16 | -------------------------------------------------------------------------------- /tests/testthat/test-as_search.R: -------------------------------------------------------------------------------- 1 | context("as_search") 2 | 3 | key <- Sys.getenv("NYTIMES_AS_KEY") 4 | 5 | test_that("returns the correct stuff", { 6 | vcr::use_cassette("as_search", { 7 | a <- as_search(q = "bailout", begin_date = "20081001", end_date = '20081201') 8 | Sys.sleep(1) 9 | expect_is(a, "list") 10 | expect_is(a$copyright, "character") 11 | expect_match(a$copyright, "Copyright") 12 | expect_is(a$meta, "data.frame") 13 | expect_is(a$data, "tbl_df") 14 | expect_is(a$data$web_url, "character") 15 | }) 16 | 17 | vcr::use_cassette("as_search_facet_dates_fl", { 18 | b <- as_search(q = "bailout", facet_field = 'section_name', begin_date = "20081001", 19 | end_date = '20081201', fl = 'word_count') 20 | expect_is(b, "list") 21 | expect_is(b$meta, "data.frame") 22 | expect_is(b$data, "tbl_df") 23 | expect_named(b, c("copyright", "meta", "data", "facets")) 24 | expect_named(b$data, c("word_count", "score")) 25 | }) 26 | 27 | vcr::use_cassette("as_search_fq", { 28 | d <- as_search(q = "money", fq = 'The New York Times') 29 | e <- as_search(q = "money", fq = 'news_desk:("Sports" "Foreign")') 30 | 31 | expect_is(d, "list") 32 | expect_is(d$meta, "data.frame") 33 | expect_is(e$data, "tbl_df") 34 | 35 | expect_is(e, "list") 36 | expect_is(e$meta, "data.frame") 37 | expect_is(e$data, "tbl_df") 38 | }) 39 | }) 40 | 41 | test_that("fails well", { 42 | skip_on_cran() 43 | 44 | expect_error(as_search(key = key), "is missing, with no default") 45 | expect_error(as_search(q = "bailout", end_date = "tttt", key = key)) 46 | }) 47 | -------------------------------------------------------------------------------- /tests/testthat/test-geo_search.R: -------------------------------------------------------------------------------- 1 | context("geo_search") 2 | 3 | key <- Sys.getenv("NYTIMES_API_KEY") 4 | 5 | test_that("returns the correct stuff", { 6 | vcr::use_cassette("geo_search", { 7 | a <- geo_search(country_code = 'US', key = key) 8 | expect_is(a, "list") 9 | expect_is(a$copyright, "character") 10 | expect_match(a$copyright, "Copyright") 11 | expect_is(a$data, "data.frame") 12 | expect_equal(unique(a$data$country_code), "US") 13 | expect_equal(length(a), 3) 14 | expect_equal(NCOL(a$meta), 2) 15 | }) 16 | 17 | vcr::use_cassette("geo_search_params", { 18 | e <- geo_search(feature_class = 'P', country_code = 'US', population = '50000_', key = key) 19 | expect_is(e$data, "data.frame") 20 | expect_is(e, "list") 21 | expect_is(e$meta, "data.frame") 22 | expect_is(e$data, "data.frame") 23 | expect_equal(unique(e$data$country_code), "US") 24 | }) 25 | }) 26 | 27 | test_that("fails well", { 28 | skip_on_cran() 29 | 30 | # bad latitude input 31 | expect_error(geo_search(latitude = "asdf", key = key), "Internal Server Error") 32 | # bad countr name doesnt error, but returns no results 33 | expect_equal(NROW(geo_search(country_name = 45, key = key)$data), 0) 34 | }) 35 | -------------------------------------------------------------------------------- /tools/propublica.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rOpenGov/rtimes/4eaef46ca60a35c879db68edba6a697418693850/tools/propublica.jpg -------------------------------------------------------------------------------- /tools/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rOpenGov/rtimes/4eaef46ca60a35c879db68edba6a697418693850/tools/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /vignettes/figure/unnamed-chunk-9-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rOpenGov/rtimes/4eaef46ca60a35c879db68edba6a697418693850/vignettes/figure/unnamed-chunk-9-1.png -------------------------------------------------------------------------------- /vignettes/nyt_civil_rights.Rmd: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | Use case - Civil Rights Coverage in NYT over time 10 | ================================================= 11 | 12 | This is an example use of `rtimes` from [Gaurav Soodoku](http://gbytes.gsood.com/). Find the original code [at GitHub](https://github.com/soodoku/nyt-civil-rights). 13 | 14 | This example is a smaller subset of the analysis in the original code, just looking at one phrase: _Affirmative Action_ 15 | 16 | ## Installation 17 | 18 | 19 | ```r 20 | install.packages("rtimes") 21 | ``` 22 | 23 | 24 | ```r 25 | library("rtimes") 26 | ``` 27 | 28 | ## Authentication 29 | 30 | First, get an API key for the Article Search API at [developer.nytimes.com/apps/register](http://developer.nytimes.com/apps/register). Then set the key in R: 31 | 32 | 33 | ```r 34 | Sys.setenv(NYTIMES_AS_KEY = '') 35 | ``` 36 | 37 | ## Query the NYTimes Article Search API 38 | 39 | Set dates. Here, one query per year from 1881 to 1985 40 | 41 | 42 | ```r 43 | dates <- format(seq(as.Date("1880/1/1"), as.Date("2000/1/1"), by = "5 years"), "%Y%m%d") 44 | ``` 45 | 46 | Make a data.frame to put data in to 47 | 48 | 49 | ```r 50 | results <- data.frame(startDate = NA, endDate = NA, afam = NA) 51 | ``` 52 | 53 | Loop through dates 54 | 55 | 56 | ```r 57 | for (i in seq_len(length(dates) - 1)) { 58 | Sys.sleep(1) 59 | counts <- NA 60 | counts <- as_search(q = "affirmative action", 61 | begin_date = dates[i], 62 | end_date = dates[i + 1])$meta$hits[1] 63 | results[i, ] <- c(dates[i], dates[i + 1], counts) 64 | } 65 | ``` 66 | 67 | ## Plot results 68 | 69 | Load packages 70 | 71 | 72 | ```r 73 | library("lubridate") 74 | library("ggplot2") 75 | library("scales") 76 | library("grid") 77 | ``` 78 | 79 | Convert dates to class `date` 80 | 81 | 82 | ```r 83 | results$startDate <- as.Date(as.character(results$startDate), format = "%Y%m%d") 84 | results$afam <- as.numeric(results$afam) 85 | ``` 86 | 87 | Plot mentions of _Affirmative Action_ 88 | 89 | 90 | ```r 91 | ggplot(results, aes(x = startDate, y = afam)) + 92 | geom_point(size = 1, color = "#42C4C7") + 93 | geom_smooth(method = "gam", formula = y ~ s(x), aes(group = 1), 94 | size = 1, colour = "#777777", alpha = 0.05, se = FALSE) + 95 | ylab("No. Articles Containing the phrase 'Affirmative Action'") + 96 | xlab("Year") + 97 | scale_x_date(breaks = pretty_breaks(n = 10)) + 98 | theme_grey(base_size = 18) 99 | ``` 100 | 101 | ![plot of chunk unnamed-chunk-9](../../tools/unnamed-chunk-9-1.png) 102 | -------------------------------------------------------------------------------- /vignettes/rtimes_vignette.Rmd: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | rtimes vignette - R client for New York Times APIs 10 | ====== 11 | 12 | ## About the package 13 | 14 | `rtimes` is an R package to search and retrieve data from the New York Times congress API. 15 | 16 | Functions in `rtimes` that wrap these APIs are prefixed by two letter acronyms fo reach API + the function name itself, e.g.: `cg` + `fxn` 17 | 18 | * `cg` for the [Congress API](https://propublica.github.io/congress-api-docs) 19 | * `as` for the [Article Search API](http://developer.nytimes.com/article_search_v2.json) 20 | * `cf` for the [Campaign Finance API](https://propublica.github.io/campaign-finance-api-docs) 21 | * `geo` for the [Geographic API](http://developer.nytimes.com/geo_api_v2.json) 22 | 23 | Please get your own API keys at http://developer.nytimes.com/apps/register - you'll need a different key for each API. 24 | 25 | I set up the functions so that you can put the key in your `.Renviron` file, which will 26 | be called on startup of R, and then you don't have to enter your API key for each run 27 | of a function. Add entries for an R session like 28 | 29 | ``` 30 | Sys.setenv(NYTIMES_GEO_KEY = "YOURKEYHERE") 31 | Sys.setenv(NYTIMES_AS_KEY = "YOURKEYHERE") 32 | Sys.setenv(PROPUBLICA_API_KEY = "YOURKEYHERE") 33 | ``` 34 | 35 | Or set them across sessions by putting entries in your `.Renviron` file like 36 | 37 | ``` 38 | NYTIMES_GEO_KEY= 39 | NYTIMES_AS_KEY= 40 | PROPUBLICA_API_KEY= 41 | ``` 42 | 43 | You can also pass in your key in a function call, but be careful not to expose your keys in 44 | code committed to public repositories. If you do pass in a function call, use e.g., `Sys.getenv("NYTIMES_GEO_KEY")`. 45 | 46 | ## Install rtimes 47 | 48 | From CRAN 49 | 50 | 51 | ```r 52 | install.packages("rtimes") 53 | ``` 54 | 55 | Development version from GitHub 56 | 57 | 58 | ```r 59 | install.packages("devtools") 60 | devtools::install_github("ropengov/rtimes") 61 | ``` 62 | 63 | ## Load rtimes 64 | 65 | 66 | ```r 67 | library('rtimes') 68 | ``` 69 | 70 | _Note: Member ID S001181 is Jeanne Shaheen_ 71 | 72 | ## Congress API 73 | 74 | ### Bill cosponsorship data for a member 75 | 76 | 77 | ```r 78 | out <- cg_billscosponsor(memberid='S001181', type='cosponsored') 79 | out$data 80 | #> # A tibble: 20 x 26 81 | #> congress bill_id bill_type number 82 | #> 83 | #> 1 115 s756-115 s S.756 84 | #> 2 115 s1616-115 s S.1616 85 | #> 3 115 s1738-115 s S.1738 86 | #> 4 115 s581-115 s S.581 87 | #> 5 115 s1710-115 s S.1710 88 | #> 6 115 s1715-115 s S.1715 89 | #> 7 115 s1726-115 s S.1726 90 | #> 8 115 s1730-115 s S.1730 91 | #> 9 115 s1706-115 s S.1706 92 | #> 10 115 s1692-115 s S.1692 93 | #> 11 115 s1693-115 s S.1693 94 | #> 12 115 s1688-115 s S.1688 95 | #> 13 115 sres241-115 sres S.RES.241 96 | #> 14 115 s1675-115 s S.1675 97 | #> 15 115 s1654-115 s S.1654 98 | #> 16 115 sres234-115 sres S.RES.234 99 | #> 17 115 s1640-115 s S.1640 100 | #> 18 115 sres233-115 sres S.RES.233 101 | #> 19 115 s1629-115 s S.1629 102 | #> 20 115 sres230-115 sres S.RES.230 103 | #> # ... with 22 more variables: bill_uri , title , 104 | #> # cosponsored_date , sponsor_title , sponsor_id , 105 | #> # sponsor_name , sponsor_state , sponsor_party , 106 | #> # sponsor_uri , gpo_pdf_uri , congressdotgov_url , 107 | #> # govtrack_url , introduced_date , active , 108 | #> # senate_passage , cosponsors , committees , 109 | #> # primary_subject , summary , summary_short , 110 | #> # latest_major_action_date , latest_major_action 111 | ``` 112 | 113 | ### Member appearances 114 | 115 | 116 | ```r 117 | out <- cg_memberappear(memberid='S001181') 118 | out$data 119 | #> # A tibble: 20 x 5 120 | #> date 121 | #> 122 | #> 1 2010-09-29 123 | #> 2 2010-09-22 124 | #> 3 2010-09-17 125 | #> 4 2010-09-16 126 | #> 5 2010-08-03 127 | #> 6 2010-07-29 128 | #> 7 2010-07-28 129 | #> 8 2010-07-22 130 | #> 9 2010-07-21 131 | #> 10 2010-07-21 132 | #> 11 2010-07-20 133 | #> 12 2010-07-15 134 | #> 13 2010-07-15 135 | #> 14 2010-06-30 136 | #> 15 2010-06-29 137 | #> 16 2010-06-23 138 | #> 17 2010-06-17 139 | #> 18 2010-06-16 140 | #> 19 2010-06-10 141 | #> 20 2010-06-09 142 | #> # ... with 4 more variables: title , url , start_time , 143 | #> # end_time 144 | ``` 145 | 146 | ## Article search API 147 | 148 | This function in most cases outputs a series of S3 objects, one for each item 149 | found. Each element, an object of class `as_search`, is a summary of a list 150 | of data. You can `unclass()` the object if you want, or index into particular 151 | elements (see egs below). 152 | 153 | Here, we search for _bailout_ between two dates, Oct 1 2008 and Dec 1 2008 154 | 155 | 156 | ```r 157 | res <- as_search(q="bailout", begin_date = "20081001", end_date = '20081201') 158 | res$copyright 159 | #> [1] "Copyright (c) 2015 The New York Times Company. All Rights Reserved." 160 | res$meta 161 | #> # A tibble: 1 x 3 162 | #> hits offset time 163 | #> 164 | #> 1 1203 0 8 165 | res$data 166 | #> # A tibble: 10 x 19 167 | #> web_url 168 | #> * 169 | #> 1 https://dealbook.nytimes.com/2008/10/17/nader-displays-new-fervor-on-the-ba 170 | #> 2 https://dealbook.nytimes.com/2008/10/07/its-the-economy-redux/ 171 | #> 3 https://www.nytimes.com/2008/12/01/opinion/l01citigroup.html 172 | #> 4 https://www.nytimes.com/2008/12/01/business/economy/01auto.html 173 | #> 5 https://www.nytimes.com/2008/12/01/business/01tanta.html 174 | #> 6 https://www.nytimes.com/2008/12/01/business/01uaw.html 175 | #> 7 https://www.nytimes.com/2008/12/01/business/economy/01stimulus.html 176 | #> 8 https://www.nytimes.com/2008/11/30/opinion/30sun1.html 177 | #> 9 https://www.nytimes.com/2008/11/30/opinion/30boskin.html 178 | #> 10 https://www.nytimes.com/2008/11/30/business/30dealer.html 179 | #> # ... with 18 more variables: snippet , abstract , source , 180 | #> # multimedia , keywords , pub_date , 181 | #> # document_type , section_name , type_of_material , 182 | #> # `_id` , word_count , score , print_page , 183 | #> # new_desk , headline.main , headline.kicker , 184 | #> # headline.print_headline , byline.original 185 | ``` 186 | 187 | 188 | 189 | Another e.g., Search for keyword _money_, within the _Sports_ and _Foreign_ news desks 190 | 191 | 192 | ```r 193 | res <- as_search(q = "money", fq = 'news_desk:("Sports" "Foreign")') 194 | res$data 195 | #> # A tibble: 10 x 19 196 | #> web_url 197 | #> * 198 | #> 1 https://www.nytimes.com/2017/06/16/world/asia/1mdb-malaysia-najib-razak.htm 199 | #> 2 https://www.nytimes.com/2017/07/28/sports/football/giants-odell-beckham-con 200 | #> 3 https://www.nytimes.com/2017/08/04/sports/soccer/neymar-psg-soccer.html 201 | #> 4 https://www.nytimes.com/2017/08/10/sports/soccer/soccer-capital-lure-of-lon 202 | #> 5 https://www.nytimes.com/2017/07/19/world/europe/grenfell-tower-fire-donatio 203 | #> 6 https://www.nytimes.com/2017/07/12/sports/basketball/the-two-men-who-make-m 204 | #> 7 https://www.nytimes.com/2017/06/24/world/africa/angola-luanda-jose-eduardo- 205 | #> 8 https://www.nytimes.com/2017/08/04/sports/soccer/wolverhampton-jorge-mendes 206 | #> 9 https://www.nytimes.com/2017/06/15/sports/soccer/fifa-jorge-luis-arzuaga-pl 207 | #> 10 https://www.nytimes.com/2017/06/26/sports/tennis/itf-pro-circuit-wozniak-st 208 | #> # ... with 18 more variables: snippet , print_page , 209 | #> # source , multimedia , keywords , pub_date , 210 | #> # document_type , new_desk , section_name , 211 | #> # type_of_material , `_id` , word_count , score , 212 | #> # uri , headline.main , headline.print_headline , 213 | #> # headline.kicker , byline.original 214 | ``` 215 | 216 | ## Campaign Finance API 217 | 218 | 219 | Here, we search for campaign details for the 2008 cycle, with FEC ID number P80003338 220 | 221 | 222 | ```r 223 | cf_candidate_details(campaign_cycle = 2008, fec_id = 'P80003338') 224 | #> $status 225 | #> [1] "OK" 226 | #> 227 | #> $copyright 228 | #> [1] "Copyright (c) 2017 ProPublica Inc. All Rights Reserved." 229 | #> 230 | #> $data 231 | #> # A tibble: 1 x 24 232 | #> id name party 233 | #> 234 | #> 1 P80003338 OBAMA, BARACK DEM 235 | #> # ... with 21 more variables: fec_uri , committee , 236 | #> # mailing_address , mailing_city , mailing_state , 237 | #> # mailing_zip , status , total_receipts , 238 | #> # total_from_individuals , total_from_pacs , 239 | #> # total_contributions , candidate_loans , 240 | #> # total_disbursements , begin_cash , end_cash , 241 | #> # total_refunds , debts_owed , date_coverage_from , 242 | #> # date_coverage_to , independent_expenditures , 243 | #> # coordinated_expenditures 244 | ``` 245 | 246 | ## Geographic API 247 | 248 | The geographic API allows you to do geo based searches of place names. It's built on 249 | the Geonames database. 250 | 251 | Here, we search for results for locations in the US 252 | 253 | 254 | ```r 255 | geo_search(country_code = 'US') 256 | #> $copyright 257 | #> [1] "Copyright (c) 2015 The New York Times Company. All Rights Reserved." 258 | #> 259 | #> $meta 260 | #> status num_results 261 | #> 1 OK 100 262 | #> 263 | #> $data 264 | #> # A tibble: 100 x 27 265 | #> concept_id concept_name geocode_id geoname_id 266 | #> * 267 | #> 1 22456 Abilene (Tex) 4500 4669635 268 | #> 2 22460 Abingdon (Va) 7752 4743815 269 | #> 3 22480 Acadia National Park (Me) 4504 4956449 270 | #> 4 22508 Adirondack Mountains (NY) 1488 5106772 271 | #> 5 22548 Alabama 364 4829764 272 | #> 6 22556 Alaska 292 5879092 273 | #> 7 22564 Albany (NY) 464 5106834 274 | #> 8 22572 Albuquerque (NM) 1596 5454711 275 | #> 9 22576 Alcatraz (San Francisco) 7772 5322901 276 | #> 10 22580 Alexandria (Va) 4008 4744091 277 | #> # ... with 90 more rows, and 23 more variables: name , 278 | #> # latitude , longitude , elevation , population , 279 | #> # country_code , country_name , admin_code1 , 280 | #> # admin_code2 , admin_code3 , admin_code4 , 281 | #> # admin_name1 , admin_name2 , admin_name3 , 282 | #> # admin_name4 , feature_class , feature_code , 283 | #> # feature_code_name , time_zone_id , dst_offset , 284 | #> # gmt_offset , geocodes_created , geocodes_updated 285 | ``` 286 | --------------------------------------------------------------------------------