stabilize_chr {stbl} | R Documentation |
Ensure a character argument meets expectations
Description
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 |
max_size |
Integer. The maximum size of the object. Object size will be
tested using |
regex |
Character scalar. An optional regex pattern to compare the
value(s) of |
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 |
x_class |
Character. The class name of |
allow_zero_length |
Logical. Are zero-length vectors acceptable? |
Details
These functions have two important differences from
base::as.character()
:
-
list
s anddata.frame
s 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, useas.character()
instead. -
NULL
values can be rejected as part of the call to this function (withallow_null = FALSE
).
Value
The argument as a character vector.
Examples
to_chr("a")
to_chr(letters)
to_chr(1:10)
to_chr(1 + 0i)
to_chr(NULL)
try(to_chr(NULL, allow_null = FALSE))
to_chr_scalar("a")
try(to_chr_scalar(letters))
stabilize_chr(letters)
stabilize_chr(1:10)
stabilize_chr(NULL)
try(stabilize_chr(NULL, allow_null = FALSE))
try(stabilize_chr(c("a", NA), allow_na = FALSE))
try(stabilize_chr(letters, min_size = 50))
try(stabilize_chr(letters, max_size = 20))
try(stabilize_chr(c("hide", "find", "find", "hide"), regex = "hide"))
stabilize_chr_scalar(TRUE)
stabilize_chr_scalar("TRUE")
try(stabilize_chr_scalar(c(TRUE, FALSE, TRUE)))
stabilize_chr_scalar(NULL)
try(stabilize_chr_scalar(NULL, allow_null = FALSE))