must {pkgutils}R Documentation

Convert warnings to errors

Description

Raise an error if a warning occurs. Useful for making certain tests more strict. It is a bit easier to use than changing the ‘warn’ entry of options from the base package (because the entry would usually need to be set back).

Usage

  must(expr, msg = NULL, ..., domain = NULL)

Arguments

expr

R expression to evaluate.

msg

Character vector to be used as error message. If empty or NULL, the conditionMessage of the issued warning is used.

...

Optional further arguments to tryCatch.

domain

Passed to stop (if a warning occurs).

Value

The result of expr (if no error occurs).

See Also

base::tryCatch base::stop base::options

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

Examples

(x <- try(must(as.numeric(c("1", "2", "3"))), silent = TRUE))
## [1] 1 2 3
stopifnot(identical(x, c(1, 2, 3)))
(x <- try(must(as.numeric(c("1", "x", "3"))), silent = TRUE))
## [1] "Error : NAs introduced by coercion\n"
## attr(,"class")
## [1] "try-error"
## attr(,"condition")
## <simpleError: NAs introduced by coercion>
stopifnot(inherits(x, "try-error"))

[Package pkgutils version 0.7.63 Index]