From ff0523515f14b3c4610d7b769e166c49836d9ed7 Mon Sep 17 00:00:00 2001 From: angelocioffi Date: Thu, 9 Mar 2017 00:14:48 -0600 Subject: [PATCH 1/3] Modified tx.R to collect 2014 registration data as well as 2016 data --- r-packages/uselections/R/tx.R | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/r-packages/uselections/R/tx.R b/r-packages/uselections/R/tx.R index ac696e5..3bc48b9 100644 --- a/r-packages/uselections/R/tx.R +++ b/r-packages/uselections/R/tx.R @@ -9,10 +9,10 @@ loadTexas <- function() { countyNameFIPSMapping <- getCountyNameFIPSMapping('48') %>% mutate(CountyName=toupper(CountyName)) - page <- read_html("https://www.sos.state.tx.us/elections/historical/nov2016.shtml") - tables <- page %>% html_nodes("table") %>% html_table(fill=TRUE) + page.2016 <- read_html("https://www.sos.state.tx.us/elections/historical/nov2016.shtml") + tables.2016 <- page.2016 %>% html_nodes("table") %>% html_table(fill=TRUE) - df <- tables[[1]] %>% + df.2016 <- tables.2016[[1]] %>% filter(`County Name` != 'Statewide Total') %>% select(CountyName=`County Name`, N=`Voter Registration`) %>% mutate_each(funs(gsub(x=., pattern=",", replacement=""))) %>% @@ -25,6 +25,25 @@ loadTexas <- function() { select(-CountyName) %>% as_tibble() + + page.2014 <- read_html("https://www.sos.state.tx.us/elections/historical/nov2014.shtml") + tables.2014 <- page.2014 %>% html_nodes("table") %>% html_table(fill=TRUE) + + df.2014 <- tables.2014[[1]] %>% + filter(`County Name` != 'Statewide Total') %>% + select(CountyName=`County Name`, N=`Voter Registration`) %>% + mutate_each(funs(gsub(x=., pattern=",", replacement=""))) %>% + mutate(D=NA, G=NA, L=NA, R=NA, O=NA) %>% + mutate(Year = 2014, Month = 11) %>% # Hardcode until we add historical data + select(CountyName, D, G, L, R, N, O, Year, Month) %>% + mutate_each("as.integer", -CountyName) %>% + mutate(CountyName=ifelse(CountyName=='LASALLE', 'LA SALLE', CountyName)) %>% + left_join(countyNameFIPSMapping, by=c("CountyName"="CountyName")) %>% + select(-CountyName) %>% + as_tibble() + + df <- rbind(df.2016, df.2014) + df } From 013fe006feef8d7696b1c9d51bcc9eeb84d13558 Mon Sep 17 00:00:00 2001 From: angelocioffi Date: Thu, 9 Mar 2017 01:05:54 -0600 Subject: [PATCH 2/3] Added historical data from every november election from 2008 to 2016 --- r-packages/uselections/R/tx.R | 58 +++++++++++++++-------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/r-packages/uselections/R/tx.R b/r-packages/uselections/R/tx.R index 3bc48b9..7c31ccc 100644 --- a/r-packages/uselections/R/tx.R +++ b/r-packages/uselections/R/tx.R @@ -9,40 +9,32 @@ loadTexas <- function() { countyNameFIPSMapping <- getCountyNameFIPSMapping('48') %>% mutate(CountyName=toupper(CountyName)) - page.2016 <- read_html("https://www.sos.state.tx.us/elections/historical/nov2016.shtml") - tables.2016 <- page.2016 %>% html_nodes("table") %>% html_table(fill=TRUE) - - df.2016 <- tables.2016[[1]] %>% - filter(`County Name` != 'Statewide Total') %>% - select(CountyName=`County Name`, N=`Voter Registration`) %>% - mutate_each(funs(gsub(x=., pattern=",", replacement=""))) %>% - mutate(D=NA, G=NA, L=NA, R=NA, O=NA) %>% - mutate(Year = 2016, Month = 11) %>% # Hardcode until we add historical data - select(CountyName, D, G, L, R, N, O, Year, Month) %>% - mutate_each("as.integer", -CountyName) %>% - mutate(CountyName=ifelse(CountyName=='LASALLE', 'LA SALLE', CountyName)) %>% - left_join(countyNameFIPSMapping, by=c("CountyName"="CountyName")) %>% - select(-CountyName) %>% - as_tibble() - - - page.2014 <- read_html("https://www.sos.state.tx.us/elections/historical/nov2014.shtml") - tables.2014 <- page.2014 %>% html_nodes("table") %>% html_table(fill=TRUE) - - df.2014 <- tables.2014[[1]] %>% - filter(`County Name` != 'Statewide Total') %>% - select(CountyName=`County Name`, N=`Voter Registration`) %>% - mutate_each(funs(gsub(x=., pattern=",", replacement=""))) %>% - mutate(D=NA, G=NA, L=NA, R=NA, O=NA) %>% - mutate(Year = 2014, Month = 11) %>% # Hardcode until we add historical data - select(CountyName, D, G, L, R, N, O, Year, Month) %>% - mutate_each("as.integer", -CountyName) %>% - mutate(CountyName=ifelse(CountyName=='LASALLE', 'LA SALLE', CountyName)) %>% - left_join(countyNameFIPSMapping, by=c("CountyName"="CountyName")) %>% - select(-CountyName) %>% - as_tibble() + df.list = list() + df.iterator <- 1 + #start at 2008 + for(i in seq(2008, 2016, 2)) + { + page <- read_html(paste("https://www.sos.state.tx.us/elections/historical/nov", i, ".shtml", sep="")) + tables <- page %>% html_nodes("table") %>% html_table(fill=TRUE) + + df.year.i <- tables[[1]] %>% + filter(`County Name` != 'Statewide Total') %>% + select(CountyName=`County Name`, N=`Voter Registration`) %>% + mutate_each(funs(gsub(x=., pattern=",", replacement=""))) %>% + mutate(D=NA, G=NA, L=NA, R=NA, O=NA) %>% + mutate(Year = i, Month = 11) %>% # Hardcode until we add historical data + select(CountyName, D, G, L, R, N, O, Year, Month) %>% + mutate_each("as.integer", -CountyName) %>% + mutate(CountyName=ifelse(CountyName=='LASALLE', 'LA SALLE', CountyName)) %>% + left_join(countyNameFIPSMapping, by=c("CountyName"="CountyName")) %>% + select(-CountyName) %>% + as_tibble() + + df.list[[df.iterator]] <- df.year.i + df.iterator <- df.iterator + 1 + } - df <- rbind(df.2016, df.2014) + df <- do.call(rbind, df.list) df From cf6d5ff04926687239d5d066a9991517044957f6 Mon Sep 17 00:00:00 2001 From: angelocioffi Date: Fri, 10 Mar 2017 21:11:10 -0600 Subject: [PATCH 3/3] Validate that the user working directory is correct before running loadTexas() --- r-packages/uselections/R/tx.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/r-packages/uselections/R/tx.R b/r-packages/uselections/R/tx.R index 7c31ccc..dba22a6 100644 --- a/r-packages/uselections/R/tx.R +++ b/r-packages/uselections/R/tx.R @@ -5,7 +5,9 @@ #' @importFrom tibble as_tibble #' @export loadTexas <- function() { - + #must set working directory to "election-transparency/r-packages/uselections" + stopifnot(basename(getwd()) == "uselections") + countyNameFIPSMapping <- getCountyNameFIPSMapping('48') %>% mutate(CountyName=toupper(CountyName))