arg_length: Check Argument Length

View source: R/arg_length.R

arg_lengthR Documentation

Check Argument Length

Description

Checks whether an argument has a specified length.

Usage

arg_length(x, len = 1L, .arg = rlang::caller_arg(x), .msg = NULL, .call)

Arguments

x

the argument to be checked

len

integer; the allowed length(s) of x. Default is 1.

.arg

the name of the argument supplied to x to appear in error messages. The default is to extract the argument's name using rlang::caller_arg(). Ignored if .msg is 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 to err(). Set to NULL to omit call information. The default is to search along the call stack for the first user-facing function in another package, if any.

Details

len can contain multiple allowed counts; an error will be thrown only if length(x) is not equal to any value of len.

Value

Returns NULL invisibly if an error is not thrown.

See Also

length(), arg_non_null() to specifically test that an object's length is or is not 0.

Examples

obj <- 1:4

try(arg_length(obj, 1))
try(arg_length(obj, 4))
try(arg_length(obj, c(1, 4)))

# These test the same thing:
try(arg_length(obj, c(0:3)))
try(when_not_null(obj,
                  arg_length(1:3)))

arg documentation built on April 9, 2026, 5:09 p.m.