Check arg is bounded
check_bound.RdCheck 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 forarg.- signal
(string:
c("error", "warning", "message"))
Must be one of"error","warning", or"message"."error"(default) callsbase::stop()to signal an error message."warning"callsbase::warning()to signal a warning message."message"callsbase::message()to signal a message.- msg
(string or
NULL:NULL)
A string that replaces the default message. Set asNULLto use the default message.- call.
(Scalar logical:
c(FALSE, TRUE))
Passed tostop()orwarning(). The defaultmsgincludes it's own formatted call, hencecall.should beFALSE(default). If you insert your ownmsg, then you may wantcall. = TRUE.
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)