Ensure a logical argument meets expectations
Source:R/stabilize_lgl.R
, R/stabilize_lgl_scalar.R
, R/to_lgl.R
, and 1 more
stabilize_lgl.Rd
to_lgl()
checks whether an argument can be coerced to
logical without losing information, returning it silently if so.
Otherwise an informative error message is signaled.
stabilize_lgl()
can check more details about the argument, but is slower
than to_lgl()
.
stabilize_lgl_scalar()
and to_lgl_scalar()
are optimized to check for
length-1 logical vectors.
Usage
stabilize_lgl(
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_lgl_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)
)
to_lgl(
x,
allow_null = TRUE,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
to_lgl_scalar(
x,
allow_null = TRUE,
allow_zero_length = TRUE,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
Arguments
- x
The argument to stabilize.
- ...
These dots are for future extensions and should be empty.
- allow_null
Logical. Is NULL an acceptable value?
- allow_na
Logical. Are NA values ok?
- min_size
Integer. The minimum size of the object. Object size will be tested using
vctrs::vec_size()
.- max_size
Integer. The maximum size of the object. Object size will be tested using
vctrs::vec_size()
.- x_arg
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
The execution environment of the call. See the
call
argument ofrlang::abort()
for more information.- x_class
Character. The class name of
x
to use in error messages. Use this if you remove a special class fromx
before checking its coercion, but want the error message to match the original class.- allow_zero_length
Logical. Are zero-length vectors acceptable?
Examples
to_lgl(TRUE)
#> [1] TRUE
to_lgl("TRUE")
#> [1] TRUE
to_lgl(1:10)
#> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
to_lgl(NULL)
#> NULL
try(to_lgl(NULL, allow_null = FALSE))
#> Error in eval(expr, envir, enclos) :
#> `NULL` must not be <NULL>.
try(to_lgl(letters))
#> Error in eval(expr, envir, enclos) :
#> `letters` <character> must be coercible to <logical>
#> ✖ Can't convert some values due to incompatible values.
#> • Locations: 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, …,
#> 25, and 26
try(to_lgl(list(TRUE)))
#> Error in eval(expr, envir, enclos) :
#> Can't coerce `list(TRUE)` <list> to <logical>.
to_lgl_scalar("TRUE")
#> [1] TRUE
try(to_lgl_scalar(c(TRUE, FALSE)))
#> Error in eval(expr, envir, enclos) :
#> `c(TRUE, FALSE)` must be a single <logical>.
#> ✖ `c(TRUE, FALSE)` has 2 values.
stabilize_lgl(c(TRUE, FALSE, TRUE))
#> [1] TRUE FALSE TRUE
stabilize_lgl("true")
#> [1] TRUE
stabilize_lgl(NULL)
#> NULL
try(stabilize_lgl(NULL, allow_null = FALSE))
#> Error in eval(expr, envir, enclos) :
#> `NULL` must not be <NULL>.
try(stabilize_lgl(c(TRUE, NA), allow_na = FALSE))
#> Error in eval(expr, envir, enclos) :
#> `c(TRUE, NA)` must not contain NA values.
#> • NA locations: 2
try(stabilize_lgl(letters))
#> Error in eval(expr, envir, enclos) :
#> `letters` <character> must be coercible to <logical>
#> ✖ Can't convert some values due to incompatible values.
#> • Locations: 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, …,
#> 25, and 26
try(stabilize_lgl(c(TRUE, FALSE, TRUE), min_size = 5))
#> Error in eval(expr, envir, enclos) :
#> `c(TRUE, FALSE, TRUE)` must have size >= 5.
#> ✖ 3 is too small.
try(stabilize_lgl(c(TRUE, FALSE, TRUE), max_size = 2))
#> Error in eval(expr, envir, enclos) :
#> `c(TRUE, FALSE, TRUE)` must have size <= 2.
#> ✖ 3 is too big.
stabilize_lgl_scalar(TRUE)
#> [1] TRUE
stabilize_lgl_scalar("TRUE")
#> [1] TRUE
try(stabilize_lgl_scalar(c(TRUE, FALSE, TRUE)))
#> Error in eval(expr, envir, enclos) :
#> `c(TRUE, FALSE, TRUE)` must be a single <logical>.
#> ✖ `c(TRUE, FALSE, TRUE)` has 3 values.
stabilize_lgl_scalar(NULL)
#> NULL
try(stabilize_lgl_scalar(NULL, allow_null = FALSE))
#> Error in eval(expr, envir, enclos) :
#> `NULL` must not be <NULL>.