Added questdlg

This commit is contained in:
Waldir Leoncio 2020-05-20 11:21:35 +02:00
parent 3105ca9995
commit d6675940a1
2 changed files with 55 additions and 0 deletions

34
R/questdlg.R Normal file
View file

@ -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)
}

21
man/questdlg.Rd Normal file
View file

@ -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
}