-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
executable file
·1892 lines (1375 loc) · 84.9 KB
/
app.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
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
#Author: Matthew Crittenden
#File Name: app.R (CMS Dashboard v3.0)
#Purpose: Created for ANSA-EAP's CheckMySchool Program to host, process, and display its CMS App reports data
#Date Created: 12/27/2019
#Date Edited: 1/7/2019
#-------Loading packages-----------------------------------------
#shiny
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
library(shinyjs)
#tidyverse
library(ggplot2)
library(dplyr)
library(tidyr)
library(stringr)
library(forcats)
#mapping
library(leaflet)
library(rgdal)
#others
library(slickR)
library(DT)
library(questionr)
library(googledrive)
library(googlesheets4)
library(rlist)
#-------Loading data----------------------------------------------
#----------reports data------------------------------------------
reports1 <- readxl::read_excel('./cleaned_cms_data_dec3-2019.xlsx')
#date is in yyyy-mm-dd format
reports1$`Reported On` <- format(strptime(reports1$`Reported On`, format = "%Y-%m-%d %H:%M:%S"), "%Y/%m/%d %H:%M:%S")
reports1$`Reported On` <- as.Date(reports1$`Reported On`)
colnames(reports1)[colnames(reports1)=="Reported On"] <- "Date"
reports1$Date <- gsub("/","-",reports1$Date)
#add a unique school column
reports1$`Unique School` <- paste0(reports1$`School Name`, " (ID: ", reports1$`School ID`,")") #may add division name here later...
#----------live reports data (not working)-------------
#enable use of googlesheets API in non-interactive settings
source("global.R") #this pulls in the hidden variable information from the global.R file
drive_auth_configure(api_key = cms_dashboard_key)
sheets_auth_configure(api_key = drive_api_key())
drive_deauth()
sheets_deauth()
#read in the live data from googlesheets and subset it
# csv_data <- read_sheet(ss = cms_csv_data)
#
# #date is in yyyy-mm-dd format
# csv_data$`Reported On` <- format(strptime(csv_data$`Reported On`, format = "%Y-%m-%d %H:%M:%S"), "%Y/%m/%d %H:%M:%S")
# csv_data$`Reported On` <- as.Date(csv_data$`Reported On`)
# colnames(csv_data)[colnames(csv_data)=="Reported On"] <- "Date"
#
# #remove the observations in reports1
# csv_data <- csv_data[which(csv_data$Date >= as.Date.character("2019/12/03")),]
# names(csv_data) <- names(reports1)
#
# #merge the two dataframes
# reports1 <- rbind(csv_data,reports1)
# reports1$Date <- gsub("/","-",reports1$Date)
#----------sni and coordinate data--------------------------------
coord_data <- read.csv('./coord_data.csv')
#remove duplicates which were from different calculations of sni
coord_data <- coord_data[!duplicated(coord_data[,1]),]
#keep only schools which have reports (make sure this uses the correct regions and divisions too)
colnames(coord_data)[colnames(coord_data)=="School_Name_y"] <- "School Name"
colnames(coord_data)[colnames(coord_data)=="School_ID"] <- "School ID"
colnames(coord_data)[colnames(coord_data)=="Region_Name"] <- "Region"
coord_data <- merge(coord_data, reports1, by = c("School ID"), all = FALSE)
#find out which School IDs are missing from coord_data
ok_list <- c()
uhoh_list <- c()
for (id in unique(reports1$`School ID`)) {
if (id %in% coord_data$`School ID`) {
ok_list <- list.append(ok_list,id)
}
else {
uhoh_list <- list.append(uhoh_list,id)
}
}
ok_list <- as.list(ok_list)
uhoh_list <- as.list(uhoh_list)
#FUTURE PROBLEM: ONLY 297 OF 308 SCHOOLS MATCHED. 11 SCHOOL IDS ARE NOT IN THE COORDINATE DATASET
#FOR NOW, JUST REMOVE THESE FROM THE SCHOOL_LEVEL MAPPING
#----------shapefile data-----------------------------------------
region_shape <- readOGR('./region_data_shp', layer = 'region_data')
division_shape <- readOGR('./divisions_data_shp', layer = 'division_polygons')
#-------UI--------------------------------------------------------
ui <- dashboardPagePlus(
collapse_sidebar = TRUE,
header = dashboardHeaderPlus(title = "CMS App Analytics",
enable_rightsidebar = TRUE,
rightSidebarIcon = "info",
dropdownMenu(type = "message",
messageItem(from = "New Look",
message = "Our dashboard has a new look!",
icon = icon("laugh-beam"))
)
), #close dashboardHeaderPlus
#-------left sidebar----------------------
sidebar = dashboardSidebar(collapsed = TRUE,
sidebarMenu(id = "left_sidebar",
menuItem("Welcome", tabName = "welcome", icon = icon("smile",lib='font-awesome')),
menuItem("User Guide", tabName = "user_guide", icon = icon("toolbox",lib='font-awesome')),
menuItem("Quick Statistics", tabName = "quick_statistics", icon = icon("th",lib='font-awesome')),
menuItem("Data Explorer", tabName = "data_explorer", icon = icon("search",lib='font-awesome')),
menuItem("Submissions Map", tabName = "submissions_map", icon = icon("globe-asia",lib='font-awesome')),
menuItem("Raw Data", tabName = "raw_data", icon = icon("download",lib='font-awesome')),
menuItem("Get Involved", icon = icon("hands-helping",lib = 'font-awesome'),
menuSubItem("Download iOS app", icon = icon("app-store",lib='font-awesome'),
href = "https://apps.apple.com/ph/app/checkmyschool/id1458068394"),
menuSubItem("Download Android app", icon = icon("google-play",lib='font-awesome'),
href = "https://play.google.com/store/apps/details?id=com.checkmyschool.app&hl=en"),
menuSubItem("Visit us on Facebook", icon = icon("facebook-square",lib='font-awesome'),
href = "https://www.facebook.com/CheckMySchool/"),
menuSubItem("Visit us on Twitter", icon = icon("twitter-square", lib='font-awesome'),
href = "https://twitter.com/onlinecms?lang=en"),
menuSubItem("Watch us on Youtube", icon = icon("youtube-square", lib='font-awesome'),
href = "https://www.youtube.com/user/onlinecms"))
)
), #close dashboardSidebar
#-------right sidebar---------------------
rightsidebar = rightSidebar(
background = "light",
#----------data query tools-------------------
rightSidebarTabContent(
id = 1,
title = "Data Query Tools",
icon = "cog",
active = TRUE,
fluidPage(
fluidRow(column(width = 12,
div(style = "font-size: 11px; font-weight: normal;",
selectInput(inputId = "selected_BP",
label = "Include best practices?",
choices = c("yes", "no"),
selected = "yes"),
selectInput(inputId = "selected_adopt",
label = "Include Adopt-a-School?",
choices = c("yes", "no"),
selected = "yes"),
dateRangeInput(inputId = "selected_dates",
label = "Timeframe:",
start = as.Date.character("2019-04-06"),
end = Sys.Date(),
min = as.Date.character("2019-04-06"),
max = Sys.Date()),
selectInput(inputId = "selected_region",
label = "Region:",
choices = c("default (all)",sort(unique(as.character(reports1$Region)))),
selected = "default (all)"),
selectInput(inputId = "selected_division",
label = "Division:",
choices = "default (all)",
selected = "default (all)"),
selectInput(inputId = "selected_school",
label = "School:",
choices = "default (all)",
selected = "default (all)"),
selectInput(inputId = "selected_category",
label = "Service Classification/Category:",
choices = c("default (all)",sort(unique(reports1$`Service Classification/Category`))),
selected = "default (all)"),
selectInput(inputId = "selected_item",
label = "Service Item:",
choices = "default (all)",
selected = "default (all)"),
actionButton(inputId = "reset1", label = "Reset", style='padding:4px; font-size:90%; margin-top:0.5em; margin-bottom:1em;')
)
)
)
)
),
#----------collaborators--------------------
rightSidebarTabContent(
id = 2,
title = "Collaborators",
icon = "users",
h6("Matthew Crittenden developed this dashboard from May to December 2019
as CMS's data specialist. He came to the Philippines as a
Summer Fellow of both William & Mary's Global Research Institute and
AMES-APIA Freeman Program in Asia."),
img(src='cms_logo.png', width = 200),
img(src='ansa_logo.png', width = 200),
img(src='goalkeepers_logo.png', width = 200),
img(src='freeman_logo1.png', width = 200),
fluidRow(column(width = 12,
img(src='gri_logo.png', width = 200), align = "center"))
)
), #close rightSidebar
#-------dashboard body-------------------
body = dashboardBody(
shinyjs::useShinyjs(),
#-------HTML/CSS tags------------------------------
tags$head(
tags$style(HTML('div#control-sidebar-2-tab.tab-pane.active>img {margin-bottom: 1.5em;}')), #this adds space in between logo pics
tags$style(HTML('.shiny-output-error-validation {color: red;}')), #change color of validation error
tags$style(HTML('.shiny-output-error {color: grey;}')), #change color of regular error
tags$style(HTML('div#DataTables_Table_0_info.dataTables_info {font-size: 12px;}')), #change font size of datatable info blurb
#below changes the navbar and left sidebar
tags$style(HTML('
/* logo */
.skin-blue .main-header .logo {
background-color: #172869;
font-size: 15px;
}
/* logo when hovered */
.skin-blue .main-header .logo:hover {
background-color: #172869;
}
/* navbar (rest of the header) */
.skin-blue .main-header .navbar {
background-color: #172869;
}
/* main sidebar */
.skin-blue .main-sidebar {
color: #FFFFFF;
}
/* active selected tab in the sidebarmenu */
.skin-blue .main-sidebar .sidebar .sidebar-menu .active a{
background-color: #FFCC00;
}
/* other links in the sidebarmenu */
.skin-blue .main-sidebar .sidebar .sidebar-menu a{
color: #FFFFFF;
}
/* other links in the sidebarmenu when hovered */
.skin-blue .main-sidebar .sidebar .sidebar-menu a:hover{
background-color: #FFCC00;
}
/* toggle button when hovered */
.skin-blue .main-header .navbar .sidebar-toggle:hover{
background-color: #223DA4;
}
/* right info toggle button when hovered */
.skin-blue .main-header .navbar .navbar-custom-menu li:hover{
background-color:#223DA4;
}
/* right info toggle button when hovered */
.skin-blue .main-header .navbar .navbar-custom-menu .dropdown-toggle:hover{
background-color:#223DA4;
}
/* body */
.content-wrapper, .right-side {
background-color: #FFFFFE;
}
'))),
#below changes boxes
tags$style(HTML("
.box.box-solid.box-primary>.box-header {
color:#fff;
background:#172869
}
.box.box-solid.box-primary{
border-bottom-color:#FFCC00;
border-left-color:#FFCC00;
border-right-color:#FFCC00;
border-top-color:#FFCC00;
border-bottom-width:2px;
border-left-width:2px;
border-right-width:2px;
border-top-width:2px;
}
")),
#-------tabItems---------------------------------
tabItems(
#-------welcome tab------------------------
tabItem(tabName = "welcome",
fluidPage(
mainPanel(width = 12,
h1("CheckMySchool App Analytics Dashboard", align = "center"),
br(),
h4("This dashboard presents the data analytics for the CheckMySchool mobile app
since its release in April 2019. The data is automatically updated each evening at 10:00 PM PST."),
h4("The goal of the CMS mobile app is to provide
a constructive and collaborative platform for users to provide necessary
feedback to the Department of Education of the Philippines. Principals,
teachers, parents, students, DepEd officials, and other local stakeholders
are able to send feedback in the form of 'submissions' to the proper authorities.
This hopefully will improve the ability of stakeholders to provide valuable
feedback to DepEd in a reasonable timeframe. The app also establishes a platform
for constructive and collaborative discourse/advertisement by allowing users to
see the success stories of school improvement around the Philippines.", align = "justify"),
br(),
fluidRow(width = 12, align = "center",
checkboxInput("condition_agreement", paste0("To ensure the ethical use of the information
presented in this dashboard, CheckMySchool respectfully requires all users
to accept the terms of use outlined in our Code of Conduct."), FALSE, width = "60%"),
a("Click here to view our Code of Conduct",target="_blank",href="CMSapp_code_of_conduct.pdf")
)
) #close mainPanel
) #close fluidPage
), #close tabItem "welcome"
#-------user guide tab-----------------------
tabItem(tabName = "user_guide",# class = "my_style_2",
fluidPage(
mainPanel(width = 12,
fluidRow(width = 12,
column(width = 10, offset = 1,
slickROutput("slickr", height = "60vh")
)
) #close fluidRow
) #close mainPanel
) #close fluidPage
), #close tabItem "user_guide"
#-------quick statistics tab--------------------
tabItem(tabName = "quick_statistics",
fluidPage(
mainPanel(width = 8,
box(width = 12, title = "Statistics at a Glance", solidHeader = TRUE, status = "primary",
tags$head(tags$style(HTML(".small-box {height: 110px}"))),
fluidRow(
column(width = 12,
valueBoxOutput("users", width = 6), #number of app users
valueBoxOutput("uniquereporters", width = 6))), #number of total reporters
fluidRow(
column(width = 12,
valueBoxOutput("numreports") #number of total reports
,valueBoxOutput("numreports_BP") #number of only Best Practice reports
,valueBoxOutput("numreports_nonBP"))), #number of only non-BP reports
fluidRow(
column(width = 12,
valueBoxOutput("numregions") #number of regions present in all reports
,valueBoxOutput("numdivisions") #number of divisions present in all reports
,valueBoxOutput("numschools"))), #number of schools present in all reports
fluidRow(
column(width = 12,
valueBoxOutput("topregion", width = 6) #region with the most reports
,valueBoxOutput("topdivision", width = 6))), #division with the most reports
fluidRow(
column(width = 12,
valueBoxOutput("topschool", width = 6) #school with the most reports
,valueBoxOutput("topcategory", width = 6))) #category with the most reports
)
),
mainPanel(width = 4,
fluidRow(#style = "margin-top:-1.35em;",
box(offset = 0,
title = "Daily Number of Submissions"
,status = "primary"
,solidHeader = TRUE
,width = "300px"
,plotOutput("dailyplot", height = "226px"))),
fluidRow(
box(offset = 0,
title = "Submissions by Region"
,status = "primary"
,solidHeader = TRUE
,width = "300px"
,plotOutput("regionplot", height = "340px")))
),
mainPanel(width = 12,
fluidRow(
column(width = 12,
div(style = "color: grey;",
h5(HTML("The features on this page are only affected by the best practices, Adopt-a-School, and date selectors in the rightsidebar.
The other selectors will not change the statistics shown."))))
))
)
), #close tabItem "quick_statistics"
#-------data explorer tab-------------------
tabItem(tabName = "data_explorer",
fluidPage(
mainPanel(width = 12,
box(title = "Pie chart of the submissions for this selection", solidHeader = TRUE, status = "primary",
plotOutput("categoryplot", height = "410px"),
downloadButton("download_pie", "Download Pie Chart", style='padding:4px; font-size:85%;')
)),
mainPanel(width = 12,
box(title = "Line graph of daily submissions for this selection", width = 10, solidHeader = TRUE, status = "primary",
plotOutput("selectdailyplot", height = "145px"),
downloadButton("download_line", "Download Line Graph", style='padding:4px; font-size:85%; margin-left:1em;')
))
) #close fluidPage
), #close tabItem "data_explorer"
#-------submissions map tab--------------------
tabItem(tabName = "submissions_map",
fluidPage(
sidebarPanel(width = 3,
div(style = "color: red;",
h5(HTML("Coordinate data for some schools are not yet available. Thank you for your patience."))),
div(style="display:inline-block;vertical-align:top;",
fluidRow(
column(4,h5(strong("Region"))),
column(8,actionButton("region_level", "Map!", style='padding:4px; font-size:80%; margin-top:0.5em'))
),
fluidRow(
column(4,h5(strong("Division"))),
column(8,actionButton("division_level", "Map!", style='padding:4px; font-size:80%; margin-top:0.5em'))
),
fluidRow(
column(4,h5(strong("School"))),
column(8,actionButton("school_level", "Map!", style='padding:4px; font-size:80%; margin-top:0.5em'))
),
hr(),
h5(strong(HTML("You can also display a map of School Neediness Index scores:"))),
fluidRow(
column(4,h5(strong("SNI"))),
column(8,actionButton("sni_level", "Map!", style='padding:4px; font-size:80%; margin-top:0.5em'))
)),
hr(),
selectInput(inputId = "selected_specific",
label = div(style = "font-size:13px;", "Find a Specific Location:"),
choices = "default (all)",
selected = "default (all)")
),#close sidebarPanel
mainPanel(width = 7,
box(title = "Map of the user's selection", width = "12", solidHeader = TRUE, status = "primary",
leafletOutput("submap", height = "720px"))
)#close mainPanel
)#close fluidPage
), #close tabItem "submissions_map"
#-------raw data tab-----------------------------
tabItem(tabName = "raw_data",
fluidPage(
sidebarPanel(width = 2,
textInput(inputId = "name_authentication", label = div(style = "font-size:13px;",
'To access and download the raw data, type valid legal name and email.'),
placeholder = "<type name here>", width = 300),
div(style = "margin-top: -0.5em;",
textInput(inputId = "email_authentication", label = NULL,
placeholder = "<type email here>", width = 300)),
downloadButton("download_data", "Download", style='padding:4px; font-size:85%; margin-top: -1em;')
),
mainPanel(width = 8,
box(title = "Preview of the raw data for this selection", width = "12", solidHeader = TRUE, status = "primary",
div(dataTableOutput("categoryreports"),
style='height: 660px;
width: 100%;
table-layout: fixed;
word-wrap: break-word;'
)
)
)
)#close fluidPage
)#close tabItem "raw_data"
) #close tabItems
) #close dashboardBody
) #close dashboardPagePlus
#-------server------------------------
server <- function(input, output, session) {
#-------show/hide selectInputs on rightSidebar for different tabs----------------
observe({
if (input$left_sidebar == "quick_statistics") {
shinyjs::hide("selected_region")
shinyjs::hide("selected_division")
shinyjs::hide("selected_school")
shinyjs::hide("selected_category")
shinyjs::hide("selected_item")
} else if (input$left_sidebar == "submissions_map") {
shinyjs::hide("selected_region")
shinyjs::hide("selected_division")
shinyjs::hide("selected_school")
shinyjs::show("selected_category")
shinyjs::show("selected_item")
} else {
shinyjs::show("selected_region")
shinyjs::show("selected_division")
shinyjs::show("selected_school")
shinyjs::show("selected_category")
shinyjs::show("selected_item")
}
})
#-------open rightSidebar when data explorer, submissions map, or raw data are opened-------------------
observe({
if (input$left_sidebar == "data_explorer" | input$left_sidebar == "submissions_map" | input$left_sidebar == "raw_data") {
shinyjs::addClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
} else {
shinyjs::removeClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
}
})
#-------TAB 2: USER GUIDE-------------------
#----------2A. making the slideshow------------------
slides=c("dashboard_tutorial1","dashboard_tutorial2","dashboard_tutorial3",
"dashboard_tutorial4","dashboard_tutorial5","dashboard_tutorial6")
slides_pics=sprintf("./www/%s.png",slides)
output$slickr <- renderSlickR({
presentation <- slickR(obj=slides_pics)
})
#-------setting up blank theme for future pie charts-------------------
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank()
)
#-------make the data reactive-----------------
reports <- reactive({
reports_reactive <- reports1[which(reports1$Date >= as.Date.character(input$selected_dates[1]) & reports1$Date <= as.Date.character(input$selected_dates[2])),]
#subset by best practices
if (input$selected_BP == "no") {
reports_reactive <- reports_reactive[which(reports_reactive$`Service Classification/Category` != "School Events"),]
} else {
reports_reactive <- reports_reactive
}
#subset by Adopt-a-School
if (input$selected_adopt == "no") {
reports_reactive <- reports_reactive[which(reports_reactive$`Service Classification/Category` != "Adopt-a-School Program"),]
} else {
reports_reactive <- reports_reactive
}
#validate to the code of conduct
validate(
need(input$condition_agreement == TRUE, "To access this information, please agree with our terms of use on the Welcome tab."))
#and the inputted dates
validate(
need(nrow(reports_reactive) != 0, "There are 0 reports with the user's selected combination of region, division, school, best practice status, and dates. Please select a different combination."))
return(reports_reactive)
})
#-------updating selectors------------------------------
#best practice, adopt-a-school, date
observeEvent(input$reset1,{
updateSelectInput(session = session,
inputId = "selected_BP",
label = "Include best practices?",
choices = c("yes", "no"),
selected = "yes")
updateSelectInput(session = session,
inputId = "selected_adopt",
label = "Include Adopt-a-School?",
choices = c("yes", "no"),
selected = "yes")
updateDateRangeInput(session = session,
inputId = "selected_dates",
label = "Timeframe:",
start = as.Date.character("2019-04-06"),
end = Sys.Date(),
min = as.Date.character("2019-04-06"),
max = Sys.Date())
})
#region
observeEvent({
length(input$selected_BP) != 0
length(input$selected_adopt) != 0
length(input$selected_dates) != 0
input$reset1
}, {
updateSelectInput(session = session,
inputId = "selected_region",
label = "Region:",
choices = c("default (all)",sort(unique(as.character(reports()$Region)))),
selected = "default (all)")
}, ignoreNULL = FALSE)
#division
observeEvent(length(input$selected_region) != 0, {
if (input$selected_region == "default (all)") {
updateSelectInput(session = session,
inputId = "selected_division",
label = "Division:",
choices = "default (all)",
selected = "default (all)")
} else {
data_available <- reports()[which(reports()$Region == input$selected_region),]
updateSelectInput(session = session,
inputId = "selected_division",
label = "Division:",
choices = c("default (all)",sort(unique(data_available$Division))),
selected = "default (all)")
}
})
#school
observeEvent(length(input$selected_division) != 0, {
if (input$selected_division == "default (all)") {
updateSelectInput(session = session,
inputId = "selected_school",
label = "School:",
choices = "default (all)",
selected = "default (all)")
} else {
data_available <- reports()[which(reports()$Region == input$selected_region),]
data_available <- data_available[which(data_available$Division == input$selected_division),]
updateSelectInput(session = session,
inputId = "selected_school",
label = "School:",
choices = c("default (all)",sort(unique(data_available$`Unique School`))),
selected = "default (all)")
}
})
#category
observeEvent({
length(input$selected_BP) != 0
length(input$selected_adopt) != 0
length(input$selected_dates) != 0
length(input$selected_region) != 0
length(input$selected_division) != 0
length(input$selected_school) != 0
input$reset1
}, {
data_available <- reports()
if (input$selected_region != "default (all)") {
data_available <- data_available[which(data_available$Region == input$selected_region),]
}
if (input$selected_division != "default (all)") {
data_available <- data_available[which(data_available$Division == input$selected_division),]
}
if (input$selected_school != "default (all)") {
data_available <- data_available[which(data_available$`Unique School` == input$selected_school),]
}
updateSelectInput(session = session,
inputId = "selected_category",
label = "Service Classification/Category:",
choices = c("default (all)",sort(unique(data_available$`Service Classification/Category`))),
selected = "default (all)")
}, ignoreNULL = FALSE)
#item
observeEvent(length(input$selected_category) != 0,{
if (input$selected_category == "default (all)") {
updateSelectInput(session = session,
inputId = "selected_item",
label = "Service Item:",
choices = "default (all)",
selected = "default (all)")
} else {
data_available <- reports()
if (input$selected_region != "default (all)") {
data_available <- data_available[which(data_available$Region == input$selected_region),]
}
if (input$selected_division != "default (all)") {
data_available <- data_available[which(data_available$Division == input$selected_division),]
}
if (input$selected_school != "default (all)") {
data_available <- data_available[which(data_available$`Unique School` == input$selected_school),]
}
data_available <- data_available[which(data_available$`Service Classification/Category` == input$selected_category),]
updateSelectInput(session = session,
inputId = "selected_item",
label = "Service Item:",
choices = c("default (all)",sort(unique(data_available$`Service Item`))),
selected = "default (all)")
}
})
#-------TAB 3: QUICK STATISTICS---------------------
#----------3A. making frequency tables--------------------------
#-------------i. region table (for statistic boxes and pie chart of reports by region)--------------------
#make a table of total reports by region
region_summary <- reactive({
region_summary <- table(reports()$Region)
region_summary <- as.data.frame(region_summary)
colnames(region_summary) <- c("Region", "Reports")
region_summary <- region_summary %>% arrange(desc(Reports))
#make percentage column (it'll improve understandability of the pie chart)
region_summary <- region_summary %>% mutate(Percentage = plyr::round_any((Reports / sum(Reports))*100, accuracy=.01, f=floor))
region_summary$`Region Percentage` <- paste0(region_summary$Region, " - ", region_summary$Percentage, '%') #treated as text data (not numeric) after this
return(region_summary)
})
#-------------ii. division table (for statistic boxes)--------------------------
#make a table of total reports by division
division_summary <- reactive({
division_summary <- table(reports()$Division)
division_summary <- as.data.frame(division_summary)
colnames(division_summary) <- c("Division", "Reports")
division_summary <- division_summary %>% arrange(desc(Reports))
return(division_summary)
})
#-------------iii. school table (for statistic boxes)--------------------------
#make a table of total reports by school
school_summary <- reactive({
school_summary <- table(reports()$`Unique School`)
school_summary <- as.data.frame(school_summary)
colnames(school_summary) <- c("School", "Reports")
school_summary <- school_summary %>% arrange(desc(Reports))
return(school_summary)
})
#-------------iv. category table (for statistic boxes)-------------------
#make a table of total reports by category
category_summary <- reactive({
category_summary <- table(reports()$`Service Classification/Category`)
category_summary <- as.data.frame(category_summary)
colnames(category_summary) <- c("Category", "Reports")
category_summary <- category_summary %>% arrange(desc(Reports))
return(category_summary)
})
#----------3B. statistic boxes-------------------
##total number of users
user_data <- reactive({
validate(
need(input$condition_agreement == TRUE, "To access this information, please agree with our terms of use on the Welcome tab."))
user_data <- 1808 #added manually lol. this needs to be scraped in the future
user_data
})
output$users <- renderValueBox({
valueBox(
formatC(user_data(), format="d", big.mark=',')
,paste0('Recorded Users')
,icon = icon("user",lib='glyphicon')
,color = 'red'
)
})
##total number of unique reporters
output$uniquereporters <- renderValueBox({
valueBox(
formatC(length(unique(reports()$`Reported By`)), format="d", big.mark=',')
,paste0('Recorded Submitters')
,icon = icon("user",lib='glyphicon')
,color = 'red'
)
})
##total number of reports
output$numreports <- renderValueBox({
valueBox(
formatC(nrow(reports()), format="d", big.mark=',')
,HTML('Total <br/> Submissions')
,icon = icon("stats",lib='glyphicon')
)
})
##total number of BP reports
output$numreports_BP <- renderValueBox({
valueBox(
formatC(nrow(reports()[which(reports()$`Service Classification/Category` == "School Events"),]), format="d", big.mark=',')
,HTML('Best <br/> Practices')
,icon = icon("stats",lib='glyphicon')
)
})
##total number of non-BP reports
output$numreports_nonBP <- renderValueBox({
valueBox(
formatC(nrow(reports()) - (nrow(reports()[which(reports()$`Service Classification/Category` == "School Events"),])), format="d", big.mark=',') #may need to change these to as.factor
,HTML('School <br/> Issues')
,icon = icon("stats",lib='glyphicon')
)
})
##total number of regions with reports
output$numregions <- renderValueBox({
valueBox(
formatC(nrow(region_summary()), format="d", big.mark=',')
,HTML('Regions <br/> with submissions')
,icon = icon("stats",lib='glyphicon')
,color = 'yellow'
)
})