Check arg is within a range
check_range.RdCheck if all elements of arg lie within the interval bounded by lower and
upper.
Usage
check_range(
arg,
lower,
upper,
lower_inclusive = TRUE,
upper_inclusive = TRUE,
signal = "error",
msg = NULL,
call. = FALSE
)Arguments
- arg
(Numeric)
The argument to check.- lower
(Scalar numeric)
The lower bound of the interval.- upper
(Scalar numeric)
The upper bound of the interval. Must be greater than or equal tolower.- lower_inclusive
(Scalar logical:
TRUE)
Whether the lower bound is inclusive.TRUEmakes the bound closed (arg >= lower).FALSEmakes the bound open (arg > lower).- upper_inclusive
(Scalar logical:
TRUE)
Whether the upper bound is inclusive.TRUEmakes the bound closed (arg <= upper).FALSEmakes the bound open (arg < upper).- 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_range() examples
#----------------------------------------------------------------------------
library(bkcheck)
#----------------------------------------------------------------------------
# Closed interval [0, 1]
#----------------------------------------------------------------------------
f <- function(x) {
x |>
check_numeric() |>
check_range(0, 1)
}
f(c(0, 0.5, 1))
try(f(c(0, 0.5, 1.5)))
#> Error : Argument `x` must be in the interval [0, 1].
#>
#> min(x, na.rm = TRUE) = 0
#> max(x, na.rm = TRUE) = 1.5
#>
#> Call: f(x = c(0, 0.5, 1.5))
#----------------------------------------------------------------------------
# Open interval (0, 1)
#----------------------------------------------------------------------------
g <- function(x) {
x |>
check_numeric() |>
check_range(0, 1, lower_inclusive = FALSE, upper_inclusive = FALSE)
}
g(c(0.25, 0.5, 0.75))
try(g(c(0, 0.5, 1)))
#> Error : Argument `x` must be in the interval (0, 1).
#>
#> min(x, na.rm = TRUE) = 0
#> max(x, na.rm = TRUE) = 1
#>
#> Call: g(x = c(0, 0.5, 1))