Check if inputs are scalars of an expected type and throw an error if not.
Usage
check_scalar_list(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_atomic(
x,
...,
allow_na = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_vector(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_integer(
x,
...,
allow_na = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_integerish(
x,
...,
finite = FALSE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_double(
x,
...,
finite = FALSE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_complex(
x,
...,
finite = FALSE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_character(
x,
...,
allow_na = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_logical(
x,
...,
allow_na = TRUE,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_raw(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_bytes(
x,
...,
allow_null = FALSE,
arg = caller_arg(x),
call = caller_env()
)
check_scalar_numeric(
x,
...,
finite = FALSE,
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_na
Whether
xis allowed to containNAvalues.- finite
Whether
xis required to contain only finite values (i.e. noNA,Inf,-Inf, orNaN).
Details
These functions can be used with the bare() modifier to check if an
object is a bare R object (i.e. has no class attribute).
Note
To handle empty strings ("") use check_string() instead of
check_scalar_character().
These check functions are wrappers of their corresponding
rlang functions. The exception
is check_scalar_numeric(), which uses is.numeric().
Examples
x <- 1L
check_scalar_integer(x)
check_scalar_double(x) |> try()
#> Error in eval(expr, envir) :
#> `x` must be a scalar <double>, not the number 1.
check_scalar_list(list(list()))
check_scalar_list(list(1, 2)) |> try()
#> Error in eval(expr, envir) :
#> `list(1, 2)` must be a scalar <list>, but it is of length 2.
check_scalar_character(NA_character_, allow_na = FALSE) |> try()
#> Error in eval(expr, envir) :
#> `NA_character_` must not be NA.
check_scalar_double(Inf, finite = TRUE) |> try()
#> Error in eval(expr, envir) :
#> `Inf` must be a finite value, not Inf.
check_scalar_logical(NULL, allow_null = TRUE)
x <- 1.0
check_scalar_integerish(x)
