[Experimental]

check_length(
  x,
  len = 1,
  min_len = NULL,
  max_len = NULL,
  null_ok = FALSE,
  .names = deparse(substitute(x))
)

assert_length(
  x,
  len = 1,
  min_len = NULL,
  max_len = NULL,
  null_ok = FALSE,
  .names = deparse(substitute(x))
)

test_length(
  x,
  len = 1,
  min_len = NULL,
  max_len = NULL,
  null_ok = FALSE,
  .names = deparse(substitute(x))
)

Arguments

x

An R object.

len

(optional) an integer number indicating the expected length of x (default: 1).

min_len

(optional) an integer number indicating the minimum length of x (default: NULL).

max_len

(optional) an integer number indicating the maximum length of x (default: NULL).

null_ok

(optional) a logical flag indicating if x can be NULL (default: FALSE).

.names

(optional) a character vector containing names for each object in ... (default: deparse(substitute(x))). This argument is used internally and should not be set by the user.

Value

  • test_*: TRUE if it passes the test; FALSE otherwise.

  • check_*: TRUE if it passes the test; a string with a message otherwise.

  • assertion_*: The same input as invisible if it passes the test; an error message otherwise.

  • expect_*: The same input as invisible if it passes the test; an error message otherwise.

Examples

x <- 1:2

test_length(x, len = 2)
#> [1] TRUE
#> [1] TRUE # Expected

check_length(x, len = 1) |> cli::cli_alert_warning()
#> ! x must have 1  element.
#> ! x must have 1 element. # Expected

check_length(x, min_len = 3) |> cli::cli_alert_warning()
#> ! x must have 3 or more elements.
#> ! x must have 3 or more elements. # Expected

check_length(x, max_len = 1) |> cli::cli_alert_warning()
#> ! x must have 1 or less elements.
#> ! x must have 1 or less elements. # Expected

x <- 1
check_length(x, min_len = 2, max_len = 3) |> cli::cli_alert_warning()
#> ! x must have a length between 2 and 3.
#> ! x must have a length between 2 and 3. # Expected