File: askUserYesNo.R

package info (click to toggle)
r-bioc-biocbaseutils 1.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: makefile: 2
file content (34 lines) | stat: -rw-r--r-- 1,023 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#' Ask user for a yes/no response
#'
#' @param prompt `character()` Question form prompt to display to the user
#'   without a question mark
#'
#' @param interactive.only `logical(1)` If `TRUE`, the function will only
#'  prompt the user when the R session is interactive. If `FALSE`, the
#'  function will always prompt the user.
#'
#' @return TRUE when user replies with 'yes' to prompt, FALSE when 'no'
#'
#' @author Martin M.
#'
#' @examples
#'
#' askUserYesNo("Do you want to continue")
#'
#' @export
askUserYesNo <-
    function(prompt, interactive.only = TRUE)
{
    if (interactive.only && !interactive())
        return(FALSE)
    responses <- c("yes", "no")
    msg1 <- paste0(prompt, " [", paste(responses, collapse = ", "), "]? ")
    msg2 <- paste0("reply with '", paste(responses, collapse = "' or '"), "'")
    repeat {
        userResponse <- trimws(tolower(readline(msg1)))
        if (userResponse %in% responses)
            break
        message(msg2)
    }
    identical(userResponse, responses[1L])
}