-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.R
177 lines (148 loc) · 4.28 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
source("R/loaders.R")
source('R/standardizers.R')
source('R/deduplicaters.R')
source("R/processors.R")
source("R/utilities.R")
source("R/runner.R")
source("config.R")
manage_run <- function() {
# Change Defaults if COMPLETE_RUN is set.
# ===
if (COMPLETE_RUN) {
REFRESH <- TRUE
COMPANY_TEST <- FALSE
MUNI_IDS <- NULL
ROUTINES <- list(
load = TRUE,
proc = TRUE,
dedupe = TRUE
)
}
# Open Log
# ===
lf <- logr::log_open(
format(Sys.time(), "%Y-%m-%d_%H%M%S"),
logdir = TRUE
)
on.exit(logr::log_close())
# Print splash screen to log.
# ===
util_print_splash()
# Test Validity of (and Zero-Pad) Municipality IDs
# ===
MUNI_IDS <- util_test_muni_ids(
muni_ids=MUNI_IDS,
path=DATA_PATH,
quiet=QUIET
)
# Test DB Values and Connections
# ===
db_vals <- unique(unlist(unname(PUSH_DBS)))
if(!all(stringr::str_detect(db_vals, "^[a-zA-Z\\_]*$"))) {
stop("VALIDATION: You provided invalid database prefixes---must be made up of characters and underscores.")
}
for (db in db_vals) {
util_test_conn(db)
}
rm(db_vals)
# Test Thresholds
threshes <- c(COSINE_THRESH, INDS_THRESH, ZIP_INT_THRESH)
if (any(threshes > 1)) {
stop("VALIDATION: COSINE_THRESH, INDS_THRESH, and ZIP_INT_THRESH must be less than 1.")
} else {
util_log_message("VALIDATION: COSINE_THRESH, INDS_THRESH, and ZIP_INT_THRESH are valid!")
}
rm(threshes)
# Test CRS
# ===
if(suppressWarnings(is.na(sf::st_crs(CRS)$input))) {
stop("VALIDATION: You provided an invalid CRS. Check config.R.")
} else {
util_log_message("VALIDATION: CRS is valid!")
}
# Test OC Path
if (!is.null(OC_PATH)) {
oc_exists <- dir.exists(file.path(DATA_PATH, OC_PATH))
if (!oc_exists) {
stop("VALIDATION: You provided an invalid OC_PATH. Check config.R.")
} else {
companies_exist <- any(stringr::str_detect(
list.files(file.path(DATA_PATH, OC_PATH)),
"companies.csv"
))
officers_exist <- any(stringr::str_detect(
list.files(file.path(DATA_PATH, OC_PATH)),
"officers.csv"
))
if(!(companies_exist & officers_exist)) {
stop("VALIDATION: OC_PATH is a folder but doesn't contain 'companies.csv' and 'officers.csv'. Check config.R.")
} else {
util_log_message("VALIDATION: OC_PATH is valid! It contains 'companies.csv' and 'officers.csv'")
}
rm(companies_exist, officers_exist)
}
rm(oc_exists)
} else {
util_log_message("VALIDATION: Passed NULL to OC_PATH. Will run without OC.")
}
# Test GDB Path
as_file <- file.exists(file.path(DATA_PATH, GDB_PATH))
as_folder <- dir.exists(file.path(DATA_PATH, GDB_PATH))
if(!as_file & !as_folder) {
stop("VALIDATION: You provided an invalid GDB_PATH. Check config.R.")
}
rm(as_file)
if(as_folder) {
if(
!any(
stringr::str_detect(
list.files(file.path(DATA_PATH, GDB_PATH)),
".gdb")
)
) {
stop("VALIDATION: GDB_PATH is a folder but contains no GDBs. Check config.R.")
}
}
util_log_message("VALIDATION: GDB_PATH is valid!")
rm(as_folder)
# Confirm With User if More Intensive Config Options are Set
# ===
if(!util_prompts(REFRESH, MUNI_IDS, COMPANY_TEST)) {
return(invisible(NULL))
}
# Quick confirmation.
# ===
if(!util_prompt_check("VALIDATION: Preflight complete! Are you ready to begin the process? (Y/N)")) {
return(invisible(NULL))
}
run(
data_path=DATA_PATH,
muni_ids=MUNI_IDS,
refresh=REFRESH,
crs=CRS,
gdb_path=GDB_PATH,
oc_path=OC_PATH,
most_recent=MOST_RECENT,
thresh=COSINE_THRESH,
inds_thresh=INDS_THRESH,
zip_int_thresh=ZIP_INT_THRESH,
routines=ROUTINES,
company_test=COMPANY_TEST,
company_test_count=COMPANY_TEST_COUNT,
push_dbs=PUSH_DBS,
return_intermediate=RETURN_INTERMEDIATE,
quiet=QUIET
)
}
# This is like if __name__ == "__main__" in python.
if (!interactive()) {
invisible(manage_run())
util_log_message("CLOSING R SESSION.", header=TRUE)
} else {
results <- manage_run()
if (!is.null(results)) {
util_log_message("ASSIGNING R OBJECTS.", header=TRUE)
for(i in 1:length(results)) assign(names(results)[i], results[[i]])
}
rm(results)
}