-
Notifications
You must be signed in to change notification settings - Fork 0
/
scc.Rmd
215 lines (181 loc) · 5.31 KB
/
scc.Rmd
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
title: "Suffolk County Council"
date: "`r Sys.Date()`"
scctemplate:
header:
site_branding: "Suffolk County Council"
navigation:
breadcrumb_trail:
- href: "index.html"
text: "Home"
- text: "Suffolk County Council"
toc:
sticky: false
numbered: false
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(httr)
library(XML)
library(dplyr)
library(rvest)
library(leaflet)
library(stringr)
library(here)
library(sf)
library(dplyr)
library(tidyr)
library(purrr)
library(mapview)
library(leafem)
library(leaflet.extras)
library(htmlwidgets)
library(htmltools)
source("get_councillor_info.R")
```
# Find Your Councillor
Use the map below to find your Councillor. Hover over each electoral district to see the name of the district.
Click on the district to get more information about the Councillor. You can also search by location using the search icon on the left.
```{r map, echo = FALSE, warning = FALSE, message = FALSE, out.width = "100%", fig.align='center'}
councillors <- get_councillor_info()
ced <- st_read("L:\\MAPINFO\\DATA\\data_new\\BoundaryLine\\OS_2021_DATA\\Data\\GB\\county_electoral_division.tab", quiet = TRUE) |>
dplyr::filter(File_Name == "SUFFOLK_COUNTY")
ced <- st_transform(ced, 4326)
ced$Name <- gsub(" ED", "", ced$Name)
ced$Name <- gsub("St\\.", "St", ced$Name)
ced <- left_join(ced, councillors, by = c("Name" = "division"))
# If name is NA, change to Vacancy
ced$name[is.na(ced$name)] <- "Vacancy"
ced <- ced |>
select(Name, councillor_name = name, party_name = party, html, image)
colours <- data.frame(
party_name = c(
"Green",
"Conservative",
"Labour",
"Labour and Co-operative",
"Liberal Democrats",
"Independent",
"West Suffolk Independents"
),
hex = c(
"#6DC23B",
"#00AEEF",
"#E4003B",
"#E4003B",
"#FAA01A",
"#B1B4B6",
"#A395A5"
)
)
ced <- left_join(ced, colours, by = c("party_name" = "party_name"))
test <- lapply(unique(ced$Name), function(district) {
counts <- ced |>
dplyr::filter(Name == district)
counts$count <- 1
if(nrow(counts) > 1 ) {
counts <- counts %>%
summarise_all(str_c, collapse = ', ')
counts$count <- 2
}
return(counts)
})
ced <- bind_rows(test)
ced$Name <- gsub("(.*),.*", "\\1", ced$Name)
ced$count <- gsub("(.*),.*", "\\1", ced$count)
ced2 <- ced |>
filter(count == 2)
ced <- ced |>
filter(count == 1)
division_popup <- sprintf(
"<strong>%s</strong><br/><strong>%s</strong><br/><strong><span style='color:%s;'>%s</span></strong><br/>Click <strong><a href = %s>here</a></strong> for full profile<br/><img src=%s width = 180px/>",
ced$Name, ced$councillor_name, ced$hex, ced$party_name, ced$html, ced$image
) %>% lapply(htmltools::HTML)
seperated <- separate(
ced2,
html,
into = c("html1", "html2"),
sep = ","
)
seperated <- separate(
seperated,
councillor_name,
into = c("councillor_name1", "councillor_name2"),
sep = ","
)
seperated <- separate(
seperated,
image,
into = c("image1", "image2"),
sep = ","
)
seperated <- separate(
seperated,
party_name,
into = c("party_name1", "party_name2"),
sep = ","
)
seperated <- separate(
seperated,
hex,
into = c("hex1", "hex2"),
sep = ","
)
division_popup2 <- sprintf(
"<strong>%s</strong><br/><strong>%s</strong><br/><strong><span style='color:%s;'>%s</span></strong><br/>Click <strong><a href = %s>here</a></strong> for full profile<br/><img src=%s width = 180px/><br/><br><strong>%s</strong><br/><strong><span style='color:%s;'>%s</span></strong><br/>Click <strong><a href = %s>here</a></strong> for full profile<br/><img src=%s width = 180px/>",
seperated$Name, seperated$councillor_name1, seperated$hex1, seperated$party_name1, seperated$html1, seperated$image1, seperated$councillor_name2, seperated$hex2, seperated$party_name2, seperated$html2, seperated$image2
) %>% lapply(htmltools::HTML)
division_popup <- append(division_popup, division_popup2)
ced <- bind_rows(ced, ced2)
division_label <- sprintf(
"<strong>%s</strong><br/>Click for more information",
ced$Name
) %>% lapply(htmltools::HTML)
ced$hex <- gsub("#6DC23B, #6DC23B", "#6DC23B", ced$hex)
ced$hex <- gsub("#00AEEF, #00AEEF", "#00AEEF", ced$hex)
councillor_map <- leaflet() |>
addTiles(group = "Topo") |>
addPolygons(
data = ced,
group = "County Electoral Division",
color = "black",
opacity = 1,
weight = 2,
fillColor = ced$hex,
fillOpacity = 0.2,
highlightOptions = highlightOptions(
color = "white",
weight = 4,
fill = "#666",
fillOpacity = 0.7,
bringToFront = TRUE
),
label = division_label,
labelOptions = labelOptions(
style = list("font-weigth" = "normal", padding = "3px 8px"),
textsize = "20px",
direction = "auto"
),
popup = division_popup,
popupOptions = popupOptions(
style = list("font-weigth" = "normal", padding = "3px 8px"),
textsize = "20px",
direction = "auto"
)
) |>
addLayersControl(
data = ced,
overlayGroups = c(
"County Electoral Division"
),
position = "bottomright"
) |>
addSearchFeatures(
targetGroups = c("County Electoral Division", ced$councillor_name),
options = searchFeaturesOptions(
hideMarkerOnCollapse = TRUE,
zoom = 12
)) |>
addFullscreenControl()
councillor_map
```