-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path07a_build_master.R
77 lines (60 loc) · 3.26 KB
/
07a_build_master.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
#SET UP
rm(list = ls())
source("00_setup_and_funs.R")
memory.limit(size=1000000)
# SET INPUT PARAMETERS
purpose <- "commute"
purpose_private <- paste0(purpose, "_private")
geography <- "lsoa"
init_region("pct_regions", geography, purpose) # Define region type and projection, import local authorities
init_outputs_national(purpose, geography) # Load national data. NB only need z_all if just doing region stats file
init_Rsmall(purpose) # Load R small codebooks
#########################
### READ IN REGIONS AND RUN BUILD MASTER FOR EACH
#########################
# DEFINE WHICH REGIONS TO BUILD [modify in input csv file]
regions_tobuild <- as.character(build_params$region_name[build_params$to_rebuild==1])
for(k in 1:length(regions_tobuild)){
# SUBSET TO THE SELECTED REGION
region <- regions_tobuild[k]
region_build_param <- build_params[build_params$region_name == region,]
region_lad_lookup <- pct_regions_lad_lookup[pct_regions_lad_lookup$region_name==region,]
# BUILD THE REGION
message(paste0("Building for ", region, " (region ", k, ") at ",Sys.time()))
if (purpose=="commute") {
maxdist_online <- 10
source("07b_commute_build_region.R") # comment out to skip build entirely
} else if (purpose=="school") {
source("07c_school_build_region.R") # comment out to skip build entirely
}
# WRITE REGION STATS FILE
# Create region stats directory if not there
if(!dir.exists(file.path(path_shiny, "regions_www/tabs/region_stats", purpose))) { dir.create(file.path(path_shiny, "regions_www/tabs/region_stats", purpose)) }
if(!dir.exists(file.path(path_shiny, "regions_www/tabs/region_stats", purpose, geography))) { dir.create(file.path(path_shiny, "regions_www/tabs/region_stats", purpose, geography)) }
if(!dir.exists(file.path(path_shiny, "regions_www/tabs/region_stats", purpose, geography, region))) { dir.create(file.path(path_shiny, "regions_www/tabs/region_stats", purpose, geography, region)) }
# Knit file
knitr::knit2html(quiet = T,
input = file.path(purpose, geography, "region_stats.Rmd"),
output = file.path(path_shiny, "regions_www/tabs/region_stats", purpose, geography, region, "region_stats.html"),
envir = globalenv(), force_v1 = T
)
# Re read region stats file and tidy
region_stats <- readLines(file.path(path_shiny, "regions_www/tabs/region_stats", purpose, geography, region, "region_stats.html"))
region_stats <- remove_style(region_stats) # remove style section
region_stats <- gsub("<table>", "<table class='region_stats_table'>", region_stats)
# Change 'Wales region' to 'Wales'
if (region == "wales") {
region_stats <- gsub("The Wales region", "Wales", region_stats)
}
write(region_stats, file.path(path_shiny, "regions_www/tabs/region_stats", purpose, geography, region, "region_stats.html"))
message(paste0("Finished ", region," at ",Sys.time()))
}
# TEST RNETS VISUALLY, ONE BY ONE
# for(k in 1) {
# region <- regions_tobuild[k]
# rnet <- readRDS(file.path(path_outputs_regional_R, purpose, geography, region, "rnet.Rds"))
# plot(rnet)
# }
# DELETE THE TEMP FILES CREATED BY MARKDOWN DOC
unlink("figure", recursive=TRUE)
file.remove("region_stats.md")