
Vectors of storage type integer and double count as numeric, c.f.
is.numeric(). To explicitly assert for integer or double vectors, see
is_integer(), is_integerish() or is_double().
is_numeric(
x,
lower = -Inf,
upper = Inf,
finite = FALSE,
any.missing = TRUE,
all.missing = TRUE,
len = NULL,
min.len = NULL,
max.len = NULL,
unique = FALSE,
sorted = FALSE,
names = NULL,
typed.missing = FALSE,
null.ok = FALSE
)
stop_is_numeric(x, ..., par. = list())The R object that was tested, typically with is_numeric().
Lower value (number) all elements of x must be greater than or
equal to.
Upper value (number) all elements of x must be lower than or
equal to.
Logical, indicating whether all elements of x must be finite,
default is FALSE.
Logical, indicating whether x may contain missing
values, default is TRUE.
Logical, indicating whether x may be entirely missing
values, default is TRUE. An empty vector has no missing values.
Expected length of x (integer).
Minimal length of x (integer).
Maximal length of x (integer).
Logical, indicating whether all values of x must be unique,
default is FALSE.
Logical, indicating whether all values of x must be sorted in
ascending order, default is FALSE.
Check for names. Default in NULL (no check). Could be
"unnamed" (has no names), "named" (has names), "unique" (has unique
names), "strict" (same as unique, but names must be also valid R variable
names), or "ids" (same as strict but not enforce uniqueness).
If FALSE (default), all types of missing values (NA,
NA_integer_, NA_real_, or NA_character_) and empty vectors are allowed
while type-checking atomic input. If TRUE, leads to strict type checking.
If set to TRUE, x may also be NULL. In this case only a
type check of x is performed, all additional checks are disabled. Default
is FALSE.
Any additional arguments (not checked, and not used).
An optional list with further parameters, like msg a custom
error message, arg the argument name, as a string, default is the
expression provided to x, mod a modifier string (only "!" is considered
here, indicating negation of the condition), class_id an identifier to
append to the error subclass, call the call where the error was generated
(the default computes the top-level call of the function(s) that called
error_numeric() using stop_top_call().
Logical for is_numeric(), TRUE if x passes all checks, FALSE
otherwise, and the internal message option is set with the indication of
what failed, can be reused by error_numeric() that always create an error
message.
is_numeric(1.2) # Better using is_num() in this simple case
#> [1] TRUE
is_numeric("a")
#> [1] FALSE
svAssert:::.checkmate_message() # Get the message set by is_numeric()
#> [1] "Must be of type 'numeric', not 'character'"
my_log <- function(y) {# x must be numeric >= 0
is_numeric(y, min.len = 1, lower = 0) || stop_is_numeric(y)
log(y)
}
my_log(1)
#> [1] 0
# |> try() to catch the error, do not use in real code!
my_log(-1:5) |> try()
#> Error in my_log(-1:5) :
#> Can't use argument `y` (a vector of type <integer> and 7 elements).
#> ℹ Element 1 is not >= 0