This function allows you to interact with a user by posing questions that
accept text input, allowing for you to specify code that will be executed if
their response satisfies a condition you define via correct_logic
argument.
This could either be inline of code or a previously defined function that
returns TRUE
or FALSE
. See examples for a demonstration of both.
cl_text_action( prompt, correct_logic, correct_action, correct_message, incorrect_action = invisible(), incorrect_message, .envir = parent.frame(1) )
prompt | A character string with the yes/no question to be asked. Passed
into |
---|---|
correct_logic | Code that evaluates the user's text input and defines a
correct input by returning a |
correct_action | Code to execute upon a "yes" answer. |
correct_message | (Optional) message to display upon a "yes" answer.
Passed into |
incorrect_action | Code to execute upon a "no" answer. Default is
|
incorrect_message | (Optional) message to display upon a "yes" answer.
Passed into |
.envir | Used to ensure that |
NA; used to execute specified code depending on a user's response.
To simply save the value of a user's input in response to a prompt, please
see cl_text_input
.
It's inspired by usethis::ui_yeah()
and shiny::textInput()
. It wraps
base::readline()
with cli::cli_text()
allowing you to format questions
using the cli
package.
Other command-line-tools:
cl_text_input()
,
cl_yes_no_action()
,
cl_yes_no_lgl()
if (interactive()) { ## Define correct_logic inline # Ask who are the best pets. If they say Tucker or Ella in their response, print # "Correct answer!. cl_text_action("Who are the best pets?", correct_logic = { if (grepl("tucker|ella", tolower(answer))) { TRUE } else { FALSE } }, # Nothing to do, just want to print a message! correct_action = invisible(), correct_message = "\U1F63B Correct answer! \U1F436", incorrect_message = "\U1F44E \U1F63E Come meet the best dog, Ella, or the best cat, Tucker, and I think you'll change your mind!") ## Use a function to define correct logic has_tucker_ella <- function(x) { if (grepl("tucker|ella", tolower(x))) { TRUE } else { FALSE } } # Ask who are the best pets. If they say Tucker or Ella in their response, # print "Correct answer!. cl_text_action("Who are the best pets?", # Pass in the user's input with the variable `answer`. correct_logic = has_tucker_ella(answer), # Nothing to do, just want to print a message! correct_action = invisible(), correct_message = "\U1F63B Correct answer! \U1F436", incorrect_message = "\U1F44E \U1F63E Come meet the best dog, Ella, or the best cat, Tucker, and I think you'll change your mind!") }