-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.R
204 lines (134 loc) · 6.04 KB
/
run.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
# Setup ------------------------------------------------------------------------
message('---> Setting things up')
# Load packages
suppressPackageStartupMessages({
library(here)
library(openxlsx)
library(dplyr)
library(stringr)
library(purrr)
library(readr)
library(stringdist)
library(janitor)
library(readxl)
})
# Setup paths for easy access
dirs <- list()
dirs$working_dir <- here()
project_name <- dirs$working_dir |>
list.files(pattern = '.Rproj') |>
str_replace('.Rproj', '')
# Find data path based on project name
working_dir_split <- dirs$working_dir |> str_split('/') |> unlist()
master_dir_pos <- which(working_dir_split == project_name)[1]
dirs$master_dir <- paste(working_dir_split[1:master_dir_pos], collapse = '/')
dirs$data <- file.path(dirs$master_dir, 'data')
dirs$functions <- file.path(dirs$working_dir, 'functions')
dirs$output <- file.path(dirs$master_dir, 'output')
if (!dir.exists(dirs$output)) {
message('---> Created output folder ', dirs$output)
dir.create(dirs$output)
}
# Read in custom functions
function_files <- list.files(dirs$functions, full.names = TRUE, pattern = '.R')
walk(function_files, source)
function_files_short <- list.files(dirs$functions, pattern = '.R')
message('---> Loaded custom function files: ',
paste(function_files_short, collapse = ', '))
# Data prep --------------------------------------------------------------------
message('---> Preparing the data')
# If you have an Excel file open on a Mac a temporary copy is saved in the same
# folder with this prefix: '~$'. The str_subset line drops this out.
files <- list.files(dirs$data, pattern = '_input.xlsx',
full.names = TRUE) |>
str_subset('\\~\\$', negate = TRUE)
la_names <- dirs$data |>
list.files(pattern = '_input.xlsx') |>
str_subset('\\~\\$', negate = TRUE) |>
str_replace('_input.xlsx', '')
schools_data <- files |>
map(read_excel) |>
map(check_input_data) |>
map2_dfr(la_names,
~mutate(.x,
local_authority = .y,
`Seed Code` = as.character(`Seed Code`))) |>
as_tibble() |>
clean_names() |>
select(-contains('not_known')) |>
mutate(across(c(sector, seed_code, school_name), str_trim)) |>
mutate(school_id = paste0(local_authority, '_', sector, '_', seed_code))
primary_data <- filter(schools_data, sector == 'Primary')
secondary_data <- filter(schools_data, sector == 'Secondary')
# Specify number of comparisons ------------------------------------------------
num_comparisons_primary_overall <- 10
num_comparisons_secondary_overall <- 10
num_comparisons_primary_la <- 10
num_comparisons_secondary_la <- 5
# Overall comparison -----------------------------------------------------------
message('---> Processing for all schools')
# Primaries
primary_inputs <- build_inputs(primary_data)
primary_tables <- calc_distances(primary_inputs,
num_comparisons_primary_overall)
primary_output <- format_tables(primary_tables, primary_data)
file_primary_output_xlsx <- file.path(dirs$output,
'All LAs_primary-schools.xlsx')
file_primary_output_csv <- str_replace(file_primary_output_xlsx,
'.xlsx', '.csv')
write.xlsx(primary_output, file_primary_output_xlsx)
write_csv(primary_output, file_primary_output_csv, progress = FALSE)
# Secondaries
secondary_inputs <- build_inputs(secondary_data)
secondary_tables <- calc_distances(secondary_inputs,
num_comparisons_secondary_overall)
secondary_output <- format_tables(secondary_tables, secondary_data)
file_secondary_output_xlsx <- file.path(dirs$output,
'All LAs_secondary-schools.xlsx')
file_secondary_output_csv <- str_replace(file_secondary_output_xlsx,
'.xlsx', '.csv')
write.xlsx(secondary_output, file_secondary_output_xlsx)
write_csv(secondary_output, file_secondary_output_csv, progress = FALSE)
# Within LA comparison ---------------------------------------------------------
message('---> Processing for individual local authorities')
num_las <- length(la_names)
primary_output_la <- vector('list', length = num_las)
secondary_output_la <- vector('list', length = num_las)
names(primary_output_la) <- la_names
names(secondary_output_la) <- la_names
for (i in 1:num_las) {
# Primary Schools
primary_data_la <- filter(primary_data,
local_authority == la_names[i])
primary_inputs_la <- build_inputs(primary_data_la)
primary_tables_la <- calc_distances(primary_inputs_la,
num_comparisons_primary_la)
primary_output_la[[i]] <- format_tables(primary_tables_la, primary_data_la)
file_primary_output_la_xlsx <- file.path(
dirs$output,
paste0(la_names[i], '_primary-schools.xlsx')
)
file_secondary_output_la_csv <- str_replace(file_primary_output_la_xlsx,
'.xlsx', '.csv')
write.xlsx(primary_output_la[[i]], file_primary_output_la_xlsx)
write_csv(primary_output_la[[i]], file_secondary_output_la_csv,
progress = FALSE)
# Secondary Schools
secondary_data_la <- filter(secondary_data,
local_authority == la_names[i])
secondary_inputs_la <- build_inputs(secondary_data_la)
secondary_tables_la <- calc_distances(secondary_inputs_la,
num_comparisons_secondary_la)
secondary_output_la[[i]] <- format_tables(secondary_tables_la,
secondary_data_la)
file_secondary_output_la_xlsx <- file.path(
dirs$output,
paste0(la_names[i], '_secondary-schools.xlsx')
)
file_secondary_output_la_csv <- str_replace(file_secondary_output_la_xlsx,
'.xlsx', '.csv')
write.xlsx(secondary_output_la[[i]], file_secondary_output_la_xlsx)
write_csv(secondary_output_la[[i]], file_secondary_output_la_csv,
progress = FALSE)
}
message('---> Finished')