Skip to contents

Check if arg is an atomic vector.

Usage

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

Examples

#----------------------------------------------------------------------------
# check_atomic() examples
#----------------------------------------------------------------------------
library(bkcheck)

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

f(TRUE)
f(1:10)
f(letters)
f(factor(letters))
f(logical(0))
f(NA)
f(NaN)

try(f(NULL))
#> Error : Argument `x` must be an atomic vector.
#> 
#> class(x) = 'NULL'
#> length(x) = 0
#> 
#> Call: f(x = NULL)
try(f(matrix(1)))
#> Error : Argument `x` must be an atomic vector.
#> 
#> class(x) = 'matrix', 'array'
#> typeof(x) = 'double'
#> nrow(x) = 1
#> ncol(x) = 1
#> 
#> Call: f(x = matrix(1))
try(f(data.frame(1)))
#> Error : Argument `x` must be an atomic vector.
#> 
#> class(x) = 'data.frame'
#> nrow(x) = 1
#> ncol(x) = 1
#> 
#> Call: f(x = data.frame(1))
try(f(list(1)))
#> Error : Argument `x` must be an atomic vector.
#> 
#> class(x) = 'list'
#> length(x) = 1
#> 
#> Call: f(x = list(1))