check_subset_character: Check if an argument is a subset of a given set

View source: R/check_subset_character.R

check_subset_characterR Documentation

Check if an argument is a subset of a given set

Description

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

Usage

check_subset_character(
  x,
  choices,
  ignore.case = FALSE,
  empty.ok = TRUE,
  fmatch = FALSE
)

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

Arguments

x

[any]
Object to check.

choices

[character]
Set of possible values. May be empty.

ignore.case

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

empty.ok

[logical(1)]
Treat zero-length x as subset of any set choices (this includes NULL)? Default is TRUE.

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_subset_character return x invisibly, whereas check_subset_character return TRUE.
If the check is not successful, assert_subset_character throws an error message and check_subset_character returns a string with the error message.

Author(s)

Petter Hopp Petter.Hopp@vetinst.no

Examples

# returns TRUE
check_subset_character(x = "Apple",
                       choices = c("apple", "pear", "orange", "banana"),
                       ignore.case = TRUE)
check_subset_character(x = c("Apple", "Pear"),
                       choices = c("apple", "pear", "orange", "banana"),
                       ignore.case = TRUE)

# returns a message
check_subset_character(x = "Tomato",
                       choices = c("apple", "pear", "orange", "banana"),
                       ignore.case = TRUE)

PetterHopp/NVIcheckmate documentation built on Dec. 15, 2024, 12:49 a.m.