knitr::opts_chunk$set( comment = "#>", collapse = TRUE, warning = FALSE, message = FALSE )
rtimes
is an R package to search and retrieve data from the New York Times congress API.
Functions in rtimes
that wrap these APIs are prefixed by two letter acronyms fo reach API + the function name itself, e.g.: cg
+ fxn
cg
for the Congress APIas
for the Article Search APIcf
for the Campaign Finance APIgeo
for the Geographic APIPlease get your own API keys at https://developer.nytimes.com/ - you can use one API key for all their APIs.
I set up the functions so that you can put the key in your .Renviron
file, which will
be called on startup of R, and then you don't have to enter your API key for each run
of a function. Add entries for an R session like
Sys.setenv(NYTIMES_API_KEY = "YOURKEYHERE") Sys.setenv(PROPUBLICA_API_KEY = "YOURKEYHERE")
Or set them across sessions by putting entries in your .Renviron
file like
NYTIMES_API_KEY=<yourkey> PROPUBLICA_API_KEY=<yourkey>
You can also pass in your key in a function call, but be careful not to expose your keys in
code committed to public repositories. If you do pass in a function call, use e.g., Sys.getenv("NYTIMES_API_KEY")
.
From CRAN
install.packages("rtimes")
Development version from GitHub
install.packages("devtools") devtools::install_github("ropengov/rtimes")
library('rtimes')
Note: Member ID S001181 is Jeanne Shaheen
out <- cg_billscosponsor(memberid='S001181', type='cosponsored') out$data
out <- cg_memberappear(memberid='S001181') out$data
This function in most cases outputs a series of S3 objects, one for each item
found. Each element, an object of class as_search
, is a summary of a list
of data. You can unclass()
the object if you want, or index into particular
elements (see egs below).
Here, we search for bailout between two dates, Oct 1 2008 and Dec 1 2008
res <- as_search(q="bailout", begin_date = "20081001", end_date = '20081201') res$copyright res$meta res$data
Sys.sleep(3)
Another e.g., Search for keyword money, within the Sports and Foreign news desks
res <- as_search(q = "money", fq = 'news_desk:("Sports" "Foreign")') res$data
Here, we search for campaign details for the 2008 cycle, with FEC ID number P80003338
cf_candidate_details(campaign_cycle = 2008, fec_id = 'P80003338')
The geographic API allows you to do geo based searches of place names. It's built on the Geonames database.
Here, we search for results for locations in the US
geo_search(country_code = 'US')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.