-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
621 lines (576 loc) · 19.8 KB
/
Copy pathserver.R
File metadata and controls
621 lines (576 loc) · 19.8 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
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# Server function
function(input, output, session) {
# Data
data_street_infra <- get_data_street_infra()
data_mu_paths <- get_data_mu_paths()
data_wards <- get_data_wards()
data_modeshare <- get_data_modeshare()
data_modeshare_subset <- reactiveVal(data_modeshare)
data_modeshare_summary <- reactive({
get_data_modeshare_summary(data_modeshare_subset())
})
filter_inputs <- reactiveValues(
infra = NULL,
other = NULL,
uid = NULL,
ward = NULL
)
north_arrow <- div(
style = "display: flex; flex-direction: column; color: #777; margin-top: 1.4rem;",
icon(
"location-arrow",
class = "fa-solid fa-2xl fa-rotate-by",
style = "margin-right: 0; --fa-rotate-angle: -45deg;"
),
span(
style = "display: block; color: #777; font-family: Source Sans Pro; font-size: 2.4rem; font-weight: 700; line-height: 1.5; text-align: center; width: 100%;",
"N"
)
)
color_wards <- colorFactor(
palette.colors(
n = 9,
palette = "Okabe-Ito"
),
data_wards$WARD
)
initial_bounds <- st_bbox(data_wards)
# Sidebar
output$sidebarInputs <-
renderUI({
if (input$sidebarNav == "map" | input$sidebarNav == "chart") {
tagList(
div(
p(
class = "shiny-input-container",
style = "font-size: 1.8rem;",
strong("Data subsets"),
actionLink(
style = "display: inline-block; margin-left: 1rem; font-size: 1.3rem;",
"actionFilterReset",
"Reset"
)
),
selectInput(
"inputSelectBikeInfra",
"Bike infrastructure",
choices = get_choices_bikeinfra(),
selected = isolate(input$inputSelectBikeInfra)
),
selectInput(
"inputSelectWard",
"City ward",
choices = get_choices_ward(),
selected = isolate(input$inputSelectWard)
),
selectInput(
"inputSelectOther",
"Other subsets",
choices = get_choices_other(),
selected = isolate(input$inputSelectOther)
)
)
)
}
})
# Welcome modal
observeEvent(
input$sidebarNav,
{
if (input$sidebarNav == "map") {
# Show welcome message
showModal(
modalDialog(
title = NULL,
easyClose = TRUE,
size = "m",
footer = NULL,
get_content_welcome(),
p(
style = "text-align: center;",
modalButton(
"Let's go"
)
)
)
)
}
},
once = TRUE
)
# Initial map output
output$mapMain <-
renderLeaflet({
leaflet() |>
addProviderTiles(providers$CartoDB.Positron) |>
addControl(
north_arrow,
position = "topright",
className = ""
) |>
addScaleBar(
position = "bottomleft",
options = scaleBarOptions(
metric = FALSE
)
) |>
addPolygons(
group = "baseWards",
data = data_wards,
stroke = TRUE,
weight = 2,
color = "#999",
opacity = 0.2,
fillColor = ~color_wards(WARD),
fillOpacity = 0.1,
) |>
addPolylines(
group = "baseStreetLines",
data = data_street_infra,
weight = 2,
opacity = 0.3,
color = "#555"
) |>
addPolylines(
group = "baseMUPaths",
data = data_mu_paths,
weight = 2,
opacity = 0.3,
color = "#555"
) |>
addCircleMarkers(
group = "baseMarkers",
data = data_modeshare,
radius = 9,
weight = 1,
color = "#555",
fillColor = "#777",
layerId = ~uid,
) |>
fitBounds(
initial_bounds[["xmin"]],
initial_bounds[["ymin"]],
initial_bounds[["xmax"]],
initial_bounds[["ymax"]]
) |>
setMaxBounds(
initial_bounds[["xmin"]],
initial_bounds[["ymin"]],
initial_bounds[["xmax"]],
initial_bounds[["ymax"]]
)
})
# Handle changes to filter inputs
observe({
req(input$sidebarNav == "map" | input$sidebarNav == "chart")
inputs_list <- reactiveValuesToList(filter_inputs)
# Allow map clicks on locations that are not highlighted
if (! is.null(inputs_list$uid)) {
args <- list(data_modeshare, uid = inputs_list$uid)
} else {
args <- c(list(data_modeshare), inputs_list)
}
subset <- do.call(get_data_modeshare_subset, args)
data_modeshare_subset(subset)
})
# Handle data subsets reset link
observeEvent(input$actionFilterReset, {
filter_inputs$uid <- NULL
updateSelectInput(
"inputSelectBikeInfra",
selected = "all",
session = session
)
updateSelectInput(
"inputSelectWard",
selected = "all",
session = session
)
updateSelectInput(
"inputSelectOther",
selected = "none",
session = session
)
})
# Update which markers are highlighted when the modeshare subset changes
observeEvent(data_modeshare_subset(), {
data <- data_modeshare_subset()
leafletProxy("mapMain") |>
clearGroup("highlightMarkers") |>
addCircleMarkers(
group = "highlightMarkers",
data = data,
radius = 9,
weight = 1,
color = "#555",
opacity = 1,
fillColor = "darkorange",
fillOpacity = 0.8,
# This layerId has to be different from the base markers
layerId = ~uid_highlighted,
)
})
# Handle map clicks
# Inspired by https://medium.com/ibm-data-ai/capture-and-leverage-mouse-locations-and-clicks-on-leaflet-map-6d8601e466a5
observeEvent(input$mapMain_click, {
marker_click <-input$mapMain_marker_click
map_click <- input$mapMain_click
if (
! is.null(marker_click)
&& all(
unlist(marker_click[c('lat','lng')]) ==
unlist(map_click[c('lat','lng')])
)
) {
# Marker was clicked
filter_inputs$uid <- marker_click$id
} else {
# Basemap was clicked
filter_inputs$uid <- NULL
}
})
# Reset map click selection when switching to the chart
observeEvent(
input$sidebarNav,
{
if (input$sidebarNav == "chart") {
filter_inputs$uid <- NULL
}
}
)
# Handle bike infrastructure filter input
observeEvent(input$inputSelectBikeInfra, {
if (input$inputSelectBikeInfra == "all") {
filter_inputs$infra <- NULL
leafletProxy("mapMain") |>
clearGroup("highlightStreetLines")
} else {
filter_inputs$infra <- input$inputSelectBikeInfra
if (input$inputSelectBikeInfra == "Multi-Use Path") {
data_infra_highlight <- data_mu_paths
} else {
data_infra_highlight <-
data_street_infra |>
filter(
BIKE_FAC == input$inputSelectBikeInfra
)
}
leafletProxy("mapMain") |>
clearGroup("highlightStreetLines") |>
addPolylines(
group = "highlightStreetLines",
data = data_infra_highlight,
weight = 2,
opacity = 0.7,
color = "darkred"
)
}
filter_inputs$uid = NULL
})
# Handle ward filter input
observeEvent(input$inputSelectWard, {
if (input$inputSelectWard == "all") {
filter_inputs$ward <- NULL
leafletProxy("mapMain") |>
clearGroup("highlightWards")
} else {
filter_inputs$ward <- input$inputSelectWard
data_wards_highlight <-
data_wards |>
filter(
WARD == input$inputSelectWard
)
leafletProxy("mapMain") |>
clearGroup("highlightWards") |>
addPolygons(
group = "highlightWards",
data = data_wards_highlight,
stroke = TRUE,
weight = 2,
color = "#999",
opacity = 0.6,
dashArray = "4",
fillColor = ~color_wards(WARD),
fillOpacity = 0.35,
)
}
filter_inputs$uid = NULL
})
# Handle "other" filter input
observeEvent(input$inputSelectOther, {
if (input$inputSelectOther == "none") {
filter_inputs$other <- NULL
} else {
filter_inputs$other <- input$inputSelectOther
}
filter_inputs$uid = NULL
})
# Trips per hour
output$valueBoxTotTrips <-
renderValueBox({
data <- data_modeshare_summary()
valueBox(
format(data$trip_count, big.mark = ","),
"Trips counted per hour",
icon = icon("route")
)
})
# Active vs Motor Vehicle mode share
output$plotPctModeShare <-
renderPlot({
data <-
data_modeshare_summary() |>
select(active_pct, mv_pct) |>
rename(
"Active trips" = active_pct,
"Motor vehicle trips" = mv_pct
) |>
pivot_longer(
cols = everything(),
names_to = "Trips",
values_to = "Percent"
) |>
mutate(
Trips = factor(
Trips,
levels = c("Motor vehicle trips", "Active trips")
)
)
data |>
ggplot(
aes(
x = Percent,
y = factor(1),
fill = Trips,
)
) +
geom_col() +
geom_text_repel(
aes(
label = paste(
fill,
": ",
round(after_stat(x), 1),
"%",
sep = ""
)
),
seed = 1,
position = position_stacknudge(
vjust = 0.5,
x = c(5, -5),
y = c(0.9, -0.9),
),
segment.curvature = -1e-20,
arrow = arrow(
length = unit(0.05, "npc"),
type = "closed"
)
) +
xlab("") +
ylab("") +
theme_void() +
theme(
legend.position = "none",
plot.margin = margin(b = 10)
)
})
# Active modes split out
output$plotPctActiveModeShare <-
renderPlot({
data <-
data_modeshare_summary() |>
select(bike_pct, ped_pct, roll_pct) |>
rename(
"Bicycle" = bike_pct,
"Pedestrian" = ped_pct,
"Other" = roll_pct
) |>
pivot_longer(
cols = everything(),
names_to = "Mode",
values_to = "Percent"
) |>
mutate(
Mode = factor(
Mode,
levels = c("Other", "Pedestrian", "Bicycle")
)
)
maxpct_summary <- max(data$Percent)
data |>
ggplot(
aes(
x = Percent,
y = Mode
)
) +
geom_col(
aes(
fill = Mode
)
) +
geom_text(
aes(
label = paste(
round(after_stat(x), 1),
"%",
sep = ""
)
),
hjust = -0.25
) +
xlim(0, maxpct_summary * 1.3) +
ylab("") +
theme_void() +
theme(
axis.text.y = element_text(
hjust = 1
),
legend.position = "none",
plot.margin = margin(b = 10)
)
})
# Data subset details
output$tableDetails <-
render_gt({
data <-
data_modeshare_subset() |>
st_drop_geometry()
if (nrow(data) == 1) {
title <- data$site_desc
data <-
data |>
summarize(
"Active trips counted per hour" = totAT_hr,
"Motor vehicle trips counted per hour" = totMV_hr,
"Walk Score" = walkscore,
"Bike Score" = bikescore
)
} else {
title <- sprintf(
"%s locations",
nrow(data)
)
data <-
data |>
summarize(
"Total active trips counted per hour" = sum(totAT_hr),
"Total motor vehicle trips counted per hour" = sum(totMV_hr),
"Average Walk Score" = round(mean(walkscore)),
"Average Bike Score" = round(mean(bikescore))
)
}
data[] <- lapply(data, function(x) format(x, big.mark = ","))
data |>
pivot_longer(
cols = everything(),
names_to = "Detail",
values_to = "Value"
) |>
gt(
rowname_col = "Detail"
) |>
tab_header(
title = title
) |>
tab_options(
column_labels.hidden = TRUE,
table.width = "100%",
table.font.names = c("Source Sans Pro", "sans-serif"),
table.font.size = 13,
heading.title.font.size = 16
)
})
# Chart with customizable X and Y
output$plotInteractiveChart <-
renderPlotly({
data <-
data_modeshare_subset() |>
rename_vars() |>
mutate(
text = paste0(
"<span style='display: block; padding: 0.5rem; text-align: left;'>",
"Location: ", Location, "<br>",
input$inputSelectX, ": ", format(round(!!input$inputSelectX, 1), big.mark = ","), "<br>",
input$inputSelectY, ": ", format(round(!!input$inputSelectY, 1), big.mark = ","),
"</span>"
)
)
plot <-
data |>
ggplot(
aes(
x = !!input$inputSelectX,
y = !!input$inputSelectY
)
) +
suppressWarnings(geom_point(
aes(
text = text
),
shape = 21,
color = "#555",
fill = "darkorange",
size = 5,
alpha = 0.6
))
if (!!input$inputCheckboxTrendLine) {
smooth_method <- ifelse(nrow(data) < 10, "lm", "loess")
plot <-
plot +
geom_smooth(
formula = y ~ x,
method = smooth_method,
na.rm = TRUE,
se = FALSE,
)
}
plot <-
plot +
ylim(0, NA) +
theme(
axis.title = element_text(
family = "Source Sans Pro",
size = 14
),
axis.title.x = element_text(
margin = margin(t = 12)
),
axis.title.y = element_text(
margin = margin(r = 12)
),
axis.text = element_text(
family = "Source Sans Pro",
size = 12
)
)
ggplotly(
plot,
tooltip = "text"
) |>
layout(
hoverlabel = list(
bgcolor = "white",
font = list(
size = 12,
color = "black"
)
)
) |>
config(displayModeBar = F)
})
# Handle chart axis reset link
observeEvent(input$actionChartReset, {
updateSelectInput(
"inputSelectX",
selected = "Motor vehicle trips counted per hour",
session = session
)
updateSelectInput(
"inputSelectY",
selected = "Active trips mode share %",
session = session
)
})
}