knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
The goal of cookr is to track in real time the results of the 2018 US congressional election by district using a live google spreatsheet populated by Cook political report, and add historical context of reported results using Ballotpedia.
The package locates which districts are still active and searches for adjacent districts to get their restults too, using this geojson file.
In the package there is a plotting function to vizualize the time depedent results. To create an output like this
Sticking a pin in this is to have data to model in the future expected voter turnout with higher frequency data.
You can install cookr from github with:
# install.packages("remotes") remotes::install_github("yonicd/cookr")
library(cookr)
cook_html <- httr::GET('https://docs.google.com/spreadsheets/d/1WxDaxD5az6kdOjJncmGph37z0BPNhV1fNAH_g7IkpC0/htmlview?sle=true#gid=326900537')
cook_html <- readRDS('src_data/cook_spreadsheet_2018_11_19.rds')
cook_data <- cook_html%>% cookr::fetch_spreadsheet()
Peek at Data
cook_data%>% dplyr::glimpse()
Filter to open districts
open_districts <- cook_data%>% cookr::cook_open()
open_districts%>%dplyr::select(-c(state_district,home_district))%>%knitr::kable()
Find the adjacent districts
adjacent_districts <- open_districts%>% cookr::cook_adjacent()
Combine Data
districts <- dplyr::bind_rows(open_districts,adjacent_districts)
districts%>%split(districts$home_district)%>%purrr::map(.f=function(x) x%>%dplyr::select(-c(state_district,home_district)))%>%purrr::map(knitr::kable)
Fetch Historical Results of the Districts From Ballotpedia
cook_history <- districts%>% cook_ballotpedia()
cook_history <- readRDS('src_data/cook_history_2018_11_19.rds')
Slack is defined as the historical reported votes minus the reported votes in 2018 for each election year. The 5th,50th and 95th percentiles of this distribution is shown in the table. This measurement gives a plausible range of votes remaining to count in the open districts.
cook_history%>%split(cook_history$home_district)%>%purrr::map(.f=function(x) x%>%dplyr::select(-c(state_district,home_district,history,results)))%>%purrr::map(knitr::kable)
p <- cook_history%>% cook_plot() p
ggplot2::ggsave(p,filename = sprintf('plots/cook_%s.png',strftime(Sys.Date(),format = '%Y_%m_%d'))) saveRDS(cook_html,file = sprintf('src_data/cook_spreadsheet_%s.rds',strftime(Sys.Date(),format = '%Y_%m_%d'))) saveRDS(cook_history,file = sprintf('src_data/cook_history_%s.rds',strftime(Sys.Date(),format = '%Y_%m_%d')))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.