-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
527 lines (476 loc) · 15.6 KB
/
server.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File: server.R
# Author: Lesley Duff
# Date created: 2024-08-31
# Description:
# Handle the server side of the ScotSport Shiny app
function(input, output, session) {
# Reactives ------------------------------------------------------------------
scotland_areas <- reactive({
selected_council_name <- input$select_council
num_councils <- length(selected_council_name)
areas <- df_sports_facilities_scotland |>
group_by(la_name)
if (num_councils > 0) {
areas <- areas |>
filter(la_name %in% selected_council_name)
}
areas <- areas |>
ungroup()
return(areas)
}) |>
bindCache(input$select_council)
# Build gt table object of counts by Facility type and subtype
# This will be filtered by council area where user has made a selection
tbl_facility_subtype_count <- reactive({
selected_councils <- input$select_council
scotland_areas() |>
count(facility_type, facility_sub_type,
name = "Facilities"
) |>
arrange(facility_type, desc(Facilities)) |>
gt(
rowname_col = "facility_sub_type",
groupname_col = "facility_type",
id = "tbl_type"
) |>
tab_header(
title = md("**Type of sports facility**"),
subtitle = md(glue::glue("Council area: *{selected_councils}*"))
) |>
cols_label(
facility_type = "**Type**",
facility_sub_type = "**Subtype**",
Facilities = "**Facilities**",
.fn = md
) |>
data_color(
columns = Facilities,
method = "numeric",
palette = "Blues",
contrast_algo = "wcag" # (Web Content Accessibility Guidelines)
) |>
summary_rows(
columns = Facilities,
fns = list(
id = "summary_total",
label = "Total",
fn = "sum"
),
fmt = ~ fmt_integer(.),
missing_text = "",
side = "bottom"
) |>
tab_stubhead(label = "Subtype") |>
tab_style( # Table title
style = cell_text(color = scotsport_text_colour),
locations = cells_title(groups = "title")
) |>
tab_style(
style = cell_text(
weight = "bold",
size = "large",
color = scotsport_text_colour,
align = "center"
),
locations = cells_row_groups()
) |>
tab_style(
style = list(
cell_text(
weight = "bold",
color = scotsport_text_colour
)
),
locations = list(
cells_stubhead(),
cells_stub_summary(),
cells_column_labels(),
cells_summary(columns = Facilities)
)
) |>
grand_summary_rows(
columns = Facilities,
fns = list(
label = md("**Total**"),
id = "grand_summary_total",
fn = "sum"
),
fmt = ~ fmt_integer(.),
missing_text = "",
side = "bottom"
) |>
tab_style(
style = cell_text(
color = scotsport_text_colour,
weight = "bold"
),
locations = list(
cells_grand_summary(),
cells_stub_grand_summary()
)
) |>
cols_align(
align = "left",
columns = where(is.factor)
) |>
opt_table_font(
font = list(
google_font(name = scotsport_table_font_family),
"Cochin", "serif"
)
)
}) |>
bindCache(input$select_council)
total_towns_with_facilities <- reactive({
# How many towns have facilities
towns <- scotland_areas() |>
# For town calculations make all (Near) entries the same town
mutate(town = stringr::str_remove(town, " \\(Near\\)")) |>
distinct(town)
total_towns <- nrow(towns)
return(total_towns)
}) |>
bindCache(input$select_council)
selected_town <- reactive({
sel_town <- input$select_town
req(sel_town)
scotland_areas() |>
filter(town == sel_town)
}) |>
bindCache(input$select_town, input$select_council)
# Scotland choropleth data ---------------------------------------------------
scotland_choropleth <- reactive({
facilities_by_council_area <- scotland_areas() |>
count(la_name)
combined_council_info <- facilities_by_council_area |>
# Join the population info
inner_join(population_council, by = join_by(la_name == area_name)) |>
mutate(
people_per_facility = round(population / n, digits = 0),
.after = population
) |>
# Join geospatial info
inner_join(df_council_boundaries,
by = join_by(la_name == local_authority)
) |>
mutate(
hectares_per_facility = round(hectares / n, digits = 0),
.after = hectares
) |>
rename(Facilities = n)
combined_council_info
}) |>
bindCache(input$select_council)
sorted_selected_area_names <- reactive({
sort(input$select_council)
}) |>
bindCache(input$select_council)
tbl_facility_detail <- reactive({
selected_council_name <- sorted_selected_area_names()
df_facility_detail <- scotland_areas() |>
# We are displaying addresses as if for mailing purposes
# Remove any "(Near)"
mutate(town = stringr::str_remove(town, "\\(Near\\)")) |>
dplyr::select(
name, address, facility_type, facility_sub_type,
town, postcode, la_name
) |>
tidyr::unite("full_address",
c(name, address, town, postcode),
sep = paste0(", "),
remove = FALSE,
na.rm = TRUE
) |>
mutate(facility_type = paste0(facility_type,
sep = ": ",
facility_sub_type
)) |>
relocate(full_address, .before = name) |>
relocate(la_name, .after = full_address) |>
select(-name, -address, -facility_sub_type, -town, -postcode) |>
arrange(full_address, la_name, facility_type) |>
# Work out how many of the same type at the same address
group_by(full_address, la_name, facility_type) |>
summarise(how_many = n()) |>
ungroup() |>
# Linebreak inside HTML table cell
mutate(full_address = stringr::str_replace_all(
full_address,
",", ",<br />"
))
df_facility_detail |>
gt(id = "facility_detail") |>
tab_header(
title = md("**Sports facilities by address**"),
subtitle = md(glue::glue("Council area: *{selected_council_name}*"))
) |>
cols_label(
full_address = md("**Address**"),
la_name = md("**Council area**"),
facility_type = md("**Type**"),
how_many = md("**How many?**")
) |>
cols_align(
align = "left",
columns = where(is.factor)
) |>
opt_row_striping() |>
opt_table_font(
font = list(
google_font(name = scotsport_table_font_family),
"Cochin", "serif"
)
) |>
opt_interactive(
use_compact_mode = TRUE,
use_filters = TRUE,
use_highlight = TRUE,
use_page_size_select = TRUE,
use_search = TRUE
)
}) |>
bindCache(input$select_council)
# For use in pulldown select
list_town_names <- reactive({
town_names <- scotland_areas() |>
count(town) |>
drop_na() |> # Need more data cleaning to identify missing towns
arrange(town)
l_town_names <- town_names$town
names(l_town_names) <- paste0(town_names$town, " (", town_names$n, ")")
l_town_names
}) |>
bindCache(scotland_areas())
# Outputs --------------------------------------------------------------------
output$home_area_names <- renderText({
build_area_names(sorted_selected_area_names())
}) |>
bindCache(sorted_selected_area_names())
output$where_area_names <- renderText({
build_area_names(sorted_selected_area_names())
}) |>
bindCache(sorted_selected_area_names())
output$type_area_names <- renderText({
build_area_names(sorted_selected_area_names())
}) |>
bindCache(sorted_selected_area_names())
output$total_facilities_areas <- renderText({
scales::label_comma()(nrow(scotland_areas()))
}) |>
bindCache(scotland_areas())
output$total_people_per_facility <- renderText({
facilities_per_council <- scotland_areas() |>
# How many facilities per council
count(la_name)
# Join the population info with the facilities
combined_council_info <- facilities_per_council |>
inner_join(population_council, by = join_by(la_name == area_name))
# Population divided by number of facilities
people_per_facility <- sum(combined_council_info$population) /
sum(combined_council_info$n)
scales::label_comma()(people_per_facility)
}) |>
bindCache(scotland_areas())
output$home_total_towns_with_facilities <- renderText({
scales::label_comma()(total_towns_with_facilities())
}) |>
bindCache(total_towns_with_facilities())
output$where_total_towns_with_facilities <- renderText({
scales::label_comma()(total_towns_with_facilities())
}) |>
bindCache(total_towns_with_facilities())
# Plots ----------------------------------------------------------------------
output$plot_facility_type <- renderPlot({
df_type <- scotland_areas() |>
group_by(facility_type) |>
summarise(n = n()) |>
ungroup() |>
mutate(facility_type = fct_reorder(facility_type, n, .desc = FALSE))
plot_facility_type(df_type)
}) |>
bindCache(scotland_areas())
output$map_town <- renderLeaflet({
# Need to have selected a town in order to see a map
req(input$select_town)
town <- selected_town()
town_types <- unique(town$facility_type)
town_map <- town |>
leaflet() |>
addTiles() |>
addProviderTiles(
provider = "Esri.WorldImagery",
group = "World Imagery (satellite)"
) |>
addMiniMap(
tiles = providers$Esri.WorldStreetMap,
position = "bottomleft",
toggleDisplay = TRUE
) |>
addResetMapButton() |>
addScaleBar(position = "bottomright")
for (i in seq_along(town_types)) {
group_data <- town[town$facility_type == town_types[i], ]
# Create the popup content
popup_content <- paste0(
"<table>",
"<tr><th>Name:</th><td>", htmltools::htmlEscape(group_data$name),
"</td></tr>",
"<tr><th>Address:</th><td>", htmltools::htmlEscape(group_data$address),
"</td></tr>",
"<tr><th>Town:</th><td>", htmltools::htmlEscape(group_data$town),
"</td></tr>",
"<tr><th>Postcode:</th><td>",
htmltools::htmlEscape(group_data$postcode), "</td></tr>",
"<tr><th>Council area:</th><td>",
htmltools::htmlEscape(group_data$la_name), "</td></tr>",
"</table>",
"<hr />",
"<table>",
"<tr><th>Type:</th><td>",
htmltools::htmlEscape(group_data$facility_type), "</td></tr>",
"<tr><th>Subtype:</th><td>",
htmltools::htmlEscape(group_data$facility_sub_type), "</td></tr>",
"<tr><th>Last update:</th><td>",
get_date(group_data$date_updated), "</td></tr>",
"</table>"
)
# Add the map pins and clusters
town_map <- addMarkers(
map = town_map,
lng = group_data$lng,
lat = group_data$lat,
group = town_types[i],
popup = popup_content,
label = group_data$name,
options = markerOptions(),
clusterOptions =
markerClusterOptions(
# when you click a cluster we zoom to its bounds
zoomToBoundsOnClick = TRUE
)
)
}
# For controlling showing/hiding layers
town_map <- addLayersControl(
map = town_map,
baseGroups = c(
"Open Street Map (default)",
"World Imagery (satellite)"
),
overlayGroups = town_types,
options = layersControlOptions(
collapsed = FALSE,
# if TRUE, the control will automatically maintain the z-order of its
# various groups as overlays are switched on and off.
autoZIndex = TRUE
)
)
town_map
}) |>
bindCache(selected_town())
# Scotland Choropleths plots -------------------------------------------------
# Scotland by number of facilities
output$plot_scotland_area_facilities <- renderPlot({
plot_facilties_scotland(scotland_choropleth(),
feature_cols = "Facilities",
label_fill = "Facilities",
plot_title = "Facilities by council area"
)
}) |>
bindCache(scotland_choropleth())
# Scotland by number of people per facility
output$plot_scotland_area_people <- renderPlot({
plot_facilties_scotland(scotland_choropleth(),
feature_cols = "people_per_facility",
label_fill = "People per facility",
plot_title = "People per facility by council area"
)
}) |>
bindCache(scotland_choropleth())
output$facility_type_table <- render_gt({
tbl_facility_subtype_count()
}) |>
bindCache(tbl_facility_subtype_count())
output$facility_detail_table <- render_gt({
tbl_facility_detail()
}) |>
bindCache(tbl_facility_detail())
output$ui_select_town <- renderUI({
selectizeInput("select_town",
label = strong("Town (number of facilities):"),
choices = c(
"Choose a town " = "",
list_town_names()
),
# This helps selectize go wider
# https://stackoverflow.com/questions/76431862/how-can-i-make-a-shiny-selectinput-overflow-a-bslibnavset-card-pill
options = list(dropdownParent = "body"),
width = "100%",
multiple = FALSE
)
}) |>
bindCache(list_town_names())
# plot helper functions ------------------------------------------------------
plot_facility_type <- function(df,
plot_title = "Facilites by type",
plot_fill_bar_chart = "dodgerblue4") {
df |>
ggplot(aes(x = facility_type, y = n)) +
geom_col(fill = plot_fill_bar_chart) +
scale_y_continuous(labels = scales::label_comma()) +
coord_flip() +
labs(
title = plot_title,
x = element_blank(),
y = element_blank(),
alt = paste0(
"A map showing the location of the facilities in the ",
"selected town. You can access text data of towns in the Address ",
"section"
)
) +
theme_minimal(base_size = scotsport_default_font_size) +
theme(text = element_text(
family = scotsport_default_font_family,
colour = scotsport_text_colour
))
}
# Choropleth map of Scotland -------------------------------------------------
plot_facilties_scotland <- function(df, feature_cols, label_fill,
plot_title) {
df |>
ggplot() +
# Do empty outline
geom_sf(data = df_council_boundaries, aes(geometry = geometry)) +
# Coloured selected councils
geom_sf(aes(
fill = .data[[feature_cols]],
geometry = geometry
)) +
theme_void(base_size = scotsport_default_font_size) +
theme(text = element_text(
family = scotsport_default_font_family,
colour = scotsport_text_colour
)) +
# Lato, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
# "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji",
# "Segoe UI Emoji", "Segoe UI Symbol"
# make the lower numbers lighter
scale_fill_viridis(direction = -1) +
labs(
fill = label_fill,
title = plot_title
)
}
# Helper functions -----------------------------------------------------------
build_area_names <- function(selected_council_name) {
num_councils <- length(selected_council_name)
if (num_councils > 0) {
councils <- paste(selected_council_name, collapse = ", ")
areas <- paste("Council area(s):", councils)
} else {
areas <- "Area: All Scotland"
}
return(areas)
}
}