Skip to content

Commit

Permalink
validate user input
Browse files Browse the repository at this point in the history
  • Loading branch information
RayStick committed Jul 4, 2024
1 parent 3532aa5 commit 0dc8c36
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
6 changes: 3 additions & 3 deletions R/domain_mapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
Output <- rbind(Output,this_Output)
} else {
# collect user responses
decision_output <- user_categorisation(selectTable_df$Label[datavar],selectTable_df$Description[datavar],selectTable_df$Type[datavar])
decision_output <- user_categorisation(selectTable_df$Label[datavar],selectTable_df$Description[datavar],selectTable_df$Type[datavar],max(Code$Code))
# input user responses into output
this_Output <- row_Output
this_Output[nrow(this_Output) + 1 , ] <- NA
Expand Down Expand Up @@ -254,7 +254,7 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
for (datavar_auto in unique(auto_row)) {

# collect user responses
decision_output <- user_categorisation(selectTable_df$Label[datavar_auto],selectTable_df$Description[datavar_auto],selectTable_df$Type[datavar_auto])
decision_output <- user_categorisation(selectTable_df$Label[datavar_auto],selectTable_df$Description[datavar_auto],selectTable_df$Type[datavar_auto],max(Code$Code))
# input user responses into output
Output$Domain_code[datavar_auto] <- decision_output$decision
Output$Note[datavar_auto] <- decision_output$decision_note
Expand Down Expand Up @@ -295,7 +295,7 @@ domain_mapping <- function(json_file = NULL, domain_file = NULL, look_up_file =
for (datavar_not_auto in unique(not_auto_row)) {

# collect user responses
decision_output <- user_categorisation(selectTable_df$Label[datavar_not_auto],selectTable_df$Description[datavar_not_auto],selectTable_df$Type[datavar_not_auto])
decision_output <- user_categorisation(selectTable_df$Label[datavar_not_auto],selectTable_df$Description[datavar_not_auto],selectTable_df$Type[datavar_not_auto],max(Code$Code))
# input user responses into output
Output$Domain_code[datavar_not_auto] <- decision_output$decision
Output$Note[datavar_not_auto] <- decision_output$decision_note
Expand Down
30 changes: 25 additions & 5 deletions R/user_categorisation.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#' @param data_element Name of the variable
#' @param data_desc Description of the variable
#' @param data_type Data type of the variable
#' @param domain_code_max Max code in the domain list (0-3 auto included, then N included via domain_file)
#' @return It returns a list containing the decision and decision note
#' @export

user_categorisation <- function(data_element,data_desc,data_type) {
user_categorisation <- function(data_element,data_desc,data_type,domain_code_max) {

first_run = TRUE
go_back = ''
Expand All @@ -25,21 +26,40 @@ user_categorisation <- function(data_element,data_desc,data_type) {
"\n\nDATA TYPE -----> ", data_type, "\n"
))

# ask user for categorisation
# ask user for categorisation:

decision <- ""
validated = FALSE
cat("\n \n")
while (decision == "") {

while (decision == "" | validated == FALSE) {
decision <- readline("Categorise data element into domain(s). E.g. 3 or 3,4: ")

# validate input given by user
decision_int <- as.integer(unlist(strsplit(decision,",")))
decision_int_NA <- any(is.na((decision_int)))
decision_int_max <- max(decision_int,na.rm=TRUE)
decision_int_min <- min(decision_int,na.rm=TRUE)
if (decision_int_NA == TRUE | decision_int_max > domain_code_max | decision_int_min < 0){
cli_alert_warning("Formatting is invalid or integer out of range. Provide one integer or a comma seperated list of integers.")
validated = FALSE}
else {
validated = TRUE
# standardize output
decision_int <- sort(decision_int)
decision <- paste(decision_int, collapse = ',')
}

}

# ask user for note on categorisation
# ask user for note on categorisation:

cat("\n \n")
decision_note <- readline("Categorisation note (or press enter to continue): ")

while (go_back != "Y" & go_back != "y" & go_back != "N" & go_back != "n") {
cat("\n \n")
go_back <- readline(prompt = "Re-do last categorisation? (y/n): ")
go_back <- readline(prompt = paste0("Respone to be saved is '",decision,"'. Would you like to re-do? (y/n): "))
}

first_run = FALSE
Expand Down
4 changes: 3 additions & 1 deletion man/user_categorisation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0dc8c36

Please sign in to comment.