Skip to contents

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 to abort().

allow_null

Whether x is allowed to be NULL.

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 the call argument of abort() for more information.

allow_all_ws

Whether x is allowed to contain elements that are all whitespace.

Value

NULL invisibly if the check passes, otherwise an error is thrown.

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.

Note

NA_character_ is not considered zero chr nor all whitespace.

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.