Skip to contents

This is a closure intended for running a checkmate check and always returning TRUE or FALSE so that it can be used in conditional statements. If the check fails, it will optionally emit the error message returned by checkmate.

Usage

checkmate_chk(fun)

Arguments

fun

A checkmate check, for example checkmate::check_integer.

Value

A function which runs the specified checkmate check functions. See 'Returned Function' for details.

Returned Function

The returned function runs a checkmate check.

Arguments

...

Arguments passed on to the specified checkmate function.

quiet

A logical which is TRUE if a failed check should not emit its error message; FALSE if it should.
Default: TRUE

Value
A logical which is TRUE if the check succeeded and FALSE if not. The message generated by a failed check is set as an attribute of the return value.

See also

Examples

# Note: chk_choice is created as follows:
# chk_choice <- checkmate_chk(checkmate::check_choice)
valid_choices <- c("pineapple", "blueberry", "peach")

# The following check succeeds:
chk_choice("peach", valid_choices)

# The following check fails and *does not* emit the error message:
chk_choice("avocado", valid_choices, quiet = TRUE)

# The following check fails and *does* emit the error message:
chk_choice("avocado", valid_choices)

# Use this check in a conditional statement:
if (!chk_choice("pineapple", valid_choices)) {
  # Do something as a result of the failure ...
  warning("Value 'pineapple' is not a valid option.")
}

if (FALSE) { # \dontrun{
# An example of using this check with `cli::cli_abort`:
res <- chk_choice("tractor", valid_choices)
if (!res) {
  cli::cli_abort("{cli::cli_verbatim(attr(res, 'err_msg'))}")
}
} # }