Skip to contents

to_chr() checks whether an argument can be coerced to character without losing information, returning it silently if so. Otherwise an informative error message is signaled.

stabilize_chr() can check more details about the argument, but is slower than to_chr().

stabilize_chr_scalar() and to_chr_scalar() are optimized to check for length-1 character vectors.

Usage

stabilize_chr(
  x,
  ...,
  allow_null = TRUE,
  allow_na = TRUE,
  min_size = NULL,
  max_size = NULL,
  regex = NULL,
  x_arg = caller_arg(x),
  call = caller_env(),
  x_class = object_type(x)
)

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)
)

to_chr(
  x,
  allow_null = TRUE,
  x_arg = caller_arg(x),
  call = caller_env(),
  x_class = object_type(x)
)

to_chr_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().

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.

allow_zero_length

Logical. Are zero-length vectors acceptable?

Value

The argument as a character vector.

Details

These functions have two important differences from base::as.character():

  • lists and data.frames are not coerced to character. In base R, such objects are coerced to character representations of their elements. For example, as.character(list(1:3)) returns "1:10". In the unlikely event that this is the expected behavior, use as.character() instead.

  • NULL values can be rejected as part of the call to this function (with allow_null = FALSE).

Examples

to_chr("a")
#> [1] "a"
to_chr(letters)
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
to_chr(1:10)
#>  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"
to_chr(1 + 0i)
#> [1] "1+0i"
to_chr(NULL)
#> NULL
try(to_chr(NULL, allow_null = FALSE))
#> Error in eval(expr, envir, enclos) : 
#>   `NULL` must not be <NULL>.

to_chr_scalar("a")
#> [1] "a"
try(to_chr_scalar(letters))
#> Error in eval(expr, envir, enclos) : 
#>   `letters` must be a single <character>.
#>  `letters` has 26 values.

stabilize_chr(letters)
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"
stabilize_chr(1:10)
#>  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"
stabilize_chr(NULL)
#> NULL
try(stabilize_chr(NULL, allow_null = FALSE))
#> Error in eval(expr, envir, enclos) : 
#>   `NULL` must not be <NULL>.
try(stabilize_chr(c("a", NA), allow_na = FALSE))
#> Error in eval(expr, envir, enclos) : 
#>   `c("a", NA)` must not contain NA values.
#>  NA locations: 2
try(stabilize_chr(letters, min_size = 50))
#> Error in eval(expr, envir, enclos) : 
#>   `letters` must have size >= 50.
#>  26 is too small.
try(stabilize_chr(letters, max_size = 20))
#> Error in eval(expr, envir, enclos) : 
#>   `letters` must have size <= 20.
#>  26 is too big.
try(stabilize_chr(c("hide", "find", "find", "hide"), regex = "hide"))
#> Error in eval(expr, envir, enclos) : 
#>   `c("hide", "find", "find", "hide")` must match the provided regex
#> pattern.
#>  Some values do not match.
#>  Locations: 2 and 3

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>.