Skip to contents

Check if arg is <, <=, >, or >= bound.

Usage

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

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

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

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

Arguments

arg

(Scalar numeric)
The argument to check.

bound

(Scalar numeric)
The bound for arg.

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

#----------------------------------------------------------------------------
# check_lt()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_numeric() |>
    check_lt(2)
}

f(0:1)
try(f(0:2))
#> Error : Argument `x` must be strictly less than 2.
#> 
#> max(x, na.rm = TRUE) = 2
#> 
#> Call: f(x = 0:2)

#----------------------------------------------------------------------------
# check_leq()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_numeric() |>
    check_leq(1)
}

f(0:1)
try(f(0:2))
#> Error : Argument `x` must be less than or equal to 1.
#> 
#> max(x, na.rm = TRUE) = 2
#> 
#> Call: f(x = 0:2)

#----------------------------------------------------------------------------
# check_gt()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_numeric() |>
    check_gt(0)
}

f(1:2)
try(f(0:2))
#> Error : Argument `x` must be strictly greater than 0.
#> 
#> min(x, na.rm = TRUE) = 0
#> 
#> Call: f(x = 0:2)

#----------------------------------------------------------------------------
# check_geq()
#----------------------------------------------------------------------------
f <- function(x) {
  x |>
    check_numeric() |>
    check_geq(2)
}

f(2:3)
try(f(1:3))
#> Error : Argument `x` must be greater than or equal to 2.
#> 
#> min(x, na.rm = TRUE) = 1
#> 
#> Call: f(x = 1:3)