assertr (version 2.7)

assert: Raises error if predicate is FALSE in any columns selected

Description

Meant for use in a data analysis pipeline, this function will just return the data it's supplied if there are no FALSEs when the predicate is applied to every element of the columns indicated. If any element in any of the columns, when applied to the predicate, is FALSE, then this function will raise an error, effectively terminating the pipeline early.

Usage

assert(
  data,
  predicate,
  ...,
  success_fun = success_continue,
  error_fun = error_stop
)

assert_( data, predicate, ..., .dots, success_fun = success_continue, error_fun = error_stop )

Arguments

data

A data frame

predicate

A function that returns FALSE when violated

...

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 assert_() to select columns using standard evaluation.

Value

By default, the data is returned if 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

verify insist assert_rows insist_rows

Examples

Run this code
# NOT RUN {
# returns mtcars
assert(mtcars, not_na, vs)

# return mtcars
assert(mtcars, not_na, mpg:carb)

library(magrittr)                    # for piping operator

mtcars %>%
  assert(in_set(c(0,1)), vs)
  # anything here will run

# }
# NOT RUN {
mtcars %>%
  assert(in_set(c(1, 2, 3, 4, 6)), carb)
  # the assertion is untrue so
  # nothing here will run
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab