stabilize_arg: Ensure an argument meets expectations

View source: R/stabilize_arg.R

stabilize_argR Documentation

Ensure an argument meets expectations

Description

stabilize_arg() is used by other functions such as stabilize_int(). Use stabilize_arg() if the type-specific functions will not work for your use case, but you would still like to check things like size or whether the argument is NULL.

stabilize_arg_scalar() is optimized to check for length-1 vectors.

Usage

stabilize_arg(
  x,
  ...,
  allow_null = TRUE,
  allow_na = TRUE,
  min_size = NULL,
  max_size = NULL,
  x_arg = caller_arg(x),
  call = caller_env(),
  x_class = object_type(x)
)

stabilize_arg_scalar(
  x,
  ...,
  allow_null = TRUE,
  allow_zero_length = TRUE,
  allow_na = TRUE,
  x_arg = caller_arg(x),
  call = caller_env(),
  x_class = object_type(x)
)

Arguments

x

The argument to stabilize.

...

Arguments passed to methods.

allow_null

⁠(length-1 logical)⁠ Is NULL an acceptable value?

allow_na

⁠(length-1 logical)⁠ Are NA values ok?

min_size

⁠(length-1 integer)⁠ The minimum size of the object. Object size will be tested using vctrs::vec_size().

max_size

⁠(length-1 integer)⁠ The maximum size of the object. Object size will be tested using vctrs::vec_size().

x_arg

⁠(length-1 character)⁠ An argument name for x. The automatic value will work in most cases, or pass it through from higher-level functions to make error messages clearer in unexported functions.

call

(environment) The execution environment to mention as the source of error messages.

x_class

⁠(length-1 character)⁠ The class name of x to use in error messages. Use this if you remove a special class from x before checking its coercion, but want the error message to match the original class.

allow_zero_length

⁠(length-1 logical)⁠ Are zero-length vectors acceptable?

Value

x, unless one of the checks fails.

Examples

wrapper <- function(this_arg, ...) {
  stabilize_arg(this_arg, ...)
}
wrapper(1)
wrapper(NULL)
wrapper(NA)
try(wrapper(NULL, allow_null = FALSE))
try(wrapper(NA, allow_na = FALSE))
try(wrapper(1, min_size = 2))
try(wrapper(1:10, max_size = 5))
stabilize_arg_scalar("a")
stabilize_arg_scalar(1L)
try(stabilize_arg_scalar(1:10))

stbl documentation built on Nov. 5, 2025, 6:02 p.m.