loading...

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())

Arguments

x

The R object that was tested, typically with is_numeric().

lower

Lower value (number) all elements of x must be greater than or equal to.

upper

Upper value (number) all elements of x must be lower than or equal to.

finite

Logical, indicating whether all elements of x must be finite, default is FALSE.

any.missing

Logical, indicating whether x may contain missing values, default is TRUE.

all.missing

Logical, indicating whether x may be entirely missing values, default is TRUE. An empty vector has no missing values.

len

Expected length of x (integer).

min.len

Minimal length of x (integer).

max.len

Maximal length of x (integer).

unique

Logical, indicating whether all values of x must be unique, default is FALSE.

sorted

Logical, indicating whether all values of x must be sorted in ascending order, default is FALSE.

names

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).

typed.missing

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.

null.ok

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).

par.

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

Value

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.

Author

Derived from code by Michel Lang, Bernd Bischl, and Dénes Tóth (authors of the {checkmate} package whose code is repackaged here). Documentation is also largely inspired from {checkmate} corresponding documentation.

Examples

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