-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
164 lines (127 loc) · 4.57 KB
/
Copy pathserver.R
File metadata and controls
164 lines (127 loc) · 4.57 KB
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
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(sp)
library(leaflet)
library(rgdal)
library(htmlTable)
partier = c("MP","S","V","SD","M","C","FP","KD")
valen = c("Riksdag","Landsting","Kommun")
upplosningar = c("Kommun","Valdistrikt")
shinyServer(function(input, output) {
#
geofilename = reactive({
if(input$upplosning == "Kommun") {
"Data/geodataKommun.rds"
} else if(input$upplosning == "Valdistrikt") {
"Data/geodataValdistrikt.rds"
}
})
geodata = reactive({
readRDS(geofilename())
})
electionfilename = reactive({
if(input$upplosning == "Kommun") {
if(input$val == "Riksdag") "Data/riksdagsvalperkommun.rds"
else if(input$val == "Landsting") "Data/landstingsvalperkommun.rds"
else if(input$val == "Kommun") "Data/kommunvalperkommun.rds"
} else if(input$upplosning == "Valdistrikt") {
if(input$val == "Riksdag") "Data/riksdagsvalpervaldistrikt.rds"
else if(input$val == "Landsting") "Data/landstingsvalpervaldistrikt.rds"
else if(input$val == "Kommun") "Data/kommunvalpervaldistrikt.rds"
}
})
electiondata = reactive({
df = readRDS(electionfilename())
})
polydata = reactive({
sp::merge(x = geodata(), y = electiondata(),
all.x = FALSE, all.y = FALSE,
by.x = 'KEY', by.y = 'KEY',
duplicateGeoms = FALSE)
})
mapToDraw = reactive({
str = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0";
pd = spTransform(polydata(),CRS(str))
d = as.data.frame(pd)
values = d[[input$parti]];
pal <- colorNumeric(
palette = "magma",
#domain = c(0,max(d[,pind])))
domain = values)
tempnamn = as.character(d$NAMN)
Encoding(tempnamn) = "latin1"
labels <- sprintf(
"<head><meta charset='UTF-8'></head>
<strong>%s</strong><br/>%g %",
tempnamn, values
) %>% lapply(htmltools::HTML)
leaflet() %>%
addTiles() %>%
addPolygons(data = pd,
weight=1,
color = ~pal(values),
fillOpacity = 0.5,
highlight = highlightOptions(
weight = 3,
color = "#666",
dashArray = "",
fillOpacity = 0.8,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto"),
smoothFactor = 1
) %>%
addLegend("bottomright",
pal = pal,
values = values,
title = "Valresultat",
labFormat = labelFormat(suffix = " %"),
opacity = 1)
})
output$map <- renderLeaflet({
mTmp=mapToDraw()
})
observeEvent(input$map_shape_click, {
#Not working
#return()
click <- input$map_shape_click
if(is.null(click))
return()
str = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0";
pd = spTransform(polydata(),CRS(str))
#pulls lat and lon from shiyn click event
lat <- click$lat
lon <- click$lng
#puts lat and lon for click point into its own data frame
coords <- as.data.frame(cbind(lon, lat))
#converts click point coordinate data frame into SP object, sets CRS
point <- SpatialPoints(coords)
proj4string(point) <- CRS(str)
#retrieves country in which the click point resides, set CRS for country
selected <- pd[point,]
dselected = as.data.frame(selected)
proj4string(selected) <- CRS(str)
parti = gsub(".proc","",partier)
resultat = as.vector(dselected[c(3:length(dselected))],mode='numeric')
df = data.frame(resultat
)
names(df) = as.character(dselected$NAMN)
rownames(df) = parti
content = htmlTable(df)
content = HTML(content)
proxy <- leafletProxy("map")
proxy %>% addPopups(lat=lat, lng=lon, popup = content,
options = popupOptions(closeButton = TRUE)
)
})
})