assert: Make assertions

Description Usage Arguments Details Value See Also Examples

View source: R/coding.R

Description

A function similar to stopifnot reporting more details on vector elements.

Usage

1
  assert(cond, orig, msg, quiet = FALSE, ...)

Arguments

cond

A logical vector, character scalar or function. If a character scalar, converted to a function with match.fun. If a function, orig is passed to it as its first argument. The function should return a logical vector of the length of orig. Contained NA values, if any, are replaced by FALSE.

orig

Mandatory if cond is (the name of) a function, otherwise ignored when empty or missing. Otherwise a vector of the length of cond if cond is a logical vector, or the single argument of cond if it is (the name of) a function.

msg

When empty or missing, an error message is constructed automatically in the case of failure. Otherwise either a sprintf template to be applied to the failing elements of orig, or a character scalar directly providing the error message.

quiet

Logical scalar determining the type of output in case of success (i.e., all values of cond are TRUE) or failure.

FALSE

TRUE is returned when successful, an error is raised otherwise.

NA

cond is returned or the resulting logical vector when cond is (the name of) a function. In case of failure, a warning is issued. This can be used to drop parts of objects such as data frames, with a warning.

TRUE

A character vector with the descriptions of the problems is returned. This vector is empty in case of success.

...

Optional arguments passed to cond when it is (the name of) a function.

Details

Compared to stopifnot this function can only conduct a test on a single object but can report element-specific details of failures.

Value

The type of return value depends on the values of quiet and cond.

See Also

base::stopifnot base::match.fun base::sprintf

Other coding-functions: L, LL, case, check, collect, contains, flatten, listing, map_names, map_values, must, set, sql, unnest

Examples

1
2
3
stopifnot(assert(function(x) x > 0, 1:10))
(x <- try(assert(function(x) x > 0, -1:8), TRUE))
stopifnot(inherits(x, "try-error"))

pkgutils documentation built on May 2, 2019, 5:49 p.m.

Related to assert in pkgutils...