Skip to contents

This function is equivalent to stabilize_chr(), but it is optimized to check for length-1 character vectors.

Usage

stabilize_chr_scalar(
  x,
  ...,
  allow_null = TRUE,
  allow_zero_length = TRUE,
  allow_na = TRUE,
  regex = NULL,
  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_zero_length

Logical. Are zero-length vectors acceptable?

allow_na

Logical. Are NA values ok?

regex

Character scalar. An optional regex pattern to compare the value(s) of x against. If a complex regex pattern throws an error, try installing the stringi package with install.packages("stringi").

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 of rlang::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 from x before checking its coercion, but want the error message to match the original class.

Value

x, unless one of the checks fails.

Examples

stabilize_chr_scalar(TRUE)
#> [1] "TRUE"
stabilize_chr_scalar("TRUE")
#> [1] "TRUE"
try(stabilize_chr_scalar(c(TRUE, FALSE, TRUE)))
#> Error in eval(expr, envir, enclos) : 
#>   `c(TRUE, FALSE, TRUE)` must be a single <character>.
#>  `c(TRUE, FALSE, TRUE)` has 3 values.
stabilize_chr_scalar(NULL)
#> NULL
try(stabilize_chr_scalar(NULL, allow_null = FALSE))
#> Error in eval(expr, envir, enclos) : 
#>   `NULL` must not be <NULL>.