From ce7fdeb0030feb7b966f0e2135de3fa998b8e3e1 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Wed, 20 May 2020 11:34:26 +0200 Subject: [PATCH] Added accepted_ans arg to improve answer handling --- R/questdlg.R | 13 ++++++++++--- man/questdlg.Rd | 10 +++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/R/questdlg.R b/R/questdlg.R index 07a0104..9f7e1aa 100644 --- a/R/questdlg.R +++ b/R/questdlg.R @@ -3,10 +3,17 @@ #' @param dlgtitle Title of question #' @param btn Vector of alternatives #' @param defbtn Scalar with the name of the default option +#' @param accepted_ans Vector containing accepted answers #' @description This function aims to loosely mimic the behavior of the #' questdlg function on Matlab #' @export -questdlg <- function(quest, dlgtitle, btn = c('y', 'n'), defbtn = 'n') { +questdlg <- function( + quest, + dlgtitle, + btn = c('y', 'n'), + defbtn = 'n', + accepted_ans = c('y', 'yes', 'n', 'no') +) { message(dlgtitle) # ========================================================================== # Replacing the default option with a capitalized version on btn @@ -21,10 +28,10 @@ questdlg <- function(quest, dlgtitle, btn = c('y', 'n'), defbtn = 'n') { # Processing answer # ========================================================================== answer <- tolower(answer) - if (!(answer %in% tolower(c(btn)))) { + if (!(answer %in% tolower(c(btn, accepted_ans)))) { if (answer != "") { warning( - "'", answer, "' is not a valid altenative. Defaulting to ", + "'", answer, "' is not a valid alternative. Defaulting to ", defbtn ) } diff --git a/man/questdlg.Rd b/man/questdlg.Rd index fdd329c..be00b5a 100644 --- a/man/questdlg.Rd +++ b/man/questdlg.Rd @@ -4,7 +4,13 @@ \alias{questdlg} \title{Prompt for multiple-choice} \usage{ -questdlg(quest, dlgtitle, btn = c("y", "n"), defbtn = "n") +questdlg( + quest, + dlgtitle, + btn = c("y", "n"), + defbtn = "n", + accepted_ans = c("y", "yes", "n", "no") +) } \arguments{ \item{quest}{Question} @@ -14,6 +20,8 @@ questdlg(quest, dlgtitle, btn = c("y", "n"), defbtn = "n") \item{btn}{Vector of alternatives} \item{defbtn}{Scalar with the name of the default option} + +\item{accepted_ans}{Vector containing accepted answers} } \description{ This function aims to loosely mimic the behavior of the