check_numeric: Check numeric

View source: R/check.R

check_numericR Documentation

Check numeric

Description

Checks that x is numeric. Uses is.numeric(), which accepts both "double" and "integer" inputs - the right semantics for "is this a number?". Prefer this over check_inherits(x, "numeric"), which rejects integers because their literal class is "integer", not "numeric" (a long-standing R/S3 quirk). This trips up wire-format callers: jsonlite::fromJSON("1") returns an integer, and the value would then fail inherits(., "numeric") despite being a perfectly good number.

Usage

check_numeric(x, allow_null = TRUE, arg_name = deparse(substitute(x)))

Arguments

x

Vector to check.

allow_null

Logical: If TRUE, NULL values are allowed and return early.

arg_name

Character: Name of the variable for error messages.

Value

Called for side effects. Throws an error if checks fail.

Author(s)

EDG

Examples

check_numeric(1L)
check_numeric(1.5)
check_numeric(c(1, 2, 3))
# Throws error:
try(check_numeric("1"))
try(check_numeric(TRUE))

rtemis.core documentation built on Sept. 20, 2026, 9:06 a.m.