Skip to content

Commit 9d24260

Browse files
committed
trycatch errors
1 parent 211f628 commit 9d24260

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

R/domain_mapping.R

+51-24
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#' @importFrom utils read.csv write.csv
1919

2020
domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file = NULL) {
21-
# Load data: Check if demo data should be used
21+
22+
## Load data: Check if demo data should be used ----
23+
2224
if (is.null(json_file) && is.null(domain_file)) {
2325
# If both json_file and domain_file are NULL, use demo data
2426
meta_json <- get("json_metadata")
@@ -50,19 +52,19 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
5052
print(lookup)
5153
}
5254

53-
# Present domains plots panel for user's reference ----
55+
## Present domains plots panel for user's reference ----
5456
graphics::plot.new()
5557
domains_extend <- rbind(c("*NO MATCH / UNSURE*"), c("*METADATA*"), c("*ALF ID*"), c("*OTHER ID*"), c("*DEMOGRAPHICS*"), domains)
5658
gridExtra::grid.table(domains_extend[1], cols = "Domain", rows = 0:(nrow(domains_extend) - 1))
5759

58-
# Get user and demo list info for log file ----
60+
## Get user and demo list info for log file ----
5961
User_Initials <- ""
6062
cat("\n \n")
6163
while (User_Initials == "") {
6264
User_Initials <- readline("Enter your initials: ")
6365
}
6466

65-
# Print information about Dataset ----
67+
## Print information about Dataset ----
6668
cli_h1("Dataset Name")
6769
cat(meta_json$dataModel$label, fill = TRUE)
6870
cli_h1("Dataset Last Updated")
@@ -82,6 +84,8 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
8284
readline(prompt = "Press any key to proceed")
8385
}
8486

87+
## Ask user which tables to process ----
88+
8589
nTables <- length(meta_json$dataModel$childDataClasses)
8690
cat("\n")
8791
cli_alert_info("Found {nTables} Table{?s} in this Dataset")
@@ -91,14 +95,20 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
9195
}
9296

9397
nTables_Process <- numeric(0)
94-
while (length(nTables_Process) == 0) {
95-
cat("\n \n")
96-
cli_alert_info("Enter each table number you want to process in this interactive session.")
97-
cat("\n")
98-
nTables_Process <- scan(file="",what=0)
98+
nTables_Process_Error <- TRUE
99+
nTables_Process_OutOfRange <- FALSE
100+
while (length(nTables_Process) == 0 | nTables_Process_Error==TRUE | nTables_Process_OutOfRange == TRUE) {
101+
tryCatch({
102+
cat("\n \n");
103+
cli_alert_info("Enter each table number you want to process in this interactive session:");
104+
cat("\n");
105+
nTables_Process <- scan(file="",what=0);
106+
nTables_Process_Error <- FALSE;
107+
nTables_Process_OutOfRange = any(nTables_Process > nTables)},
108+
error=function(e) {nTables_Process_Error <- TRUE; print(e); cat("\n"); cli_alert_danger('Your input is in the wrong format, reference the table numbers and try again')})
99109
}
100110

101-
# Extract each Table
111+
# Extract each Table ----
102112
for (dc in nTables_Process) {
103113
cat("\n")
104114
cli_alert_info("Processing Table {dc} of {nTables}")
@@ -185,18 +195,26 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
185195
}
186196
} # end of loop for DataElement
187197

188-
# Print the AUTO CATEGORISED responses for this Table - request review
198+
## Print the AUTO CATEGORISED responses for this Table and request review ----
189199
Output_auto <- subset(Output, Note == 'AUTO CATEGORISED')
190200
cat("\n \n")
191201
cli_alert_warning("Please check the auto categorised data elements are accurate for table {meta_json$dataModel$childDataClasses[[dc]]$label}:")
192202
cat("\n \n")
193203
print(Output_auto[, c("DataElement", "Domain_code")])
194204

195-
auto_row <- numeric(0)
196-
cat("\n \n")
197-
cli_alert_info("Press enter to accept these auto categorisations, or enter each row number you'd like to edit:")
198-
cat("\n")
199-
auto_row <- scan(file="",what=0)
205+
# extract the rows to edit
206+
auto_row_Error <- TRUE
207+
auto_row_OutOfRange <- FALSE
208+
while (auto_row_Error==TRUE | auto_row_OutOfRange == TRUE) {
209+
tryCatch({
210+
cat("\n \n");
211+
cli_alert_info("Press enter to accept the auto categorisations for table {meta_json$dataModel$childDataClasses[[dc]]$label} or enter each row you'd like to edit:");
212+
cat("\n");
213+
auto_row <- scan(file="",what=0);
214+
auto_row_Error <- FALSE;
215+
auto_row_OutOfRange = any(auto_row > nrow(selectTable_df))},
216+
error=function(e) {auto_row_Error <- TRUE; print(e); cat("\n"); cli_alert_danger('Your input is in the wrong format, reference the row numbers and try again')})
217+
}
200218

201219
if (length(auto_row) != 0) {
202220

@@ -210,7 +228,7 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
210228
}
211229
}
212230

213-
# Ask if user wants to review their responses for this Table
231+
## Ask if user wants to review their responses for this Table ----
214232
review_cats <- ""
215233
while (review_cats != "Y" & review_cats != "N") {
216234
cat("\n \n")
@@ -222,11 +240,20 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
222240
cat("\n \n")
223241
print(Output_not_auto[, c("DataElement", "Domain_code")])
224242
cat("\n \n")
225-
not_auto_row <- numeric(0)
226-
cat("\n \n")
227-
cli_alert_info("Press enter to accept your categorisations for table {meta_json$dataModel$childDataClasses[[dc]]$label}, or enter each row number you'd like to edit:")
228-
cat("\n")
229-
not_auto_row <- scan(file="",what=0)
243+
244+
# extract the rows to edit
245+
not_auto_row_Error <- TRUE
246+
not_auto_row_OutOfRange <- FALSE
247+
while (not_auto_row_Error==TRUE | not_auto_row_OutOfRange == TRUE) {
248+
tryCatch({
249+
cat("\n \n");
250+
cli_alert_info("Press enter to accept your categorisations for table {meta_json$dataModel$childDataClasses[[dc]]$label} or enter each row you'd like to edit:");
251+
cat("\n");
252+
not_auto_row <- scan(file="",what=0);
253+
not_auto_row_Error <- FALSE;
254+
not_auto_row_OutOfRange = any(not_auto_row > nrow(selectTable_df))},
255+
error=function(e) {not_auto_row_Error <- TRUE; print(e); cat("\n"); cli_alert_danger('Your input is in the wrong format, reference the row numbers and try again')})
256+
}
230257

231258
if (length(not_auto_row) != 0) {
232259

@@ -241,15 +268,15 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
241268
}
242269
}
243270

244-
# Fill in columns that have all rows identical
271+
## Fill in columns that have all rows identical ----
245272
Output$Initials <- User_Initials
246273
Output$MetaDataVersion <- meta_json$dataModel$documentationVersion
247274
Output$MetaDataLastUpdated <- meta_json$dataModel$lastUpdated
248275
Output$DomainListDesc <- DomainListDesc
249276
Output$Dataset <- meta_json$dataModel$label
250277
Output$Table <- meta_json$dataModel$childDataClasses[[dc]]$label
251278

252-
# Save final categorisations for this Table
279+
## Save final categorisations for this Table ----
253280
utils::write.csv(Output, output_fname, row.names = FALSE)
254281
cat("\n")
255282
cli_alert_success("Your final categorisations have been saved to {output_fname}")

0 commit comments

Comments
 (0)