assertr (version 2.7)

insist_rows: Raises error if dynamically created predicate is FALSE for any row after applying row reduction function

Description

Meant for use in a data analysis pipeline, this function applies a function to a data frame that reduces each row to a single value. Then, a predicate generating function is applied to row reduction values. It will then use these predicates to check each of the row reduction values. If any of these predicate applications yield FALSE, this function will raise an error, effectively terminating the pipeline early. If there are no FALSEs, this function will just return the data that it was supplied for further use in later parts of the pipeline.

Usage

insist_rows(
  data,
  row_reduction_fn,
  predicate_generator,
  ...,
  success_fun = success_continue,
  error_fun = error_stop
)

insist_rows_( data, row_reduction_fn, predicate_generator, ..., .dots, success_fun = success_continue, error_fun = error_stop )

Arguments

data

A data frame

row_reduction_fn

A function that returns a value for each row of the provided data frame

predicate_generator

A function that is applied to the results of the row reduction function. This will produce, a true predicate function to be applied to every element in the vector that the row reduction function returns.

...

Comma separated list of unquoted expressions. Uses dplyr's select to select columns from data.

success_fun

Function to call if assertion passes. Defaults to returning data.

error_fun

Function to call if assertion fails. Defaults to printing a summary of all errors.

.dots

Use insist_rows_() to select columns using standard evaluation.

Value

By default, the data is returned if dynamically created predicate assertion is TRUE and and error is thrown if not. If a non-default success_fun or error_fun is used, the return values of these function will be returned.

Details

For examples of possible choices for the success_fun and error_fun parameters, run help("success_and_error_functions")

See Also

insist assert_rows assert verify

Examples

Run this code
# NOT RUN {
# returns mtcars
insist_rows(mtcars, maha_dist, within_n_mads(30), mpg:carb)

library(magrittr)                    # for piping operator

mtcars %>%
  insist_rows(maha_dist, within_n_mads(10), vs:am)
  # anything here will run

# }
# NOT RUN {
mtcars %>%
  insist_rows(maha_dist, within_n_mads(1), everything())
  # the assertion is untrue so
  # nothing here will run
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab