This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
168 lines (110 loc) · 5.57 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# shiny app
# voting results canton zurich
library(jsonlite)
library(purrr)
library(dplyr)
library(tidyr)
library(sf)
library(leaflet)
library(shiny)
library(shinythemes)
library(DT)
library(tmap)
library(ggplot2)
library(curl)
urls <- jsonlite::fromJSON("https://opendata.swiss/api/3/action/package_show?id=echtzeitdaten-am-abstimmungstag")
datevote <- substr(urls$result$resources$download_url,39,48)
# urls$result$resources$download_url[1]
gemeinden<- sf::read_sf("GEN_A4_GEMEINDEN_SEEN_2018_F", stringsAsFactors = FALSE) %>% select(BFS)
# Sidebar with a slider input for number of bins
ui <- fluidPage(
tags$header(tags$style(".navbar-header { font-family: Arial Black;}"),
tags$style(" .h1, .h2, .h3, .h4, .h5, .h6, h1, h2, h3, h4, h5, h6 { font-family: Arial Black;}")),
navbarPage(theme = shinytheme("cerulean"),
title= "ZH Vote",
tabPanel("Abstimmungstermin",
fluidRow(
column(4, selectizeInput('urlselect',
label= 'Abstimmungstermin auswählen',
choices = datevote,
selected = "2016_09_25"))),
plotOutput("histogram", width = "100%"),
DT::dataTableOutput("table")
),
tabPanel("Karte",
br(),
# Show a plot of the generated distribution
fluidRow(
column(4, selectInput('topicselect',
label= 'Vorlage auswählen',""))),
# Show a plot of the generated distribution
mainPanel(
leaflet::leafletOutput("mapview")
)
),
tabPanel("Über",
mainPanel(
h3("Open Data : Abstimmungsresultate via Echtzeit-Webservice"),
"Diese Shiny-App ist auf dem Echtzeit Abstimmungsdaten-Webservice des Kantons Zürich aufgebaut. Sie wird im Vorfeld von Abstimmungsterminen automatisch via opendata.swiss DCAT Action API aktualisiert.",
br(),
a("https://opendata.swiss/de/dataset/echtzeitdaten-am-abstimmungstag")
)
)))
# Define server logic required to draw a histogram
server <- function(input, output,session) {
data <- reactive({jsonlite::fromJSON(paste0( "https://www.wahlen.zh.ch/abstimmungen/",input$urlselect,"/viewer_download.php"))})
# transform nested list into dataframe
datanew <- reactive({
nd <- data() %>%
purrr::map_dfr(bind_rows) %>%
tidyr::unnest(VORLAGEN)
nd <- nd %>%
dplyr::mutate_at(vars(JA_STIMMEN_ABSOLUT,NEIN_STIMMEN_ABSOLUT,JA_PROZENT,STIMMBETEILIGUNG),as.numeric) %>%
dplyr::group_by(BFS,VORLAGE_NAME,VORLAGE_ID) %>%
dplyr::summarize(ja_anteil=round(sum(JA_STIMMEN_ABSOLUT,na.rm=T)/sum(JA_STIMMEN_ABSOLUT+NEIN_STIMMEN_ABSOLUT,na.rm=T)*100,1))
dplyr::inner_join(gemeinden,nd, by=c("BFS"))
})
# data for the leaflet map, filtered by the selected votation
mapdata <- reactive({
datanew() %>% dplyr::filter(VORLAGE_NAME==input$topicselect)
})
# votations on the selected vote
vorlagen <- reactive({ unique(datanew()$VORLAGE_NAME) })
observe({
updateSelectInput(session, "topicselect",
choices = vorlagen())})
#
output$mapview <- renderLeaflet({
# m1 <-mapview::mapview(mapdata(), zcol = "ja_anteil", at = seq(0, 100, 5), legend = TRUE)
#
# m1@map
tm <- tm_basemap(leaflet::providers$Stamen.TerrainBackground) +
tm_shape(mapdata()) +
tm_polygons(col = "ja_anteil", palette = "-Blues") +
tm_tiles(leaflet::providers$Stamen.TonerLabels, group = "Labels")
tmap_leaflet(tm)
})
output$histogram <- renderPlot(ggplot(datanew()) +
geom_histogram(aes(x=ja_anteil),fill="steelblue")+
facet_wrap(~VORLAGE_NAME)+
theme_minimal()+
geom_vline(xintercept=50)+
scale_x_continuous(limits = c(0, 100),breaks=seq(0,100,10))+
annotate("rect", xmin=50, xmax=100, ymin=0, ymax=Inf,fill="steelblue",alpha=0.3)+
annotate("rect", xmin=0, xmax=50, ymin=0, ymax=Inf,fill="coral",alpha=0.3)+
annotate("text", x=80,y=70,label = "bold(JA)", colour = "white",parse = TRUE,size = 9)+
annotate("text", x=20,y=70,label = "bold(NEIN)", colour = "white",parse = TRUE,size = 9)+
labs(x="Ja-Anteil (%)",y="Anzahl Gemeinden"),
width = "auto", height = "auto", res = 72)
output$table <- DT::renderDataTable(DT::datatable({
datanew() %>%
dplyr::mutate(Status=ifelse(ja_anteil>0, "ausgezählt","nicht ausgezählt")) %>%
dplyr::group_by(VORLAGE_NAME,Status) %>%
dplyr::summarize(Gebiete=n()) %>%
sf::st_set_geometry(NULL) %>%
dplyr::rename(Vorlage=VORLAGE_NAME)
}, options = list(paging = FALSE))
)
}
# Run the application
shinyApp(ui = ui, server = server)