Apply a predicate check function to each element of a vector and throw an error if any element fails the check.
Arguments
- .x
A list or atomic vector.
- .f
A function or formula to apply to each element of
.x(passed toas_function()). Must return a logical vector of allTRUEfor no error to occur. Non-logicalandNAvalues will trigger an error.- ...
Additional arguments passed to
cli_abort()which forwards unmatched arguments toabort().- call
The execution environment of a currently running function, e.g.
caller_env(). The function will be mentioned in error messages as the source of the error. See the call argument ofabort()for more information.
Details
walk_check() is designed to work with predicate functions,
throwing an error indicating the element that fails the check.
If you wish to use a function for .f that itself errors,
pass contextual information to that function
directly (e.g., using a shorthand anonymous function),
as the error will be thrown from that function's context
and won't have direct access to information from the caller
such as ... and call.
Examples
x <- list(1, 2, "a")
walk_check(x, is.atomic)
walk_check(x, ~ length(.x) == 1L)
walk_check(x, is.numeric) |> try()
#> Error in eval(expr, envir) :
#> Check result for `.x[[3]]` is not TRUE.
walk_check(x, \(el) nchar(el) == 1L)
# Named elements are shown in the error.
x <- list(a = 1, b = 2, c = "a")
walk_check(x, is.numeric) |> try()
#> Error in eval(expr, envir) :
#> Check result for `.x[['c']]` (index: 3) is not TRUE.
