2020-05-20 11:21:35 +02:00
|
|
|
#' @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
|
2020-05-20 11:34:26 +02:00
|
|
|
#' @param accepted_ans Vector containing accepted answers
|
2020-05-20 11:21:35 +02:00
|
|
|
#' @description This function aims to loosely mimic the behavior of the
|
|
|
|
|
#' questdlg function on Matlab
|
|
|
|
|
#' @export
|
2020-05-20 11:34:26 +02:00
|
|
|
questdlg <- function(
|
|
|
|
|
quest,
|
2020-05-20 13:08:57 +02:00
|
|
|
dlgtitle = "",
|
2020-05-20 11:34:26 +02:00
|
|
|
btn = c('y', 'n'),
|
|
|
|
|
defbtn = 'n',
|
|
|
|
|
accepted_ans = c('y', 'yes', 'n', 'no')
|
|
|
|
|
) {
|
2020-05-20 11:21:35 +02:00
|
|
|
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)
|
2020-05-20 11:34:26 +02:00
|
|
|
if (!(answer %in% tolower(c(btn, accepted_ans)))) {
|
2020-05-20 11:21:35 +02:00
|
|
|
if (answer != "") {
|
|
|
|
|
warning(
|
2020-05-20 11:34:26 +02:00
|
|
|
"'", answer, "' is not a valid alternative. Defaulting to ",
|
2020-05-20 11:21:35 +02:00
|
|
|
defbtn
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
answer <- defbtn
|
|
|
|
|
}
|
|
|
|
|
return(answer)
|
|
|
|
|
}
|