R/get_candidate_by_name.R

Defines functions get_candidate_by_name

Documented in get_candidate_by_name

#' Get Candidate Info
#' 
#' https://www.propublica.org/datastore/apis
#' HTTP Request: GET https://api.propublica.org/campaign-finance/v1/{cycle}/candidates/search
#'
#' @param NAME The first or last name of the candidate.
#' @param cycle The election cycle
#' @inheritParams get_new_members
#'
#' @return List of returned JSON from endpoint that retrieves federal candidates by last name, using a query string parameter.
#' @export
#'
#' @examples
#' \dontrun{
#' get_candidate_by_name('Wilson', 2016)
#' }
#' 
get_candidate_by_name <- function(NAME, cycle=2018, page = 1, myAPI_Key){
  API = 'campaign-finance'
  if(!validate_cycle(cycle))
    stop("Incorrect cycle")
  if(is.character(NAME)){
    query <- sprintf("%s/candidates/search.json?query=%s", cycle, NAME)
    pp_query(query, API, page = page, myAPI_Key = myAPI_Key)
  } else {stop("NAME has to be character. The first or last name of the candidate.")}
}

Try the ProPublicaR package in your browser

Any scripts or data that you put into this service are public.

ProPublicaR documentation built on Sept. 8, 2023, 5:53 p.m.