valid: Generic validation function

View source: R/valid.R

validR Documentation

Generic validation function

Description

Designed to be called inside a customvalid_* function (e.g. valid_yes_no())

Usage

valid(
  choice = character(),
  choices = character(),
  reverse = FALSE,
  flip = FALSE,
  strict = TRUE,
  unname = FALSE,
  call = sys.call(-1)[[1]]
)

Arguments

choice

(character) Available values

choices

(character) Selection of available values

reverse

(logical) Reverse order yes/no

flip

(logical) Flip names and values yes/no

strict

(logical) Be strict about "things that might go wrong" yes/no

unname

(logical) Drop names yes/no

call

(call) Enclosing call

Examples

valid_foo <- function(
  choice = character(),
  ...
) {
  choices <- letters[1:3]

  names(choices) <- choices %>% toupper()

  valid::valid(
    choice = choice,
    choices = choices,
    ...
  )
}
valid_foo()
# Via name
valid_foo("A")
valid_foo("A", "C")
# Via value
valid_foo("a")
valid_foo("a", "c")
# Handling obvious errors
try(valid_foo("D"))
try(valid_foo("d"))
# Handling partial errors
try(valid_foo("A", "D"))
try(valid_foo("A", "D", .strict = FALSE))
try(valid_foo("a", "d"))
try(valid_foo("a", "d", .strict = FALSE))

rappster/valid documentation built on Aug. 19, 2022, 1:59 p.m.