cl_text_action: Ask Text Questions via the Console

Description Usage Arguments Details Value See Also Examples

View source: R/func_cli-actions.R

Description

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.

Usage

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)
)

Arguments

prompt

A character string with the yes/no question to be asked. Passed into cli::cli_text() and can use its theming syntax.

correct_logic

Code that evaluates the user's text input and defines a correct input by returning a TRUE and an incorrect input by returning FALSE. The user's input is stored in the variable answer.

correct_action

Code to execute upon a "yes" answer.

correct_message

(Optional) message to display upon a "yes" answer. Passed into cli::cli_alert_success() and can use its theming syntax.

incorrect_action

Code to execute upon a "no" answer. Default is invisible(), i.e. do nothing.

incorrect_message

(Optional) message to display upon a "yes" answer. Passed into cli::cli_alert_danger() and can use its theming syntax.

.envir

Used to ensure that cli::cli_text() evaluates the prompt, yes_message, and no_message in the appropriate environment. Expert use only.

Details

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.

Value

NA; used to execute specified code depending on a user's response.

See Also

Other command-line-tools: cl_text_input(), cl_yes_no_action(), cl_yes_no_lgl()

Examples

 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!")
}

jdtrat/jdtools documentation built on Dec. 20, 2021, 10:05 p.m.