Learn R Programming

assertions (version 0.3.0)

assert_between: Assert input is between a specified minimum and maximum value

Description

Assert a number is between a specified minimum and maximum value. To check all numbers in a vector / matrix are between minimum and maximum values, see assert_all_between()

Usage

assert_between(
  x,
  minimum,
  maximum,
  inclusive = TRUE,
  msg = NULL,
  call = rlang::caller_env(),
  arg_name = NULL
)

Value

invisible(TRUE) if x is between the specified minimum and maximum values, otherwise aborts with the error message specified by msg

Arguments

x

An object to check

minimum

The minimum value to compare against

maximum

The maximum value to compare against

inclusive

should comparisons allow equality with the minimum and maximum values? (flag)

msg

A character string containing the error message to display if x is not between the specified minimum and maximum values (string)

call

Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details.

arg_name

Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name).

Examples

Run this code
try({
assert_between(3, 1, 4) # Passes
assert_between(3, 1, 4) # Passes
assert_between(c(2,3,4), 1, 4) # Throws error (Must be a number)
assert_between('A', 1, 4) # Throws error (Must be a number)
assert_between(5, 1, 4, msg = "custom error message") # Throws custom error
})

Run the code above in your browser using DataLab