cl_text_input: Ask Text Questions via the Console

Description Usage Arguments Details Value See Also Examples

View source: R/func_cli.R

Description

This function allows you to interact with a user by posing questions that accept 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.

Usage

1

Arguments

prompt

A character string with the question to be asked. Passed into cli::cli_text() 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

In general, I would recommend using cl_text_action as it accepts code to execute depending on the user's input, but this function is perhaps more adaptable.

Value

A character string containing the user's response.

See Also

Other command-line-tools: cl_text_action(), 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
favorite_cat_question <- function() {
  answer <- cl_text_input("Name your {.strong favorite} cat.")
  if (tolower(answer) != "tucker") {
    cli::cli_alert_danger("\U1F63E Come meet my cat, Tucker,
    and I think you'll change your mind!")
  } else {
    cli::cli_alert_success("Correct answer! \U1F63B")
  }
}

favorite_dog_question <- function() {
  answer <- cl_text_input("Name your {.strong favorite} dog.")
  if (tolower(answer) != "ella") {
    cli::cli_alert_danger("\U1F44E Come meet my dog, Ella,
    and I think you'll change your mind!")
  } else {
    cli::cli_alert_success("Correct answer!\U1F436")
  }
}

if (interactive()) favorite_cat_question()

if (interactive()) favorite_dog_question()

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