generated from dfe-analytical-services/shiny-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.R
215 lines (165 loc) · 7.48 KB
/
global.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
# ---------------------------------------------------------
# This is the global file.
# Use it to store functions, library calls, source files etc.
# Moving these out of the server file and into here improves performance
# The global file is run only once when the app launches and stays consistent across users
# whereas the server and UI files are constantly interacting and responsive to user input.
#
# ---------------------------------------------------------
# Library calls ---------------------------------------------------------------------------------
shhh <- suppressPackageStartupMessages # It's a library, so shhh!
shhh(library(shiny))
shhh(library(shinyjs))
shhh(library(tools))
shhh(library(testthat))
shhh(library(shinytest))
shhh(library(shinydashboard))
shhh(library(shinyWidgets))
shhh(library(shinyGovstyle))
shhh(library(dplyr))
shhh(library(tidyr))
shhh(library(ggplot2))
shhh(library(plotly))
shhh(library(DT))
shhh(library(xfun))
shhh(library(metathis))
shhh(library(shinyalert))
shhh(library(checkmate))
shhh(library(stringr))
# shhh(library(shinya11y))
# Functions ---------------------------------------------------------------------------------
# Here's an example function for simplifying the code needed to commas separate numbers:
# cs_num ----------------------------------------------------------------------------
# Comma separating function
cs_num <- function(value) {
format(value, big.mark = ",", trim = TRUE)
}
# tidy_code_function -------------------------------------------------------------------------------
# Code to tidy up the scripts.
tidy_code_function <- function() {
message("----------------------------------------")
message("App scripts")
message("----------------------------------------")
app_scripts <- eval(styler::style_dir(recursive = FALSE)$changed)
message("R scripts")
message("----------------------------------------")
r_scripts <- eval(styler::style_dir("R/")$changed)
message("Test scripts")
message("----------------------------------------")
test_scripts <- eval(styler::style_dir("tests/", filetype = "r")$changed)
script_changes <- c(app_scripts, r_scripts, test_scripts)
return(script_changes)
}
# Source scripts ---------------------------------------------------------------------------------
# Source any scripts here. Scripts may be needed to process data before it gets to the server file.
# It's best to do this here instead of the server file, to improve performance.
# source("R/filename.r")
# appLoadingCSS ----------------------------------------------------------------------------
# Set up loading screen
appLoadingCSS <- "
#loading-content {
position: absolute;
background: #000000;
opacity: 0.9;
z-index: 100;
left: 0;
right: 0;
height: 100%;
text-align: center;
color: #FFFFFF;
}
"
site_primary <- "https://department-for-education.shinyapps.io/pupil-yields-dashboard/"
site_overflow <- "https://department-for-education.shinyapps.io/dfe-shiny-template-overflow/"
sites_list <- c(site_primary) # We can add further mirrors where necessary. Each one can generally handle about 2,500 users simultaneously
ees_pub_name <- "Statistical publication" # Update this with your parent publication name (e.g. the EES publication)
ees_publication <- "https://explore-education-statistics.service.gov.uk/find-statistics/pupil-yield-from-housing-developments/" # Update with parent publication link
google_analytics_key <- "Z967JJVQQX"
source("R/read_data.R")
la_lad_lookup <- read.csv("data/la_lad_hierarchy.csv", stringsAsFactors = F) %>%
mutate(
la_name = gsub(",", "", la_name),
lad_name = gsub(",", "", lad_name),
date_of_introduction = as.Date(date_of_introduction),
date_of_termination = as.Date(date_of_termination)
) %>%
filter(
status == "live" | date_of_termination >= as.Date("2023-03-31"),
date_of_introduction <= as.Date("2023-03-31") | is.na(date_of_introduction)
)
# Read in the data
df_py <- read_data()
df_ehcp <- read_ehcp()
df_pc <- read_pc()
# Create clean versions of the file for download--------------
df_py_download <- read_data()
df_ehcp_download <- read_ehcp()
df_pc_download <- read_pc()
# renames the columns of the old data set using the lookup table
df_py_download <- data.table::setnames(df_py_download, old = metadata_PY$programmer_friendly_names, new = metadata_PY$user_friendly_name, skip_absent = TRUE)
df_ehcp_download <- data.table::setnames(df_ehcp_download, old = metadata_EHCP$programmer_friendly_names, new = metadata_EHCP$user_friendly_name, skip_absent = TRUE)
df_pc_download <- data.table::setnames(df_pc_download, old = metadata_PC$programmer_friendly_names, new = metadata_PC$user_friendly_name, skip_absent = TRUE)
# Get geographical levels from data
df_py$education_phase <- factor(df_py$education_phase, levels = )
choicesgeographic_level <- c("England", "County/Unitary", "District")
choicesLAs <- df_py %>%
filter(geographic_level == "County/Unitary" | la_name == "Cardiff") %>%
pull(la_name) %>%
unique() %>%
sort()
choicesLADs <- df_py %>%
filter(geographic_level == "District") %>%
pull(la_name) %>%
unique() %>%
sort()
choicesYears <- unique(df_py$time_period) %>% sort(decreasing = TRUE)
df_py$time_period <- factor(df_py$time_period, levels = choicesYears %>% sort())
filter_list <- data.frame(
name = c("School phase", "School type", "Number of bedrooms", "Housing type", "Tenure"),
colid = c("education_phase", "education_type", "number_of_bedrooms", "housing", "tenure"),
default = c("Primary", "Mainstream", "All", "All", "All")
)
choiceseducation_type <- unique(df_py$education_type) %>% sort()
choicesPhase <- c("Early Years", "Primary", "Secondary", "Post-16", "Special Schools/AP")
df_py$education_phase <- factor(df_py$education_phase, levels = choicesPhase)
choiceshousing <- c("All", levels(df_py$housing)) %>% unique()
choicestenure <- c("All", levels(df_py$tenure)) %>% unique()
choicesnumber_beds <- c("All", unique(df_py$number_of_bedrooms) %>% sort()) %>% unique()
choices_default <- list(
education_type = choiceseducation_type,
education_phase = choicesPhase,
housing = choiceshousing,
tenure = choicestenure,
number_of_bedrooms = choicesnumber_beds
)
# post completion data
# Get geographical levels from data
df_pc$education_phase <- factor(df_pc$education_phase, levels = )
choicesgeographic_levelpc <- c("England", "County/Unitary", "District")
choicesLAs <- df_pc %>%
filter(geographic_level == "County/Unitary" | la_name == "Cardiff") %>%
pull(la_name) %>%
unique() %>%
sort()
choicesLADspc <- df_pc %>%
filter(geographic_level == "District") %>%
pull(la_name) %>%
unique() %>%
sort()
choicesYearspc <- unique(df_pc$time_period) %>% sort(decreasing = TRUE)
df_pc$time_period <- factor(df_pc$time_period, levels = choicesYearspc %>% sort())
filter_listpc <- data.frame(
name = c("School phase", "Years After Completion"),
colid = c("education_phase", "years_after_completion"),
default = c("Primary", "All")
)
choicesPhasepc <- c("Early Years", "Primary", "Secondary", "Post-16", "SP/Alternative")
df_pc$education_phasepc <- factor(df_pc$education_phase, levels = choicesPhasepc)
choicesYearsAfterCompletion <- unique(df_pc$years_after_completion) %>% sort(decreasing = TRUE)
df_pc$years_after_completion <- factor(df_pc$years_after_completion, levels = choicesYearsAfterCompletion %>% sort())
choicespc <- list(
education_phase = choicesPhasepc,
time_period = choicesYearspc
)
dfe_palette <- c("#12436D", "#28A197", "#801650", "#F46A25", "#3D3D3D", "#A285D1")
technical_table <- read.csv("data/TechnicalTable.csv")