chr_vec {dataclass}R Documentation

Validator: Check if element is a character

Description

This function is used to check whether something is a character. You can use this function to check the length and allowable values of character. You can also specify the level of a violation. If level is set to "warn" then invalid inputs will warn you. However, if level is set to "error" then invalid inputs will abort.

Usage

chr_vec(
  max_len = Inf,
  min_len = 1,
  allowed = NA,
  level = "error",
  allow_na = FALSE,
  allow_dups = TRUE
)

Arguments

max_len

The maximum length of a character element

min_len

The minimum length of a character element

allowed

A vector of allowable values

level

Setting "warn" throws a warning, setting "error" halts

allow_na

Should NA values be allowed?

allow_dups

Should duplicates be allowed?

Value

A function with the following properties:

* Checks whether something is a character vector * Determines whether the check will throw warning or error * Optionally checks for element length * Optionally checks for allowable values

Examples

# Define a dataclass for testing characters:
my_dataclass <-
  dataclass(
    string = chr_vec(allowed = c("this", "or", "that")),
    other_string = chr_vec()
  )

# `string` must be one of these: `c("this", "or", "that")`
my_dataclass(
  string = "this",
  other_string = "I can be anything I want (as long as I am a string)"
)

[Package dataclass version 0.3.0 Index]