From d6675940a184346bd23af2008f0acb022a36ecfb Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Wed, 20 May 2020 11:21:35 +0200 Subject: [PATCH] Added questdlg --- R/questdlg.R | 34 ++++++++++++++++++++++++++++++++++ man/questdlg.Rd | 21 +++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 R/questdlg.R create mode 100644 man/questdlg.Rd diff --git a/R/questdlg.R b/R/questdlg.R new file mode 100644 index 0000000..07a0104 --- /dev/null +++ b/R/questdlg.R @@ -0,0 +1,34 @@ +#' @title Prompt for multiple-choice +#' @param quest Question +#' @param dlgtitle Title of question +#' @param btn Vector of alternatives +#' @param defbtn Scalar with the name of the default option +#' @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') { + message(dlgtitle) + # ========================================================================== + # Replacing the default option with a capitalized version on btn + # ========================================================================== + btn[match(tolower(defbtn), tolower(btn))] <- toupper(defbtn) + # ========================================================================== + # Creating prompt + # ========================================================================== + option_char <- paste0(' [', paste(btn, collapse = ', '), ']') + answer <- readline(paste0(quest, option_char, ": ")) + # ========================================================================== + # Processing answer + # ========================================================================== + answer <- tolower(answer) + if (!(answer %in% tolower(c(btn)))) { + if (answer != "") { + warning( + "'", answer, "' is not a valid altenative. Defaulting to ", + defbtn + ) + } + answer <- defbtn + } + return(answer) +} \ No newline at end of file diff --git a/man/questdlg.Rd b/man/questdlg.Rd new file mode 100644 index 0000000..fdd329c --- /dev/null +++ b/man/questdlg.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/questdlg.R +\name{questdlg} +\alias{questdlg} +\title{Prompt for multiple-choice} +\usage{ +questdlg(quest, dlgtitle, btn = c("y", "n"), defbtn = "n") +} +\arguments{ +\item{quest}{Question} + +\item{dlgtitle}{Title of question} + +\item{btn}{Vector of alternatives} + +\item{defbtn}{Scalar with the name of the default option} +} +\description{ +This function aims to loosely mimic the behavior of the +questdlg function on Matlab +}