To ask questions programmatically in the R language, use the askYesNo() function.
askYesNo in R
The askYesNo() is a built-in R function that provides a standard way to ask the user a yes/no question. The askYesNo() method provides a way for front-ends to substitute their own dialogs.
The askYesNo() function accepts the case-independent partial matches to the prompts. If no response is given, the value of default will be returned.
askYesNo(msg, default = TRUE, prompts = getOption("askYesNo", gettext(c("Yes", "No", "Cancel"))), ...)
msg: It is the prompt message for the user.
default: It is the default response.
prompts: They are any character vector containing 3 prompts corresponding to return values of TRUE, FALSE, or NA, or a single character value containing the prompts separated by / characters or a function to call.
…: They are additional parameters ignored by the default function.
Run the following code specifically in RStudio and inside R interactive console.
askYesNo("Are you using askYesNo?")
> askYesNo("Do you want to use askYesNo?") Do you want to use askYesNo? (Yes/no/cancel) y  TRUE > askYesNo("Do you want to use askYesNo?") Do you want to use askYesNo? (Yes/no/cancel) n  FALSE > askYesNo("Do you want to use askYesNo?") Do you want to use askYesNo? (Yes/no/cancel) c  NA
You can see that if we answer Yes or y, it returns TRUE
If we answer No or n, it returns FALSE.
If we reply Cancel or c, it returns NA.
If strings (or a string such as “Y/N/C“) are given as prompts, the choices will be mapped to lowercase for the non-default choices and left as-is for the default choice.
For an interactive environment, use the following code.
if (interactive()) askYesNo("Are you using askYesNo?")
If you are no in the interactive console, then this won’t work.
That is it for the askYesNo() Function in R.