is_valid_variable_name: Is the string a valid variable name?

View source: R/is-code.R

assert_all_are_valid_variable_namesR Documentation

Is the string a valid variable name?

Description

Checks strings to see if they are valid variable names.

Usage

assert_all_are_valid_variable_names(
  x,
  allow_reserved = TRUE,
  allow_duplicates,
  na_ignore = FALSE,
  severity = getOption("assertive.severity", "stop")
)

assert_any_are_valid_variable_names(
  x,
  allow_reserved = TRUE,
  allow_duplicates,
  na_ignore = FALSE,
  severity = getOption("assertive.severity", "stop")
)

is_valid_variable_name(x, allow_reserved = TRUE, allow_duplicates)

Arguments

x

Input to check.

allow_reserved

If TRUE then "..." and "..1", "..2", etc. are considered valid.

allow_duplicates

Deprecated and ignored.

na_ignore

A logical value. If FALSE, NA values cause an error; otherwise they do not. Like na.rm in many stats package functions, except that the position of the failing values does not change.

severity

How severe should the consequences of the assertion be? Either "stop", "warning", "message", or "none".

Value

The assert_* functions return nothing but throw an error if the corresponding is_* function returns FALSE.

See Also

make.names.

Examples

make_random_string <- function(n)
{
  paste0(sample(letters, n, replace = TRUE), collapse = "")
}
long <- c(make_random_string(10000), make_random_string(10001))
x <- c("x", "y_y0.Y", ".", "x y", "...", "..1", long)
unname(is_valid_variable_name(x))
unname(is_valid_variable_name(x, allow_reserved = FALSE))
#These examples should fail.
assertive.base::dont_stop(
  assert_all_are_valid_variable_names(c("...", "..1"), allow_reserved = FALSE)
)

assertive.code documentation built on May 31, 2023, 5:35 p.m.