-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
121 lines (79 loc) · 2.77 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
library(shiny)
source("setup.R")
voteWidgets <- list()
colStyle <- "display: flex; flex-direction: column; justify-content: center;"
rowStyle <- "display: flex; flex-direction: rtl; justify-content: center;"
rcolumn <- function(width, ...) column(width, ..., style=paste(colStyle, "text-align: right;"))
lcolumn <- function(width, ...) column(width, ..., style=paste(colStyle, "text-align: left;"))
for (i in seq_len(nrow(parties))) {
this.party <- row.names(parties)[i]
voteWidgets[[this.party]] <- fluidRow(
rcolumn(4, strong(this.party)),
rcolumn(3, numericInput(paste0("votes", i), "", parties$Vote[i], 0, 100, 1)),
rcolumn(2, uiOutput(paste0("electorates", i))),
rcolumn(2, uiOutput(paste0("total", i))),
style=rowStyle
)
}
ui <- fluidPage(
titlePanel("New Zealand Parliament 2017"),
fluidRow(
column(6, wellPanel(
h3("Party Votes"),
fluidRow(
lcolumn(4),
lcolumn(3, h6(strong("Votes"))),
lcolumn(2, h6(strong("Electorates"), align="right")),
lcolumn(2, h6(strong("Total"), align="right")),
style=rowStyle
),
voteWidgets,
fluidRow(
rcolumn(7),
rcolumn(2, uiOutput("electoratesTotal")),
rcolumn(2, uiOutput("total")),
style=rowStyle
)
)),
column(6, wellPanel(h3("Seats in Parliament"), plotOutput("housePlot"), textOutput("test")))
)
)
server <- function(input, output, session) {
candidates <- reactive(C)
votes <- reactive({
rval <- sapply(seq_along(partyNames), function(i) input[[paste0("votes", i)]])
names(rval) <- partyNames
rval
})
electorateSeats <- reactive(
tapply(candidates()$Winner, candidates()$Party, sum)[partyNames]
)
totalSeats <- reactive({
v <- votes()
s <- electorateSeats()
x <- ifelse(v < 5 & s < 1, 0, v)
pmax(SainteLague(x), s)
})
for(i in seq_along(partyNames)) {
output[[paste0("electorates", i)]] <- eval(
substitute(
renderText(electorateSeats()[x]),
list(x=partyNames[i])
)
)
}
for(i in seq_along(partyNames)) {
output[[paste0("total", i)]] <- eval(
substitute(
renderText(totalSeats()[x]),
list(x=i)
)
)
}
output$housePlot <- renderPlot(housePlot(totalSeats()))
output$electoratesTotal <- renderUI(strong(sum(electorateSeats())))
output$total <- renderUI(strong(sum(totalSeats())))
#output$test <- renderText(totalSeats())
session$onSessionEnded(stopApp)
}
shinyApp(ui, server)