Checks whether values are within a range (arg_between()), greater than some value (arg_gt()), greater than or equal to some value (arg_gte()), less than some value (arg_lt()), or less than or equal to some value (arg_lte()).
Usage
arg_between(
x,
range = c(0, 1),
inclusive = TRUE,
.arg = rlang::caller_arg(x),
.msg = NULL,
.call
)
arg_gt(x, bound = 0, .arg = rlang::caller_arg(x), .msg = NULL, .call)
arg_gte(x, bound = 0, .arg = rlang::caller_arg(x), .msg = NULL, .call)
arg_lt(x, bound = 0, .arg = rlang::caller_arg(x), .msg = NULL, .call)
arg_lte(x, bound = 0, .arg = rlang::caller_arg(x), .msg = NULL, .call)Arguments
- x
the argument to be checked
- range
a vector of two values identifying the required range.
- inclusive
a logical vector identifying whether the range boundaries are inclusive or not. If only one value is supplied, it is applied to both range boundaries. Default is
TRUE, which will not throw an error ifxis equal to the boundaries.- .arg
the name of the argument supplied to
xto appear in error messages. The default is to extract the argument's name usingrlang::caller_arg(). Ignored if.msgis supplied.- .msg
an optional alternative message to display if an error is thrown instead of the default message.
- .call
the execution environment of a currently running function, e.g.
.call = rlang::current_env(). The corresponding function call is retrieved and mentioned in error messages as the source of the error. Passed toerr(). Set toNULLto omit call information. The default is to search along the call stack for the first user-facing function in another package, if any.- bound
the bound to check against. Default is 0.
Details
x is not checked for type, as it is possible for values other than numeric values to be passed and compared; however, an error will be thrown if typeof(x) is not equal to typeof(range) or typeof(bound). NA values of x will be removed. The arguments to range, inclusive, and bound are checked for appropriateness.
Examples
z <- 2
try(arg_between(z, c(1, 3))) # No error
try(arg_between(z, c(1, 2))) # No error
try(arg_between(z, c(1, 2),
inclusive = FALSE)) # Error
#> Error : `z` must be between 1 and 2 (exclusive).
try(arg_between(z, c(1, 2),
inclusive = c(TRUE, FALSE))) # Error
#> Error : `z` must be greater than or equal to 1 and less than 2.
try(arg_gt(z, 0)) # No error
try(arg_gt(z, 2)) # Error
#> Error : `z` must be greater than 2.
try(arg_gte(z, 2)) # No error
try(arg_lt(z, 0)) # Error
#> Error : `z` must be negative.
try(arg_lt(z, 2)) # Error
#> Error : `z` must be less than 2.
try(arg_lte(z, 2)) # No error
try(arg_lte(z, "3")) # Error: wrong type
#> Error : `z` must be less than or equal to "3".