This repository was archived by the owner on Apr 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.R
More file actions
executable file
·3331 lines (2794 loc) · 141 KB
/
app.R
File metadata and controls
executable file
·3331 lines (2794 loc) · 141 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Dashboard app based on R Shiny to visualise various transport data generated
# as part of the Inclusive and Healthy Mobility project at UCL
# Developed by Alistair Leak https://www.linkedin.com/in/alistairleak/ as part
# of the Inclusive and Healthy Mobility project at UCL.
# Improved and backend transferred to PostgreSQL by Sakinah Yusof, Alfie Long, and Timothy Bruce.
################################
## app.R ##
library(shinydashboard) # Dashboard.
library(data.table) # Use of data.tables - high performance data in R
library(leaflet) # Interface to leaflet maps
library(leaflet.minicharts) # Display flows on leaflet maps
library(RColorBrewer) # Color Ramps
library(dplyr) # tools for data manipulation
library(stringr) # tools for handling strings
library(lubridate) # Tools for handling dates and times
library(dygraphs) # Access to dygraphs javascript in R
library(shinyWidgets) # Helper functions for shiny
library(htmltools) # Helper functions for Shiny
library(rbokeh) # Access to Bokeh plotting in R
library(maptools) # read and write shapefiles
library(shinyjs) # Additional java script functionality
library(forcats) # Helper functions for working with factors
library(rintrojs) # Javascript based introduction to app
library(sf) # for creating sf objects
library(DT) # To render the data output tables
library(shinyTime) # To intuitively input time into the App
library(viridisLite) #tool to bind viridis to leaflet
library(viridis) #tool use viridis palette
library(mapview) #To easily save displayed maps as .png or .pdf file
library(RPostgreSQL) # r to PostgreSQL specific interface wrapper
library(DBI) # Basic SQL database connection package
library(pool) # Tool to handle multiple connections to the same database
library(glue) # To paste together complex text, mainly used in the SQL commands
library(wesanderson) #tool to use wespalette
####HTML fix
#to make NA value correctly positioned (underneath) in legend for continous variables
css_fix <- "div.info.legend.leaflet-control br {clear: both;}"
html_fix <- as.character(htmltools::tags$style(type = "text/css", css_fix))
#options(shiny.trace = F)
#######################################################################################################
# Data ---------------------------------------------------------------------------------------
#If these variables being assigned inside of the server itself becomes problematic (about line 900)
#comment out the assigning of the variables there and uncomment and fill in here
#load in the env variables for connecting to database
db_name=Sys.getenv("DBNAME")
host_name=Sys.getenv("HOSTNAME")
user_name=Sys.getenv("AGGDASHUSERNAME")
password_name=Sys.getenv("AGGDASHPASSNAME")
port_no=Sys.getenv("PORTNAME")
schema_name <- Sys.getenv("AGGDASHSCHEMANAME")
#####################
#Set up DB connection pool
pool <- dbPool(drv = dbDriver("PostgreSQL"),
dbname = db_name,
host = host_name,
port = port_no,
user = user_name,
password = password_name)
#######################################################################################################
# UI definition ---------------------------------------------------------------------------------------
ui <- function(request) {
dashboardPage(title = "UCL TfWM Dashboard",
dashboardHeader(title = tagList(tags$span(class = "logo-mini", icon("dyalog")), tags$span(class="logo-lg", "Dashboard")), tags$li(class = "dropdown", actionButton("full_reset", "Refresh App"))),
## Sidebar content
dashboardSidebar(
sidebarMenu(
#menu of all tabs
menuItem("Where and When", icon = icon("project-diagram"), startExpanded = TRUE,
menuSubItem("O-D Flows", tabName = "tab_od_c_real_od"),
menuSubItem("In or Outflow by Area", tabName = "gen_att_tab")),
menuItem("Who Travels", tabName = "tab_dygraphs", icon = icon("users")),
menuItem("Access to Services", tabName = "tab_access", icon = icon("bus")),
menuItem("Who is Eligible", tabName = "tab_elig", icon = icon("user")),
# Here we will have instructions, credits etc
menuItem("About", tabName = "tab_about", icon = icon("info")),
bookmarkButton(
label = "",
title = "Share the Current App State as a URL.",
icon("paper-plane"),
style = "color: #fff; margin: auto; border: none; background: none;"
)
)
),
# Dashboard body holds each of the dashboard pages.
dashboardBody(
useShinyjs(),
introjsUI(),
useSweetAlert(),
tags$script(HTML("$('body').addClass('sidebar-mini');")),
tabItems(
##### Where and When -------------------------------------------------------------------------------------------------------
tabItem(
tabName = "tab_od_c_real_od",
tags$h4("Explore origin-destination flows", style = "display: block; font-size: 1.5em; margin-top: 0.1em; margin-bottom: 0.5em;"),
## Flows sub-tab ------
# Top menu bar has all control elements
fluidRow(
box(
width = 3, title = "When?", id = "od_box_1_real", status = "primary",
solidHeader = T, style = "min-height: 270px;",
column(
12,
airDatepickerInput("od_c_real_date_start_1",
label = "Start month",
value = "2015-10-01",
maxDate = "2016-08-01",
minDate = "2015-08-01",
view = "months",
minView = "months",
dateFormat = "yyyy-mm"
),
em(h5("These flows represent the trips on a certain provider. Data currently available for the period 2015-08-11 to 2016-08-18"))
),
column(
12,
#shinyjs::disabled(
sliderInput(
inputId = "od_c_real_period",
label = "Period in months",
min = 1, max = 24, post = " months(s)", value = 1
)
#)
)
),
box(
width = 3, title = "Who?", id = "od_box_2_real", status = "primary",
solidHeader = T, style = "min-height: 270px;",
column(
12,
shinyjs::disabled(
pickerInput(
inputId = "od_c_real_demographics",
label = "Demographics",
choices = list(
"Age" = list(
"61 - 65" = "61",
"66 - 70" = "66",
"71 - 75" = "71",
"76 - 80" = "76",
"81 + " = "81"
),
"Gender" = list(
"Male" = "M",
"Female" = "F"
)
),
selected = c("61", "66", "71", "76", "81", "M", "F"),
options = list(
`actions-box` = T,
size = 10,
`selected-text-format` = "count > 3"
),
multiple = T
)
)
),
br(),
column(
12,
shinyjs::disabled(
pickerInput(
inputId = "od_c_real_type_1",
label = "Card type",
choices = list(
"Concessionary" = "con",
"Non-Concessionary" = "non",
"Disability" = "dis"
),
selected = "con",
options = list(
`actions-box` = T,
size = 10
),
multiple = T
)
)
),
column(
12,
shinyjs::disabled(
sliderInput("od_c_real_time_1",
label = "Time of Day",
min = 0, max = 24, post = ":00", value = c(0, 24)
)
)
)
),
box(
width = 3, title = "Compare to another time period", id = "od_box_3_real", status = "primary",
solidHeader = T, style = "min-height: 270px;",
column(
12,
materialSwitch(label=strong("Compare?"),
inputId = "od_c_real_version", status = "primary", value = FALSE
)
),
br(), br(),
column(
12,
airDatepickerInput("od_c_real_date_start_2",
label = "Start month of 2nd period",
value = "2015-11-01",
maxDate = "2016-08-01",
minDate = "2015-08-01",
view = "months",
minView = "months",
dateFormat = "yyyy-mm"
)
)),
box(
width = 3, title = "Plot", id = "od_box_4_real", status = "warning",
solidHeader = T, style = "min-height: 270px;",
column(
12,
#shinyjs::disabled( # sy: enabled other scale options
pickerInput(
inputId = "od_c_real_scale",
label = "Spatial scale ",
width = "100%",
choices = list(
"LSOA" = "lsoa",
"MSOA" = "msoa",
"Local Authority" = "la"
),
selected = "lsoa",
options = list(
`actions-box` = T,
size = 10
),
multiple = F
)
#)
),
column(
12,
numericInput(inputId= "od_c_real_nrows",
label = 'Top % of flows',
value = '1', min = 0, max = 100, step = 1,
width = '100%')
),
column(
12,
actionButton("od_help_real", "Help", width = "100%"),
actionButton("od_c_real_s_update", "Update",
width = "100%",
style = "color: #fff; background-color: #FF8C00; border-color: #FF8C00")
)
)
),
# Second row contains maps and data output.
fluidRow(
tabBox(id="od_map_fluid_row_id",
width = 12,
tabPanel(
title = "Map",
absolutePanel(
top = "65px", left = "80px", style = "z-index: 1000;",
selectInput(
inputId = "od_c_real_zoom_to", label = NULL, width = "150px",
choices = list(
"West Midlands CA" = 0, "Birmingham" = 1, "Dudley" = 2,
"Coventry" = 3, "Sandwell" = 4, "Solihull" = 5,
"Walsall" = 6, "Wolverhampton" = 7
),
selected = 0
)
),
addSpinner(leafletOutput("map_com_real", height = 600), spin = "circle", color = "#E41A1C"),
absolutePanel(
top = "65px", width = 300, length = 700, right = "40px", style = "z-index: 999; font-size = 5px", id = "od_map_id",
box(
title = "Click on the map to view details", id = "od_map_real_toggle",
width = 320, style = "background: transparent; font-size: 8px;", status = "primary", solidHeader = T,
addSpinner(rbokehOutput("inbound_bokeh_plot_real", height = 250, width = 290),
spin = "circle",
color = "#E41A1C"
),
addSpinner(rbokehOutput("outbound_bokeh_plot_real", height = 250, width = 290),
spin = "circle",
color = "#E41A1C"), collapsible = T, collapsed = T
)
)),
tabPanel(
"Data",
dataTableOutput("od_c_real_tbl"),
downloadButton("od_c_real_downloader", "Download Data")
)
)
)),
## Generation / Attraction sub-tab ------
tabItem(
tabName = "gen_att_tab",
tags$h4("Explore where and when trips take place", style = "display: block; font-size: 1.5em; margin-top: 0.1em; margin-bottom: 0.5em;"),
fluidRow(
box(
width = 3, title = "When?", id = "gen_att_box_1_real", status = "primary",
solidHeader = T, style = "min-height: 270px;",
column(
12,
airDatepickerInput("gen_att_date_start_1",
label = "Start month",
value = "2015-08-01",
maxDate = "2016-08-01",
minDate = "2015-08-01",
view = "months",
minView = "months",
dateFormat = "yyyy-mm"
),
em(h5("These data represent the trips on a certain provider. Data currently available for the period 2015-08-11 to 2016-08-18"))
),
column(
12,
sliderInput(
inputId = "gen_att_period",
label = "Period in months",
min = 1,
max = 24,
post = " month(s)",
value = 1
)
)
),
box(
width = 3, title = "Who?", id = "gen_att_2_real", status = "primary",
solidHeader = T, style = "min-height: 270px;",
column(
12,
shinyjs::disabled(
pickerInput(
inputId = "gen_att_demographics",
label = "Demographics",
choices = list(
"Age" = list(
"61 - 65" = "61",
"66 - 70" = "66",
"71 - 75" = "71",
"76 - 80" = "76",
"81 + " = "81"
),
"Gender" = list(
"Male" = "M",
"Female" = "F"
)
),
selected = c("61", "66", "71", "76", "81", "M", "F"),
options = list(
`actions-box` = T,
size = 10,
`selected-text-format` = "count > 3"
),
multiple = T
)
)
),
br(),
column(
12,
shinyjs::disabled(
pickerInput(
inputId = "gen_att_type_1",
label = "Card type",
choices = list(
"Concessionary" = "con",
"Non-Concessionary" = "non",
"Disability" = "dis"
),
selected = "con",
options = list(
`actions-box` = T,
size = 10
),
multiple = T
)
)
),
column(
12,
shinyjs::disabled(
sliderInput("gen_att_time_1",
label = "Time of Day",
min = 0, max = 24, post = ":00", value = c(0, 24)
)
)
)
),
box(
width = 3,
title = "Compare to another time period",
id = "gen_att_3_real",
status = "primary",
solidHeader = T,
style = "min-height: 270px;",
column(
12,
materialSwitch(label = strong("Compare?"),
inputId = "gen_att_c_version", status="primary", value = FALSE)
),
br(), br(),
column(
12,
airDatepickerInput("gen_att_date_start_2",
label = "Start month of 2nd period",
value = "2015-09-01",
maxDate = "2016-08-01",
minDate = "2015-08-01",
view = "months",
minView = "months",
dateFormat = "yyyy-mm"
)
)
),
box(
width = 3, title = "Plot", id = "gen_att_4_real", status = "warning",
solidHeader = T, style = "min-height: 270px;",
column(
12,
radioGroupButtons(
label = "Outflow or Inflow?",
inputId = "gen_att_or_version",
choices = list("Out" = "gen", "In" = "att"),
selected = "gen",
justified = T,
individual = T,
width = "100%",
checkIcon = list(
yes = icon("ok", lib = "glyphicon")
)
)
),
column(
12,
#shinyjs::disabled( # sy: enabled other scale options
pickerInput(
inputId = "att_gen_scale",
label = "Spatial scale ",
width = "100%",
choices = list(
"LSOA" = "lsoa",
"MSOA" = "msoa",
"Local Authority" = "la"
),
selected = "lsoa",
options = list(
`actions-box` = T,
size = 10
),
multiple = F
)
#)
),
column(
12,
actionButton("gen_att_help_real", "Help", width = "100%"),
actionButton("gen_att_c_update", "Update",
width = "100%",
style = "color: #fff; background-color: #FF8C00; border-color: #FF8C00"),
shinyjs::hidden(actionButton("gen_att_c_reset", "Reset", width = "100%",
style = "color: #fff; background-color: #000000; border-color: #000000"))
))
),
# Second row contains maps and data output.
fluidRow(
HTML(html_fix),
tabBox(
width = 12,
tabPanel(
title = "Map",
absolutePanel(
top = "65px", left = "80px", style = "z-index: 1000;",
selectInput(
inputId = "gen_att_zoom_to", label = NULL, width = "150px",
choices = list(
"West Midlands CA" = 0, "Birmingham" = 1, "Dudley" = 2,
"Coventry" = 3, "Sandwell" = 4, "Solihull" = 5,
"Walsall" = 6, "Wolverhampton" = 7
),
selected = 0
)
),
addSpinner(leafletOutput("map_gen_att", height = 600), spin = "circle", color = "#E41A1C"),
absolutePanel(
top = "65px", width = 300, length = 350, right = "40px", style = "z-index: 999; font-size = 5px", id = "gen_att_map_id",
box(
title = "Click on the map to view details", id = "gen_att_map_toggle",
width = 320, style = "background: transparent; font-size: 8px;", status = "primary", solidHeader = T,
addSpinner(rbokehOutput("gen_att_bokeh_plot_real", height = 250, width = 290),
spin = "circle",
color = "#E41A1C"
), collapsible = T, collapsed = T
)
)
),
tabPanel(
"Data",
dataTableOutput("gen_att_c_real_tbl"),
downloadButton("gen_att_downloader", "Download Data")
)
)
)
),
##### Who Travels -------------------------------------------------------------------------------------------------------------------
tabItem(
tabName = "tab_dygraphs",
tags$h4("Explore who travels over time (ENCTS Concessions)", style = "display: block; font-size: 1.5em; margin-top: 0.1em; margin-bottom: 0.5em;"),
#sidebar with demographic input options
sidebarLayout(
sidebarPanel(width = 2,
verticalLayout(
box(width = 15, title = "Who and When", id = "tygraph_box_1", status = "primary", solidHeader = T, style = "min-height: 200px;",
verticalLayout(
shinyjs::disabled(
selectInput(
inputId = "dygraph_statistic", label = "Statistic",
choices = list("Journeys" = "journeys", "Passengers" = "passengers", "Journeys per person" = "journeys_pp")
)
),
selectInput(
inputId = "dygraph_type", label = "Passenger type",
choices = list(
"Over 60 Concession" = "Over 60 Concession",
"Disabled Concession" = "Disabled Concession"
)
),
selectInput(
inputId = "dygraph_time_resolution", label = "Resolution",
choices = list("Month" = "month", "Quarter" = "quarter")
),
materialSwitch(inputId = "dygraph_table_show_1", label = "Show Datatable", status = "primary",
value= FALSE)
)
)
)
),
#main panel containing dygraphs
mainPanel(width = 10,
tabBox(width = 12,
#Breakdown by Gender tab
tabPanel(title = "Breakdown by gender",
fluidRow(width = 12,
dygraphOutput("dygraph_gender", height = "500px", width = "98%")
),
#adds a row with the data table
fluidRow(box(width = 12, dataTableOutput("dt1"), downloadButton("gender_downloader", "Download Data"))
)),
#Breakdown by Age tab
tabPanel(title = "Breakdown by age",
fluidRow(
dygraphOutput("dygraph_age", height = "500px", width = "98%")),
fluidRow(box(width = 12, dataTableOutput("dt2"), downloadButton("age_downloader", "Download Data")))
)
)
)
)
),
##### Access to Services -----------------------------------------------------------------------------------------------------
tabItem(
tabName = "tab_access",
tags$h4("Explore local access to services", style = "display: block; font-size: 1.5em; margin-top: 0.1em; margin-bottom: 0.5em;"),
tabPanel("Exploring Accessibility",
fluidRow(
box(
width = 3, title = "What?", id = "ac_box_1", status = "primary", solidHeader = T, style = "min-height: 95px;",
column(
12,
pickerInput(
inputId = "ac_type",
label = "Select service",
width = "100%",
choices = list(
"Clinic" = "Clinic",
"GP" = "GP",
"Hospital" = "Hospital",
"Retail Centre" = "Retail Centre",
"Train Station" = "Train Station",
"Supermarket (Small)" = "Supermarket (Small)",
"Supermarket (Medium)" = "Supermarket (Medium)",
"Supermarket (Large)" = "Supermarket (Large)",
"Supermarket (Very Large)" = "Supermarket (Very Large)"
),
selected = "Hospital",
options = list(
`actions-box` = T,
size = 10
),
multiple = F
)
)
),
box(
width = 3, title = "When?", id = "ac_box_2", status = "primary", solidHeader = T, height= "137px",
airDatepickerInput(
inputId = "ac_date",
label = "Date",
value = "2014-01-07",
maxDate = "2016-10-11",
minDate = "2010-10-05",
view="months",
minView = "month",
dateFormat = "yyyy-mm"
)
),
#decrease the marigin under date picker
box(
width = 4, title = "Compare accessibility to another time period", id = "ac_box_3",
status = "primary", solidHeader = T, height = "137px",
column(
5, br(),
materialSwitch(label = strong("Compare?"),
inputId = "ac_c_version", status= "primary", value = FALSE)
),
column(
7,
airDatepickerInput(
inputId = "ac_date_comp",
label = "2nd Date",
value = "2015-01-07",
maxDate = "2016-10-11",
minDate = "2010-10-05",
view="months",
minView = "months",
dateFormat = "yyyy-mm"
)
)
),
box(
width = 2, title = "Plot", id = "ac_box_4", status = "warning", solidHeader = T, style = "min-height: 95px;",
actionButton("access_help", "Help", align = "center", width = "100%"),
actionButton("ac_c_s_update", "Update",
align = "center", width = "100%",
style = "color: #fff; background-color: #FF8C00; border-color: #FF8C00"
)
)
),
fluidRow(
tabBox(
width = 12,
tabPanel(
title = "Map",
absolutePanel(
top = "65px", left = "80px", style = "z-index: 999;",
selectInput(
inputId = "ac_zoom_to", label = NULL, width = "150px",
choices = list(
"West Midlands CA" = 0, "Birmingham" = 1, "Dudley" = 2,
"Coventry" = 3, "Sandwell" = 4, "Solihull" = 5,
"Walsall" = 6, "Wolverhampton" = 7
),
selected = 0
)
),
addSpinner(leafletOutput("ac_map", height = 600), spin = "circle", color = "#E41A1C"),
absolutePanel(
top = "65px", width = 300, length = 350, right = "40px", style = "z-index: 999; font-size = 5px", id = "ac_map_id",
box(
title = "Click on the map to view details", id = "ac_map_toggle",
width = 320, style = "background: transparent; font-size: 8px;", status = "primary", solidHeader = T,
addSpinner(rbokehOutput("access_bokeh_plot", height = 250, width = 290),
spin = "circle",
color = "#E41A1C"
), collapsible = T, collapsed = T
)
)
),
tabPanel(
title = "Data",
dataTableOutput("ac_tbl"),
downloadButton("access_downloader", "Download Data")
)
)
)
)
),
##### Who is eligible ----------------------------------------------------------------------------------------------------------------
tabItem(
tabName = "tab_elig",
tags$h4("View local eligibility to concessionary travel", style = "display: block; font-size: 1.5em; margin-top: 0.1em; margin-bottom: 0.5em;"),
fluidRow(
box(
width = 3, title = "What?", id = "elig_box_0", solidHeader = T, status = "primary", style="min-height: 95px",
radioGroupButtons(
inputId = "elig_source",
label = "What eligibility?",
choices = list("Historical" = "estimate", "Forecasts" = "forecast"),
selected = "estimate",
width = "100%",
individual = T,
justified = T,
checkIcon = list(yes = icon("ok", lib = "glyphicon"))
)
),
box(
width = 3, title = "When?", id = "elig_box_1", solidHeader = T, status = "primary", style="min-height: 95px",
# shinyjs::hidden(
airDatepickerInput(
inputId = "elig_year",
label = "Date",
value="2010",
minDate="2008",
maxDate="2016",
view="years",
minView="years",
dateFormat="yyyy")
# )
,
airDatepickerInput(
inputId = "elig_forecast_year",
label = "Date",
value = "2016",
minDate="2016",
maxDate="2041",
view="years",
minView="years",
dateFormat="yyyy")
),
box(
width = 4, title = "Compare eligibility to another time period", id = "elig_box_2", solidHeader = T, status = "primary", style="min-height: 95px",
column (5, br(),
materialSwitch(inputId = "elig_version", label=strong("Compare?"), status = "primary", value = FALSE)
),
column(7,
airDatepickerInput(inputId = "elig_year_comp",
label = "2nd Date",
value="2011",
minDate="2008",
maxDate="2016",
view="years",
minView="years",
dateFormat="yyyy"),
airDatepickerInput(inputId = "elig_year_comp_forecast",
label = "2nd Date",
value = "2017",
minDate="2016",
maxDate="2041",
view="years",
minView="years",
dateFormat="yyyy")
)
),
box(
width = 2, title = "Plot", id = "elig_box_3", solidHeader = T, status = "warning", style="min-height: 95px",
actionButton("elig_help", "Help", width = "100%"),
actionButton("elig_update", "Update",
width = "100%",
style = "color: #fff; background-color: #FF8C00; border-color: #FF8C00"
)
)
),
fluidRow(
tabBox(
width = 12,
tabPanel(
title = "Map", width = 12,
absolutePanel(
top = "65px", left = "80px", style = "z-index: 1000;",
selectInput(
inputId = "elig_zoom_to", label = NULL, width = "150px",
choices = list(
"West Midlands CA" = 0, "Birmingham" = 1, "Dudley" = 2,
"Coventry" = 3, "Sandwell" = 4, "Solihull" = 5,
"Walsall" = 6, "Wolverhampton" = 7
),
selected = 0
)
),
addSpinner(leafletOutput("map_elig", height=600), spin = "circle", color = "#E41A1C"),
absolutePanel(
top = "65px", width = 300, length = 350, right = "40px", style = "z-index: 999; font-size = 5px", id = "elig_map_id",
box(
title = "Click on the map to view details", id = "map_elig_toggle", width = 320,
style = "background: transparent; font-size: 8px;", status = "primary", solidHeader = T,
addSpinner(rbokehOutput("c_bokeh_plot", height = 250, width = 290), spin = "circle", color = "#E41A1C"),
collapsible = T, collapsed = T
)
)
),
tabPanel(
title = "Data", width = 12,
dataTableOutput("elig_s_tbl"),
downloadButton("elig_downloader", "Download Data")
)
)
)
),
# Third tab content: Details about how to use tool and credits ----------------------------------------
tabItem(
tabName = "tab_about",
p("This dashboard was developed as part of the research project 'Inclusive and Healthy Mobility: Understanding Trends in Concessionary Travel in the West Midlands', conducted at University College London in partnership with Transport for West Midlands. Click here to find more information about the project."),
p("The dashboard was created using", tags$a(href = "https://shiny.rstudio.com/", "R Studio Shiny", target="_blank"), " by ", tags$a(href = "https://www.linkedin.com/in/alistairleak/", "Alistair Leak", target="_blank"), ", and updated by ", tags$a(href = "https://github.com/aclong", "Alfie Long", target="_blank"),", ", tags$a(href = "https://github.com/Timothy-Bruce", "Timothy Bruce", target="_blank"), " and ", tags$a(href = "https://github.com/majki00", "Michal Iliev", target="_blank"), "."),
p("Research team: ", tags$a(href = "https://www.ucl.ac.uk/bartlett/casa/jens-kandt", "Jens Kandt", target="_blank"), " (PI), ", tags$a(href = "https://www.geog.ucl.ac.uk//people/academic-staff/paul-longley", "Paul Longley", target="_blank"), " (Co-I), Alistair Leak, ",tags$a(href = "https://github.com/aclong", "Alfie Long", target="_blank"), ", " , tags$a(href = "https://www.ucl.ac.uk/geospatial-analytics/people/ffion-carney", "Ffion Carney", target="_blank"), " - University College London"),
p("Project partners: Chris Lane (Co-I), Daniel Pass, Phillip Evans, Anne Schweickert, Robert Walker - Transport for West Midlands"),
h4("Data Sources"),
tabBox(
width = 12,
tabPanel(
"Who Travels",
p("The trends are based on anonymous bus and tram boardings recorded on smart cards of passengers registered under the English National Concessionary Travel Scheme (ENCTS) in the West Midlands Combined Authority between 2009 and 2016.")
),
tabPanel(
"Where and When",
p("Origin-destination flows of ENCTS passengers between stops from August 2015 to August 2016 aggregated from bus stop-level up to OAs, LSOAs, and MSOAs."),
p("The data record the frequency of journeys between stops, OAs, LSOAs, and MSOAs where it was possible to determine both journeys' origins and destinations. These counts do not represent all journeys and are limited to a specific service provider.")
),
tabPanel(
"Access to Services",
p("Estimates of travel time to selected services based on bus timetables and walking trips along the ITN road network (2010-2016)."),
p("Travel time is the mean minimum time to reach the selected service across a 24-hour period calculated at 30 minute intervals."),
tags$ul(
tags$li("Supermarkets: TBC"),
tags$li("GPs, Clinics and Hospitals: TBC"),
tags$li("Retail Centres: TBC"),
tags$li("Train Stations: TBC")
)
),
tabPanel(
"Who is Eligible",
p("Residents eligible for ENCTS in the West Midlands Combined Authority"),
tags$ul(
tags$li("2010 to 2016 based on ONS mid-year population estimates."),
tags$li("2016 to 2041 based on ONS population forecasts.")
)
),
tabPanel(
"General",
p("Office for National Statistics"),
tags$ul(
tags$li("Administrative boundaries (Output Area, Lower Super Output Area, Middle Super Output Area)"),
tags$li("Administrative Area Centroids (Output Area, Lower Super Output Area, Middle Super Output Area)")
)
)
)
)
)
),
skin = "blue"
)
}
#######################################################################################################
# Functions ---------------------------------------------------------------------------------------
# Server does all the backend work,
server <- function(input, output, session) {
#allow refreshing of browser or app
session$allowReconnect(TRUE)
#reset button
observeEvent(input$full_reset, {
#reload app
session$reload()
})
# Function to range standardise frequencies for plotting.
range01 <- function(x) {
(x - min(x)) / (((max(x) - min(x))))
}
########################################################################################################
# Where and When Tab ----------------------------------------------------------------------------------
## Flow sub-tab-----
# Comparative OD data Map
output$map_com_real <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(-1.703929, 52.447027, zoom = 10)
})
observe({
if (input$od_c_real_zoom_to == 0) {
leafletProxy("map_com_real") %>% flyTo(lng = -1.891587, lat = 52.484689, zoom = 10)
} else if (input$od_c_real_zoom_to == 1) {
leafletProxy("map_com_real") %>% flyTo(lng = -1.890401, lat = 52.486243, zoom = 13L)
} else if (input$od_c_real_zoom_to == 2) {
leafletProxy("map_com_real") %>% flyTo(lng = -2.081112, lat = 52.512255, zoom = 13L)
} else if (input$od_c_real_zoom_to == 3) {
leafletProxy("map_com_real") %>% flyTo(lng = -1.519693, lat = 52.406822, zoom = 13L)
} else if (input$od_c_real_zoom_to == 4) {
leafletProxy("map_com_real") %>% flyTo(lng = -2.010793, lat = 52.536167, zoom = 13L)
} else if (input$od_c_real_zoom_to == 5) {
leafletProxy("map_com_real") %>% flyTo(lng = -1.777610, lat = 52.411811, zoom = 13L)
} else if (input$od_c_real_zoom_to == 6) {
leafletProxy("map_com_real") %>% flyTo(lng = -1.982919, lat = 52.586214, zoom = 13L)
} else if (input$od_c_real_zoom_to == 7) {
leafletProxy("map_com_real") %>% flyTo(lng = -2.128820, lat = 52.586973, zoom = 13L)
}
})
observeEvent(input$od_c_real_period, {
if (input$od_c_real_period != "custom") {
shinyjs::show("od_c_real_date_start_1")
shinyjs::hide("od_c_real_date_start_custom")
} else {
shinyjs::show("od_c_real_date_start_custom")
shinyjs::hide("od_c_real_date_start_1")
}
})
observeEvent(input$od_c_real_version,{
#allow to choose the second date only if the comaprison button is on (TRUE)
if(input$od_c_real_version==FALSE){
shinyjs::disable("od_c_real_date_start_2")
} else if (input$od_c_real_version==TRUE){
shinyjs::enable("od_c_real_date_start_2")
}
})
data_od_s_real <- reactive({
withProgress(message = "Processing Request", value = 0, {
incProgress(1 / 2, message = "Importing data.")
#subset points and lookup table columns according to scale
if(input$od_c_real_scale=="lsoa"){
input_scale_string <- input$od_c_real_scale
}else{
input_scale_string <- paste0("lsoa, ",input$od_c_real_scale)
}
#SQL query using inputs
DT2_s <- setDT(dbGetQuery(pool, glue(
"SELECT f.sum_nf AS sum_nf, f.origin_area_code AS origin, f.destination_area_code AS destination, f.orig_lon, f.orig_lat, f.orig_common_name, st_x(y.geometry) AS dest_lon, st_y(y.geometry) AS dest_lat, COALESCE(y.common_name, f.destination_area_code) AS dest_common_name ",
"FROM (SELECT e.sum_nf, e.origin_area_code, e.destination_area_code, st_x(x.geometry) AS orig_lon, st_y(x.geometry) AS orig_lat, COALESCE(x.common_name, e.origin_area_code) AS orig_common_name ",
"FROM (SELECT SUM(d.sum_nf) AS sum_nf, d.origin_area_code, d.destination_area_code ",
"FROM (SELECT c.sum_nf, c.destination, c.origin_area_code, j.{input$od_c_real_scale} AS destination_area_code ",
"FROM (SELECT a.sum_nf, a.origin, a.destination, i.{input$od_c_real_scale} AS origin_area_code ",
"FROM (SELECT SUM(nf) AS sum_nf, origin, destination ",
"FROM {schema_name}.flows_lsoa_n ",
"WHERE date::date >= DATE '{as.Date(input$od_c_real_date_start_1[[1]])}' AND ",
"date::date <= DATE '{as.Date(input$od_c_real_date_start_1[[1]])}' + INTERVAL '{input$od_c_real_period-1} months' ",
"GROUP BY origin, destination) a ",
"LEFT JOIN ",
"(SELECT {input_scale_string} FROM {schema_name}.stop_oa_lsoa_msoa_la GROUP BY {input_scale_string}) i ON (a.origin = i.lsoa)) c ",
"LEFT JOIN (SELECT {input_scale_string} FROM {schema_name}.stop_oa_lsoa_msoa_la GROUP BY {input_scale_string}) j ON (c.destination = j.lsoa)) d ",