ask: Ask questions to the user, at the command line, and get the...

Description Usage Arguments Details Value Question types ‘input’ type ‘confirm’ type ‘choose’ type ‘checkbox’ type ‘constant’ type Conditional execution Examples

View source: R/ask.R

Description

Ask questions to the user, at the command line, and get the answers

Usage

1
ask(..., .prompt = yellow(paste0(symbol$pointer, " ")))

Arguments

...

Questions to ask, see details below.

.prompt

Prompt to prepend to all questions.

Details

Ask a series of questions to the user, and return all results together in a list.

The ask function takes named arguments only:

Value

A named list with the answers.

Question types

input

One line of text input.

confirm

A yes-no question, ‘y’ and ‘yes’ are considered as positive, ‘n’ and ‘no’ as negative answers (case insensitively).

choose

Choose one item form multiple items.

checkbox

Select multiple values from a list.

constant

Not a question, it defines constants.

‘input’ type

1
2
  input(message, default = "", filter = NULL, validate = NULL,
        when = NULL)
message

The message to print.

default

The default vaue to return if the user just presses enter.

filter

If not NULL, then it must be a function, that is called to filter the entered result.

validate

If not NULL, then it must be a function that is called to validate the input. The function must return TRUE for valid inputs and an error message (character scalar) for invalid ones.

when

See ‘Conditional execution’ below.

‘confirm’ type

1
  confirm(message, default = TRUE, when = NULL)
message

The message to print.

default

The default answer if the user just presses enter.

when

See ‘Conditional execution’ below.

‘choose’ type

1
  choose(message, choices, default = NA, when = NULL)
message

Message to print.

choices

Possible choices, character vector.

default

Index or value of the default choice (if the user hits enter, or NA for no default. Values are matched using partial matches via pmatch.

when

See ‘Conditional execution’ below.

‘checkbox’ type

1
  checkbox(message, choices, default = numeric(), when = NULL)
message

Message to print.

choices

Possible choices, character vector.

default

Indices or values of default choices. values are matches using partial matches via pmatch.

when

See ‘Conditional execution’ below.

‘constant’ type

1
  constant(value = constant_value, when = NULL)
constant_value

The constant value. Note that the argument must be named.

when

See ‘Conditional execution’ below.

Conditional execution

The when argument to a question can be used for conditional execution of questions. If it is given (and not NULL), then it must be a function. It is called with the answers list up to that point, and it should return TRUE or FALSE. For TRUE, the question is shown to the user and the result is inserted into the answer list. For FALSE, the question is not shown, and the answer list is not chagned.

Examples

1
2
3
4
5
6
7
8
9
## Not run: 
ask(
  name =  input("What is your name?"),
  cool = confirm("Are you cool?"),
  drink = choose("Select your poison!", c("Beer", "Wine")),
  language = checkbox("Favorite languages?", c("C", "C++", "Python", "R"))
)

## End(Not run)

gaborcsardi/ask documentation built on Dec. 27, 2021, 9:56 a.m.