check_choice_character: Check if an object is an element of a given set

View source: R/check_choice_character.R

check_choice_characterR Documentation

Check if an object is an element of a given set

Description

Check if an object is an element of a given set in the object name. The function is based on checkmate::check_choice, but includes the argument ignore.case.

Usage

check_choice_character(
  x,
  choices,
  null.ok = FALSE,
  ignore.case = FALSE,
  fmatch = FALSE
)

assert_choice_character(
  x,
  choices,
  null.ok = FALSE,
  ignore.case = FALSE,
  fmatch = FALSE,
  .var.name = checkmate::vname(x),
  comment = NULL,
  add = NULL
)

Arguments

x

[any]
Object to check.

choices

[character]
Set of possible values.

null.ok

[logical(1)]
If set to TRUE, x may also be NULL. In this case only a type check of x is performed, all additional checks are disabled.

ignore.case

[logical(1)]
Case is ignored if TRUE. Default is FALSE.

fmatch

[logical(1)]
Use the set operations implemented in fmatch in package fastmatch. If fastmatch is not installed, this silently falls back to match. fastmatch::fmatch modifies y by reference: A hash table is added as attribute which is used in subsequent calls.

.var.name

[character(1)]
Name of the checked object to print in error messages. Defaults to the heuristic implemented in vname.

comment

[character(1)]
Extra information to be appended to the standard error message in assertions.

add

[AssertCollection]
Collection to store assertions. See AssertCollection.

Details

The object must be of type character. The check is intended for functions were using camelCase may make the argument easier to remember. Therefore, the function will ignore case if ignore.case = TRUE.

Value

Depending on the function prefix:
If the check is successful, the function assert_choice_character return x invisibly, whereas check_choice_character return TRUE.
If the check is not successful, assert_choice_character throws an error message and check_choice_character returns a string with the error message.

Author(s)

Petter Hopp Petter.Hopp@vetinst.no

Examples

# returns TRUE
check_choice_character(x = "APPLE",
                     choices = c("Apple", "Pear", "Orange"),
                     null.ok = FALSE,
                     ignore.case = TRUE,
                     fmatch = FALSE)

NorwegianVeterinaryInstitute/NVIcheckmate documentation built on Dec. 14, 2024, 10:43 p.m.