This function allows you to interact with a user by posing yes/no questions.
It's inspired by usethis::ui_yeah()
, allowing for you to specify code that
will be executed upon a user responding "yes" or "no."
cl_yes_no_action( prompt, yes_action, yes_message, no_action = invisible(), no_message, yes_opts = c("Yes", "Duh!", "Absolutely", "Please", "Obvi, yeah."), no_opts = c("No", "Nah", "Not now", "Not today", "No, thanks."), n_yes = 1, n_no = 2, shuffle = TRUE, .envir = parent.frame(1) )
prompt | A character string with the yes/no question to be asked. Passed
into |
---|---|
yes_action | Code to execute upon a "yes" answer. |
yes_message | (Optional) message to display upon a "yes" answer. Passed
into |
no_action | Code to execute upon a "no" answer. Default is
|
no_message | (Optional) message to display upon a "yes" answer. Passed
into |
yes_opts | A character vector of "yes" strings, randomly sampled to display to the user. |
no_opts | A character vector of "no" strings, randomly sampled to display to the user. |
n_yes | An integer defining how many "yes" strings to include when asking the user a question. Default is 1. |
n_no | An integer defining how many "no" strings to include when asking the user a question. Default is 2. |
shuffle | Logical: TRUE by default. Should the order of the yes/no options be shuffled before being presented to the user? |
.envir | Used to ensure that |
NA; used to execute specified code depending on a user's response.
To ask yes/no questions and return a logical value, please see
cl_yes_no_lgl
.
Other command-line-tools:
cl_text_action()
,
cl_text_input()
,
cl_yes_no_lgl()
if (interactive()) { # define a function to create a directory with approval. yn_create_dir <- function(path) { path <- here::here(path) cl_yes_no_action(prompt = "The directory {.file {path}} does not exist. Would you like to create it?", yes_action = fs::dir_create(path), yes_message = "Okay, creating {.file {path}}.", no_message ="Okay, not creating {.file {path}}.") } # Test the function yn_create_dir("new-folder") # Ask a simple yes/no question that prints a conditional response cl_yes_no_action(prompt = "Do you love sushi?", yes_action = print("I'm not surprised, it's great!"), no_action = print("Hmmm...have you tried it?")) # Simple yes/no with content-related yes/no options cl_yes_no_action(prompt = "Is your favorite cat Tucker?", yes_action = print("Correct answer!"), no_action = print("Wrong answer..."), yes_opts = "Duh, he's the cutest", no_opts = c("I'm a dog person", "I'm allergic to cats")) # Add some color (indicating the correct answer in this case) cl_yes_no_action(prompt = "Is your favorite cat Tucker?", yes_action = print("Correct answer!"), no_action = print("Wrong answer..."), yes_opts = crayon::green("Duh, he's the cutest"), no_opts = c("I'm a dog person", "I'm allergic to cats")) # Conduct multiple actions like a normal R script upon a "yes" (no would work similarly) path <- here::here("another-test-folder") cl_yes_no_action(prompt = "The directory {.file {path}} does not exist. Would you like to create it?", yes_action = { # create path fs::dir_create(path) # indicate path was created cli::cli_alert_success("Created directory at {.file {path}}") # assign saved_path as path saved_path <- path # print a random letter just because we can! print(sample(letters,1)) }, no_message ="The directory {.file {path}} was not created." ) }