From 0dc8c36eb39748bfe943dc3a8320b1a36c3b7b45 Mon Sep 17 00:00:00 2001 From: Rachael Stickland <50215726+RayStick@users.noreply.github.com> Date: Thu, 4 Jul 2024 14:19:56 +0100 Subject: [PATCH] validate user input --- R/domain_mapping.R | 6 +++--- R/user_categorisation.R | 30 +++++++++++++++++++++++++----- man/user_categorisation.Rd | 4 +++- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/R/domain_mapping.R b/R/domain_mapping.R index ebe2dd9a..9cb50155 100755 --- a/R/domain_mapping.R +++ b/R/domain_mapping.R @@ -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 @@ -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 @@ -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 diff --git a/R/user_categorisation.R b/R/user_categorisation.R index d275067b..567846f9 100644 --- a/R/user_categorisation.R +++ b/R/user_categorisation.R @@ -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 = '' @@ -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 diff --git a/man/user_categorisation.Rd b/man/user_categorisation.Rd index d7e64427..13b7b46d 100644 --- a/man/user_categorisation.Rd +++ b/man/user_categorisation.Rd @@ -4,7 +4,7 @@ \alias{user_categorisation} \title{user_categorisation} \usage{ -user_categorisation(data_element, data_desc, data_type) +user_categorisation(data_element, data_desc, data_type, domain_code_max) } \arguments{ \item{data_element}{Name of the variable} @@ -12,6 +12,8 @@ user_categorisation(data_element, data_desc, data_type) \item{data_desc}{Description of the variable} \item{data_type}{Data type of the variable} + +\item{domain_code_max}{Max code in the domain list (0-3 auto included, then N included via domain_file)} } \value{ It returns a list containing the decision and decision note