Skip to contents

Check if arg is a scalar whole number (to tolerance tol). Must be a numeric vector, length == 1, and not missing.

Usage

check_scalar_whole_number(
  arg,
  tol = 1e-08,
  signal = "error",
  msg = NULL,
  call. = FALSE
)

Arguments

arg

(object)
The argument to check.

tol

(Scalar non-negative numeric: 1e-08)
Relative differences smaller than tol are ignored, thus assumed to be a whole number.

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_scalar_whole_number() examples
#----------------------------------------------------------------------------
library(bkcheck)

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

logical <- TRUE
integer <- 1L
numeric <- 1.000000001
character <- letters[1]
factor <- factor(character)

f(integer)
f(numeric)

try(f(logical))
#> Error : Argument `x` must be a scalar whole number vector.
#> 
#> `x` = TRUE
#> class(x) = 'logical'
#> length(x) = 1
#> 
#> Call: f(x = logical)
try(f(character))
#> Error : Argument `x` must be a scalar whole number vector.
#> 
#> `x` = 'a'
#> class(x) = 'character'
#> length(x) = 1
#> 
#> Call: f(x = character)
try(f(factor))
#> Error : Argument `x` must be a scalar whole number vector.
#> 
#> `x` = 'a'
#> class(x) = 'factor'
#> length(x) = 1
#> 
#> Call: f(x = factor)