Checks that every element of x lies within the interval [lower, upper].
Each end is independently inclusive (closed, the default) or exclusive (open)
via lower_inclusive / upper_inclusive, so every interval shape is
expressible: [a, b], ]a, b[, ]a, b], [a, b[. Pass NULL for a bound to
leave that side unbounded (]-Inf, b], [a, Inf[). Comparisons use only < /
>, so this works for any comparable type — numbers, integers, dates,
date-times, even strings — and never coerces.
Usage
assert_between(
x,
lower = NULL,
upper = NULL,
lower_inclusive = TRUE,
upper_inclusive = TRUE,
na_ok = FALSE,
null_ok = FALSE,
arg = rlang::caller_arg(x),
call = rlang::caller_env()
)Arguments
- x
Object to check.
- lower
Lower bound, or
NULLfor unbounded below. Must be the same class asx(no coercion is performed); a mismatched class compares silently wrong.- upper
Upper bound, or
NULLfor unbounded above.- lower_inclusive
Single logical. If
TRUE(default) the lower bound is inclusive (x >= lower); ifFALSEit is exclusive (x > lower).- upper_inclusive
Single logical. If
TRUE(default) the upper bound is inclusive (x <= upper); ifFALSEit is exclusive (x < upper).- na_ok
Single logical. If
FALSE(default) anyNAinxfails the check; ifTRUE,NAelements are ignored and only the non-missing values are range-checked. Either wayxis returned unchanged (including anyNA).- null_ok
Single logical. If
TRUE, aNULLvalue passes the check without error. Use this for optional arguments that default toNULL. Defaults toFALSE, soNULLis rejected unless you opt in.- arg
Name used to refer to
xin error messages. Defaults to the name of the expression passed asx.- call
Environment used as the error's call context. Defaults to the calling function, so errors point at the user's code.
Examples
assert_between(5, 0, 10) # [0, 10]
assert_between(0.5, 0, 1, lower_inclusive = FALSE) # ]0, 1]
assert_between(c(1, NA, 3), 0, 10, na_ok = TRUE)
assert_between(Sys.time(), lower = as.POSIXct("2000-01-01"))
