atm_vec {dataclass}R Documentation

Validator: Check if element is atomic

Description

This function is used to check whether something is atomic. Atomic elements are represented by simple vectors, (i.e., numeric, logical, character) but also include special vectors like date vectors. You can use this function to check the length of a vector. 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

atm_vec(
  max_len = Inf,
  min_len = 1,
  level = "error",
  allow_na = FALSE,
  allow_dups = TRUE
)

Arguments

max_len

The maximum length of an atomic element

min_len

The minimum length of an atomic element

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 atomic * Determines whether the check will throw warning or error * Optionally checks for element length

Examples

# Define a dataclass for testing atomic:
my_dataclass <-
  dataclass(
    num_val = num_vec(),
    # Setting warn means a warning will occur if violation is found
    # The default is "error" which is stricter and will halt upon violation
    atm_val = atm_vec(level = "warn")
  )

# While `num_val` must be a number, `atm_val` can be any atomic element!
my_dataclass(
  num_val = c(1, 2, 3),
  atm_val = Sys.Date()
)

my_dataclass(
  num_val = c(1, 2, 3),
  atm_val = c(TRUE, FALSE)
)

my_dataclass(
  num_val = c(1, 2, 3),
  atm_val = c("This is", "a character!")
)

[Package dataclass version 0.3.0 Index]