Check if inputs contain forbidden values and error if so.
Usage
check_no_na(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_finite(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_unique(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_nzchar(
x,
...,
allow_all_ws = 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.- allow_all_ws
Whether
xis allowed to contain elements that are all whitespace.
Details
NA checks are done with anyNA();
finite checks are done with any() and is.finite();
unique checks are done with anyDuplicated();
zero chr checks are done with any() and nzchar();
If allow_all_ws = FALSE then whitespace elements are identified using
grepl("\\s+", x).
Input types are not checked, they are passed 'as is' to the functions
that do the forbidden value checking. The only exception is for NULL
inputs, which error if allow_null = FALSE.
Examples
x <- c(1, 2, NA)
check_no_na(x) |> try()
#> Error in eval(expr, envir) :
#> `x` must not contain NA values.
x <- c(1, 2, Inf)
check_finite(x) |> try()
#> Error in eval(expr, envir) :
#> `x` must not contain non-finite values.
x <- c(1, 2, 3, 1)
check_unique(x) |> try()
#> Error in eval(expr, envir) :
#> `x` must have unique elements. Duplicates: 1.
x <- c("a", "b", "")
check_nzchar(x) |> try()
#> Error in eval(expr, envir) : `x` must not contain empty strings.
x <- c("a", "b", " ")
check_nzchar(x, allow_all_ws = FALSE) |> try()
#> Error in eval(expr, envir) :
#> `x` must not contain all whitespace elements.
