Raises error if predicate is FALSE in any columns selected

Share:

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

1
2
3
4
assert(data, predicate, ..., error_fun = assertr_stop)

assert_(data, predicate, ..., .dots, error_fun = assertr_stop,
  .nameofpred = "")

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.

error_fun

Function to call if assertion fails. Takes one error string. Uses stop by default

.dots

Use assert_() to select columns using standard evaluation.

.nameofpred

Text representation of predicate for printing in case of assertion violation. Will automatically be retrieved if left blank (default)

Value

data if predicate assertion is TRUE. error if not.

Note

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

See Also

verify insist assert_rows insist_rows

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
# returns mtcars
assert(mtcars, not_na, vs)

# equivalent statements using standard evaluation
assert_(mtcars, not_na, "vs")
var <- "vs"
assert_(mtcars, not_na, var)

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

# equivalent using standard evaluation
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
## End(Not run)