Description Usage Arguments Details Value See Also Examples
View source: R/func_cli-actions.R
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.
1 2 3 4 5 6 7 8 9 | 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 |
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.
NA; used to execute specified code depending on a user's response.
Other command-line-tools:
cl_text_input()
,
cl_yes_no_action()
,
cl_yes_no_lgl()
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 35 36 37 38 39 40 41 42 43 44 | 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!")
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.