Check if inputs are expected scalar values and throw an error if not.
Usage
check_true(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_false(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_bool(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_string(
x,
...,
string = NULL,
allow_empty = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)Arguments
- x
An object to check.
- ...
Additional arguments passed to
cli_abort()which forwards unmatched arguments toabort().- allow_null
Whether
xis allowed to beNULL.- arg
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem.
- call
The execution environment of a currently running function, e.g.
caller_env(). The function will be mentioned in error messages as the source of the error. See thecallargument ofabort()for more information.- string
A character vector of allowed values for
x. IfNULL, the value is not checked. The check passes ifxis any of the values instring.- allow_empty
Whether
xis allowed to be an empty string (i.e. whenFALSE,""is not allowed).
Examples
x <- TRUE
check_true(x)
check_false(x) |> try()
#> Error in eval(expr, envir) :
#> `x` must be a single FALSE, not TRUE.
check_bool(NA) |> try()
#> Error in eval(expr, envir) :
#> `NA` must be a single TRUE or FALSE, not NA.
check_bool(NULL, allow_null = TRUE)
x <- "a"
check_string(x)
check_string(x, string = c("a", "b"))
check_string(x, string = c("b", "c")) |> try()
#> Error in eval(expr, envir) :
#> `x` must be one of "b" or "c".
check_string("", allow_empty = FALSE) |> try()
#> Error in eval(expr, envir) : `""` must not be an empty string.
