verify: Raises error if expression is FALSE anywhere

Description Usage Arguments Details Value Note See Also Examples

View source: R/assertions.R

Description

Meant for use in a data analysis pipeline, this function will just return the data it's supplied if all the logicals in the expression supplied are TRUE. If at least one is FALSE, this function will raise a error, effectively terminating the pipeline early

Usage

1
verify(data, expr, success_fun = success_continue, error_fun = error_stop)

Arguments

data

A data frame, list, or environment

expr

A logical expression

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.

Details

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

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.

Note

See vignette("assertr") for how to use this in context

See Also

assert insist

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
verify(mtcars, drat > 2)     # returns mtcars
## Not run: 
verify(mtcars, drat > 3)     # produces error
## End(Not run)


library(magrittr)            # for piping operator

## Not run: 
mtcars %>%
  verify(drat > 3) %>%
  # anything here will not run
## End(Not run)

mtcars %>%
  verify(nrow(mtcars) > 2)
  # anything here will run

alist <- list(a=c(1,2,3), b=c(4,5,6))
verify(alist, length(a) > 2)
verify(alist, length(a) > 2 && length(b) > 2)
verify(alist, a > 0 & b > 2)

## Not run: 
alist %>%
  verify(alist, length(a) > 5)
  # nothing here will run
## End(Not run)

lorenzwalthert/assertr documentation built on May 20, 2019, 4:06 p.m.