MyNixOS website logo
Description

Wrapper for the Project 'VoteSmart' API.

An R interface to the Project 'VoteSmart'<https://justfacts.votesmart.org/> API.

galaxy_gif

check CRANstatus

votesmart deck

This package is a wrapper around the VoteSmartAPI written by your friendly neighborhood progressive tech organization, 🌟 Deck Technologies 🌟. Feel free to use this package in any way you like.

VoteSmart provides information on US political candidates’ positions on issues, votes on bills, and ratings by third party organizations, among other data.

Installation

install.packages("votesmart")

Or the development version:

devtools::install_github("decktools/votesmart", build_vignettes = TRUE)

API Keys

You’ll need a VoteSmart API key in order to use this package. You can register for one here.

Store your key in an environment variable named VOTESMART_API_KEY with

Sys.setenv(VOTESMART_API_KEY = "<your_key>")

You can check that it’s there with

Sys.getenv("VOTESMART_API_KEY")

This package never stores your key in your R session’s global environment.

An Example

VoteSmart collects ratings on various issues that Special Interest Groups (SIGs) give to political candidates.

Let’s say we want to know how Elizabeth Warren tends to be rated on a few issues.

library(votesmart)
suppressPackageStartupMessages(library(dplyr))
conflicted::conflict_prefer("filter", "dplyr")
#> [conflicted] Will prefer dplyr::filter over any other package.

We’ll first want to know what her VoteSmart candidate_id is. We can search for her using candidates_get_by_lastname:

warrens <-
  candidates_get_by_lastname(
    "warren",
    election_years = 2012
  )
#> Requesting data for {last_name: warren, election_year: 2012, stage_id: }.

knitr::kable(warrens)
candidate_idfirst_namenick_namemiddle_namelast_namesuffixtitleballot_namestage_idelection_yearpreferred_nameelection_partieselection_statuselection_stageelection_district_idelection_district_nameelection_officeelection_office_idelection_state_idelection_office_type_idelection_specialelection_dateoffice_partiesoffice_statusoffice_district_idoffice_district_nameoffice_state_idoffice_idoffice_nameoffice_type_idrunning_mate_idrunning_mate_name
139104AdamNALeeWarrenNANAAdam Lee Warren2012AdamRepublicanLostPrimaryNANAAttorney General12MOSFALSE08/07/2012NANANANANANANANANANA
103860DennisNANAWarrenNANADennis C. Warren2012DennisRepublicanWithdrawnGeneral2844616State Senate9IDLFALSE11/06/2012NANANANANANANANANANA
141272ElizabethNAAnnWarrenNASenatorElizabeth A. Warren2012ElizabethDemocraticWonGeneralNANAU.S. Senate6MACFALSE11/06/2012Democraticactive20512SrMA6U.S. SenateCNANA
117839HarryNAJosephWarrenNARepresentativeHarry Warren2012HarryRepublicanWonGeneral2552077State House8NCLFALSE11/06/2012Republicanactive2551976NC8State HouseLNANA
138202PeteNANAWarrenNANAPete Warren2012PeteRepublicanRemovedPrimary2184230State House8FLLFALSE08/14/2012NANANANANANANANANANA
137066StephenNANAWarrenNANAStephen Warren2012StephenRepublicanLostPrimary2786522BState House8IDLFALSE05/15/2012NANANANANANANANANANA
135832TomNANAWarrenNANATom Warren2012TomDemocraticLostGeneral2578276State House8OHLFALSE11/06/2012NANANANANANANANANANA
139311WesleyNAG.WarrenNANAWesley G. Warren2012WesleyRepublicanLostGeneral2187462State House8FLLFALSE11/06/2012NANANANANANANANANANA

Filtering to her first name and taking her candidate_id, we can now grab Warren’s ratings by all SIGs with rating_get_candidate_ratings.

(id <-
  warrens %>%
  filter(first_name == "Elizabeth") %>%
  pull(candidate_id)
)
#> [1] "141272"

ratings <-
  rating_get_candidate_ratings(
    candidate_ids = id,
  )
#> Requesting data for {candidate_id: 141272, sig_id: }.

