Require that x contains a number of elements matching a specification
Source:R/assert_contains.R
assert_contains.Rdassert_contains() applies spec to each element of x independently
and counts how many elements match. It returns x unchanged if that count
falls between min_matches and max_matches (inclusive), and errors
otherwise.
Usage
assert_contains(
x,
spec,
...,
min_matches = 1,
max_matches = NULL,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)Arguments
- x
The object to stabilize.
- spec
(function)A single stabilizer or coercion function, such as ato_*function (to_chr(), etc.), astabilize_*function (stabilize_chr(), etc.), or a function produced by aspecify_*()call (specify_chr(), etc.). Applied independently to each element ofx.- ...
Arguments passed to methods.
- min_matches
(
integer(1)) The minimum number of elements ofxthat must matchspec. Must be>= 0. Set to0(with non-NULLmax_matches) to check only an upper bound on the number of matches.- max_matches
(
integer(1)orNULL) The maximum number of elements ofxthat may matchspec. Must be>= min_matches.NULL(default) skips the upper-bound check.- 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, unchanged, if the number of elements of x matching spec
is between min_matches and max_matches, or an error condition with
classes <stbl-error>, <stbl-condition>, <rlang_error>, <error>,
<condition>, and a specific class by failure mode:
<stbl-error-too_few_matches>when fewer thanmin_matcheselements matchspec.<stbl-error-too_many_matches>when more thanmax_matcheselements matchspec.
See also
Other stabilization functions:
assert_not(),
assert_present(),
stabilize_all_of(),
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
# By default, at least 1 element must match spec
assert_contains(list("1", "a", "b"), stabilize_int)
#> [[1]]
#> [1] "1"
#>
#> [[2]]
#> [1] "a"
#>
#> [[3]]
#> [1] "b"
#>
# Require at least 2 matching elements
assert_contains(list("1", "2", "a"), stabilize_int, min_matches = 2)
#> [[1]]
#> [1] "1"
#>
#> [[2]]
#> [1] "2"
#>
#> [[3]]
#> [1] "a"
#>
# Errors because no elements are int-ish
try(assert_contains(list("a", "b"), stabilize_int))
#> Error in eval(expr, envir) :
#> `list("a", "b")` <list> must contain at least 1 element matching `spec`.
#> ✖ Found 0 matching elements.
# Errors because too many elements are int-ish
try(assert_contains(list("1", "2", "3"), stabilize_int, max_matches = 2))
#> Error in eval(expr, envir) :
#> `list("1", "2", "3")` <list> must contain at most 2 elements matching
#> `spec`.
#> ✖ Found 3 matching elements.
# spec is applied to each element, not to x as a whole, so a scalar spec
# like stabilize_int_scalar() still matches every element of a list
assert_contains(
list(1L, 2L, 3L),
stabilize_int_scalar,
min_matches = 2,
max_matches = 3
)
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
#>
#> [[3]]
#> [1] 3
#>
# Use min_matches = 0 to check only an upper bound on the number of matches
assert_contains(
list("1", "a", "b"),
stabilize_int,
min_matches = 0,
max_matches = 1
)
#> [[1]]
#> [1] "1"
#>
#> [[2]]
#> [1] "a"
#>
#> [[3]]
#> [1] "b"
#>