exemplar: Create a validation function from an exemplar

View source: R/exemplar.R

exemplarR Documentation

Create a validation function from an exemplar

Description

This function will print a validation function that can be used to make sure that a new object looks like the input (its exemplar).

Some checks are commented out. This is because the exemplar does not meet the criteria (eg. no duplicate values) or the checks are too specific to be used by default (range checks). The intention is that users will modify the validation functions to meet their needs before placing them in pipelines and scripts.

The functions produced by exemplar require at least R 3.5 (due to improvements made to ⁠\link{stopifnot}⁠ but otherwise requires no dependencies. That is, exemplar generates functions that do not need exemplar or any other packages to run.

Usage

exemplar(
  x,
  ...,
  .function_suffix = NULL,
  .enable_range_assertions = FALSE,
  .enable_deviance_assertions = FALSE,
  .allowed_deviance = 4
)

Arguments

x

The object to use as the exemplar of the validation function.

...

Additional arguments used when building the assertions. Currently this is only used to apply validation to only certain columns in a data frame. This uses tidyselect functions. Refer to dplyr package for more information.

.function_suffix

By default the generated function will be named after the input, eg. exemplar(mtcars) will generate a function named validate_mtcars. This parameter allows overriding the suffix, eg. exemplar(mtcars, .function_suffix = "my_data") will generate a function named validate_my_data.

.enable_range_assertions

Assertions for numeric columns/vectors will include range assertions, to ensure that any new data is within the range of the exemplar. These assertions will be commented out by default, unless the argument to this parameter is TRUE.

.enable_deviance_assertions

Assertions for numeric columns/vectors will include deviance assertions, to ensure that any new data is within a number of standard deviations of the mean of the exemplar, as configured by .allowed_deviance. These assertions will be commented out by default, unless the argument to this parameter is TRUE.

.allowed_deviance

Configures the number of standard deviations from the mean that new data is allowed to be. The deviance assertions are commented out by default unless .enable_deviance_assertions is set to TRUE. The .allowed_deviance defaults to 4, such that new data is within 4 standard deviations of the mean based on the statistical properties of the exemplar.

Examples

exemplar(mtcars)
exemplar(mtcars$gear)
exemplar(mtcars, -cyl)
exemplar(mtcars, starts_with("d"))
exemplar(mtcars, .function_suffix = "my_data")


mdneuzerling/exemplar documentation built on Jan. 13, 2024, 1:46 a.m.