knitr::kable(ratings %>% sample_n(3))
rating_idcandidate_idsig_idratingrating_nametimespanrating_textcategory_id_1category_name_1category_id_2category_name_2category_id_3category_name_3category_id_4category_name_4category_id_5category_name_5category_id_6category_name_6category_id_7category_name_7category_id_8category_name_8category_id_9category_name_9
86151412721161100Positions2014Senator Elizabeth Warren supported the interests of the American Federation of Labor and Congress of Industrial Organizations (AFL-CIO) 100 percent in 2014.43Labor UnionsNANANANANANANANANANANANANANANANA
10219141272241218Lifetime Positions2016Senator Elizabeth Warren supported the interests of the Conservative Review 18 percent in 2016.17ConservativeNANANANANANANANANANANANANANANANA
970514127219851Positions2017-2018Senator Elizabeth Warren supported the interests of the NumbersUSA 1 percent in 2017-2018.40ImmigrationNANANANANANANANANANANANANANANANA

And compute on them:

ratings %>%
  filter(
    category_name_1 %in%
      c(
        "Environment",
        "Fiscally Conservative",
        "Education",
        "Civil Liberties and Civil Rights",
        "Campaign Finance"
      )
  ) %>%
  group_by(category_name_1) %>%
  summarise(
    avg_rating = mean(as.numeric(rating), na.rm = TRUE)
  ) %>%
  arrange(category_name_1)
#> # A tibble: 5 × 2
#>   category_name_1                  avg_rating
#>   <chr>                                 <dbl>
#> 1 Campaign Finance                     100   
#> 2 Civil Liberties and Civil Rights      86.6 
#> 3 Education                             89.3 
#> 4 Environment                           87.2 
#> 5 Fiscally Conservative                  8.78

For more in-depth examples of how these all fit together, check out the vignette with:

vignette("votesmart")

Available Functions

These functions are named after the snake_cased version of the API endpoints.

If you see an endpoint you want to be made available in this package that isn’t yet, feel free to submit an issue or a pull request!

Summary of Functions

candidates_get_by_lastname

Get a dataframe of candidates given a vector of last_names, election_years (optional, defaulting to current year), and stage_ids (optional)

candidates_get_by_levenshtein

Get a dataframe of fuzzy-matched candidates given a vector of last_names, election_years (optional), and stage_ids (optional)

candidates_get_by_office_state

Get a dataframe of candidates by the state in which they ran for office given a vector of state_ids (optional), office_ids, and election_years (optional)

election_get_election_by_year_state

Get a dataframe of election ids and their attributes given a vector of years and state_ids (optional)

measure_get_measures

Get a dataframe of ballot measure attributes given a measure_id

measure_get_measures_by_year_state

Get a dataframe of ballot measure ids and their attributes given a vector of years and state_ids (optional)

office_get_levels

Get the VoteSmart office_level_ids and their associated names (federal, state, local)

office_get_offices_by_level

Get office_ids and their associated names (e.g. "President") for a given office_level_id

rating_get_candidate_ratings

Get SIG (Special Interest Group) ratings for candidates given a candidate_id and a sig_id (optional)

rating_get_categories

Get rating category_ids and their associated names (e.g. "Abortion", "Environment") given a vector of state_ids (optional)

rating_get_sig

Get information about a vector of SIGs (Special Interest Groups) given a sig_id

rating_get_sig_list

Get a dataframe of SIG (Special Interest Group) given a rating category_id and a state_id (optional)

votes_get_by_official

Get a dataframe of the way officials have voted on bills given a candidate_id, an office_id (optional), a category_id (optional) and a year the vote occurred (optional)

Package Data

Only a subset of all of the VoteSmart endpoints have yet been made available through this package.

You can see a full dataframe of the VoteSmart endpoints and their associated arguments with

data("endpoint_input_mapping")

or

data("endpoint_input_mapping_nested")

Other Details

  • This package currently contains no rate limiting infrastructure as there is very little information about what rate limits VoteSmart imposes, if any

  • The VoteSmart API does not allow for bulk requests, i.e. a single request can only contain one value for each parameter

    • The functions in this package allow multiple inputs to be specified for each argument, but requests are sent one at a time for each combination of inputs

Feel free to reach out in the Issues with any bugs or feature requests! 💫

Metadata

Version

0.1.2

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows