-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
136 lines (128 loc) · 5.29 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
library(shiny)
library(shinyjs)
library(DT)
load("reapp.RData")
source("apportion.R")
source("chooser.R")
states <- states[complete.cases(states), ]
min_seats <- 50
max_seats <- 1e5
ui <- fluidPage(
useShinyjs(),
titlePanel("Reapportionment Simulator"),
sidebarLayout(
sidebarPanel(
numericInput("seats",
sprintf("Number of House seats (%d-%d)",
min_seats,
max_seats),
435,
min = min_seats,
max = max_seats),
selectInput("year",
"Population Year",
choices = sort(unique(states[["Year"]])),
selected = "2010"),
uiOutput("add_state"),
"Population data from:",
HTML("<a href = https://www.census.gov/data/tables/time-series/demo/popest/2010s-state-total.html>www.census.gov</a>")
),
mainPanel(
tabsetPanel(
tabPanel("Apportionment",
"Apportionment formula:",
HTML("<a href = https://www.everycrsreport.com/files/20130802_R41357_06aeca1ea6c97545c522ec0aedf047b705d37f69.html>www.everycrsreport.com</a>"),
DT::dataTableOutput("apportionment")),
tabPanel("Historical Election Results",
selectInput("election",
"Election Year",
choices = unique(pres_results[["Year"]]),),
uiOutput("award_pr"),
verbatimTextOutput("election_results"),
DT::dataTableOutput("er"),
"Election data from:",
HTML("<a href = https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/42MVDX>dataverse.harvard.edu</a>")
),
tabPanel("Election Simulator",
uiOutput("chooser"),
verbatimTextOutput("custom_election")
)
))
)
)
server <- function(input, output, session) {
state_data <- reactive({
year <- input[["year"]]
droplevels(states[states[["Year"]] == year, ])
})
election_results <- reactive({
year <- input[["election"]]
pres_results[pres_results[["Year"]] == year, ]
})
app <- reactive({
apportion(states = state_data(),
nseats = min(max(input[["seats"]], min_seats, na.rm = TRUE), max_seats),
new_states = input[["new_states"]])
})
output[["add_state"]] <- renderUI({
# ch <- setdiff(state_data()[["State"]], state_list)
ch <- setdiff(states[["State"]], state_list)
checkboxGroupInput("new_states", label = "New States", choices = ch)
})
output[["apportionment"]] <- DT::renderDataTable({
DT::datatable(app(),
extensions = c("FixedHeader", "Buttons"),
rownames = FALSE,
options = list(
pageLength = nrow(app()),
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
fixedHeader = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'print')
)) %>%
formatPercentage(c("Population %", "Seat %", "Electoral Vote %", "Rep Factor", "EV Factor"), digits = 2) %>%
formatRound("Population", mark = ",", digits = 0)
})
output[["election_results"]] <- renderPrint({
prw <- input[["pr_win"]]
get_ev_results(election_results(), app(), prw)
})
output[["award_pr"]] <- renderUI({
if ("Puerto Rico" %in% input[["new_states"]]) {
choices <- sort(unique(election_results()[["Winner"]]))
radioButtons("pr_win", "Award Puerto Rico to:", choices, inline = TRUE)
}
})
output[["chooser"]] <- renderUI({
er <- election_results()
if ("Puerto Rico" %in% input[["new_states"]]) {
new_dat <- list(Year = 0, State = "Puerto Rico", Winner = min(er[["Winner"]]))
er <- rbind(er, as.data.frame(new_dat))
}
st <- with(er, split(State, Winner))
candidate <- names(st)
chooserInput("assign_state", candidate[[1]], candidate[[2]],
st[[1]],
st[[2]],
size = 20,
multiple = TRUE)
})
output[["custom_election"]] <- renderPrint({
er <- election_results()
st <- with(er, split(State, Winner))
st[[1]] <- c(st[[1]], "Puerto Rico")
candidate <- names(st)
l <- input[["assign_state"]][["left"]]
r <- input[["assign_state"]][["right"]]
er2 <- data.frame(State = c(l, r),
Winner = ifelse(c(l, r) %in% l,
candidate[[1]],
candidate[[2]]))
get_ev_results(er2, app(), NULL)
})
}
shinyApp(ui = ui, server = server)