data_validator {dataclass}R Documentation

Convert a dataclass to a data frame validator

Description

If you intend to use your dataclass to validate data frame like object such as tibbles, data frames, or data tables, pass the dataclass into this function to modify behavior.

Usage

data_validator(x, strict_cols = TRUE)

Arguments

x

A dataclass object

strict_cols

Should additional columns be allowed in the output?

Value

A function with the following properties:

* A modified dataclass function designed to accept data frames * A single argument to test new data frames * Each column in a new data frame will be tested * An error occurs if new data passed to the returned function are invalid * Data is returned if new data passed to the returned function are valid

Examples

# Define a dataclass for creating data! Pass to data_validator():
my_df_dataclass <-
  dataclass(
    dte_col = dte_vec(),
    chr_col = chr_vec(),
    # Custom column validator ensures values are positive!
    new_col = function(x) all(x > 0)
  ) |>
  data_validator()

# Validate a data frame or data frame like objects!
data.frame(
  dte_col = as.Date("2022-01-01"),
  chr_col = "String!",
  new_col = 100
) |>
  my_df_dataclass()

# Allow additional columns in output
test_df_class <-
  dataclass(
    dte_col = dte_vec()
  ) |>
  data_validator(strict_cols = FALSE)

tibble::tibble(
  dte_col = as.Date("2022-01-01"),
  other_col = "a"
) |>
  test_df_class()

[Package dataclass version 0.3.0 Index]