Try to coerce or validate x as all of several specs
Source:R/stabilize_all_of.R
stabilize_all_of.Rdstabilize_all_of() validates and coerces x by applying every function in
... to x, independently. x must satisfy every spec, and all specs must
agree on the coerced result; if any spec fails, or specs disagree on the
coerced value, an informative error is thrown. stabilise_all_of() is a
synonym.
Usage
stabilize_all_of(
x,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
stabilise_all_of(
x,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)Arguments
- x
The object to stabilize.
- ...
Unnamed stabilizer functions, such as
stabilize_*functions (stabilize_chr(), etc.),to_*functions (to_chr(), etc.), functions produced byspecify_*()calls (specify_chr(), etc.), orassert_*()functions (such asassert_not()) that return their input unchanged. Each is applied to the originalx;xmust pass every one of them, and they must all return the same value.- x_arg
(
character(1)) The name of the object being stabilized to use in error messages. 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
(
character(1)) The class name of the object being stabilized to use in error messages. Use this if you remove a special class from the object before checking its coercion, but want the error message to match the original class.
Value
x coerced or validated by every function in ..., or an error
condition with classes <stbl-error>, <stbl-condition>, <rlang_error>,
<error>, <condition>, and a specific class by failure mode:
<stbl-error-empty_specs>when no functions are supplied in....<stbl-error-named_spec>when any element of...is named.<stbl-error-cant_stabilize_all_of>when any provided function fails.<stbl-error-inconsistent_all_of>when every function succeeds, but they don't all produce the same coerced value.
See also
Other stabilization functions:
assert_contains(),
assert_not(),
assert_present(),
stabilize_any_of(),
stabilize_arg(),
stabilize_chr(),
stabilize_chr_scalar(),
stabilize_date(),
stabilize_date_scalar(),
stabilize_dbl(),
stabilize_dbl_scalar(),
stabilize_df(),
stabilize_dttm(),
stabilize_dttm_scalar(),
stabilize_dur(),
stabilize_dur_scalar(),
stabilize_fct(),
stabilize_fct_scalar(),
stabilize_int(),
stabilize_int_scalar(),
stabilize_lgl(),
stabilize_lgl_scalar(),
stabilize_lst(),
stabilize_one_of(),
stabilize_time(),
stabilize_time_scalar(),
to_chr(),
to_chr_scalar(),
to_date(),
to_date_scalar(),
to_dbl(),
to_dbl_scalar(),
to_dttm(),
to_dttm_scalar(),
to_dur(),
to_dur_scalar(),
to_fct(),
to_fct_scalar(),
to_int(),
to_int_scalar(),
to_lgl(),
to_lgl_scalar(),
to_time(),
to_time_scalar()
Examples
# Returns x unchanged when all functions succeed
stabilize_all_of(1L, stabilize_int, stabilize_int_scalar)
#> [1] 1
# Each spec is applied to the original x, independently
stabilize_all_of("ab", specify_chr(regex = "^a"), specify_chr(regex = "b$"))
#> [1] "ab"
# Errors with a combined message when a spec fails
try(stabilize_all_of("a", stabilize_int, stabilize_chr))
#> Error in eval(expr, envir) :
#> `"a"` must match all of the provided stabilizers.
#> ✖ `"a"` <character> must be coercible to <integer> (Locations: 1)
# Errors because each spec sees the original "1" (a string); the second
# spec would succeed on 1L, but doesn't get the chance to see it
try(stabilize_all_of("1", stabilize_int, specify_dbl(coerce_character = FALSE)))
#> Error in eval(expr, envir) :
#> `"1"` must match all of the provided stabilizers.
#> ✖ Can't coerce `"1"` <character> to <double>.
# Errors when specs succeed but disagree on the coerced value
# (stabilize_int() keeps 1L an integer; specify_dbl() makes it a double)
try(stabilize_all_of(1L, stabilize_int, specify_dbl()))
#> Error in eval(expr, envir) :
#> `1L` must be coerced the same way by every provided stabilizer.
#> ℹ The provided stabilizers succeeded, but disagreed on the coerced value.