Skip to contents

Check if arg is a numeric vector of real numbers. Every value must be finite and non-missing and the length must be greater than zero.

Usage

check_real(arg, signal = "error", msg = NULL, call. = FALSE)

Arguments

arg

(object)
The argument to check.

signal

(string: c("error", "warning", "message"))
Must be one of "error", "warning", or "message". "error" (default) calls base::stop() to signal an error message. "warning" calls base::warning() to signal a warning message. "message" calls base::message() to signal a message.

msg

(string or NULL: NULL)
A string that replaces the default message. Set as NULL to use the default message.

call.

(Scalar logical: c(FALSE, TRUE))
Passed to stop() or warning(). The default msg includes it's own formatted call, hence call. should be FALSE (default). If you insert your own msg, then you may want call. = TRUE.

Value

invisible(arg) or condition determined by argument signal.

Details

This is a shortcut for the chain check_numeric(), check_valid(), and check_finite(). Use check_valid_numeric() to allow infinite values.

Examples

#----------------------------------------------------------------------------
# check_real() examples
#----------------------------------------------------------------------------
library(bkcheck)

f <- function(x) {
  x |>
    check_real()
}

integer <- 1:4
numeric <- c(1, 1.5)
character <- letters[1:4]

f(integer)
f(numeric)

try(f(character))
#> Error : Argument `x` must be a finite, non-missing numeric vector with length
#> greater than zero.
#> 
#> class(x) = 'character'
#> length(x) = 4
#> 
#> Call: f(x = character)
try(f(numeric(0)))
#> Error : Argument `x` must be a finite, non-missing numeric vector with length
#> greater than zero.
#> 
#> class(x) = 'numeric'
#> length(x) = 0
#> 
#> Call: f(x = numeric(0))
try(f(c(1, NA)))
#> Error : Argument `x` must be a finite, non-missing numeric vector with length
#> greater than zero.
#> 
#> Missing at: 2
#> class(x) = 'numeric'
#> length(x) = 2
#> 
#> Call: f(x = c(1, NA))
try(f(c(1, Inf)))
#> Error : Argument `x` must be a finite, non-missing numeric vector with length
#> greater than zero.
#> 
#> Infinite at: 2
#> class(x) = 'numeric'
#> length(x) = 2
#> 
#> Call: f(x = c(1, Inf))