generic-expectations {testdat} | R Documentation |
Expectations: generic helpers
Description
These functions allow for testing of multiple columns (vars
) of a data
frame (data
), with an optional filter (flt
), using an arbitrary function
(func
).
Usage
expect_all(
vars,
func,
flt = TRUE,
data = get_testdata(),
args = list(),
func_desc = NULL
)
expect_any(
vars,
func,
flt = TRUE,
data = get_testdata(),
args = list(),
func_desc = NULL
)
Arguments
vars |
< |
func |
A function to use for testing that takes a vector as the first argument and returns a logical vector of the same length showing whether an element passed or failed. |
flt |
< |
data |
A data frame to test. The global test data is used by default. |
args |
A named list of arguments to pass to |
func_desc |
A human friendly description of |
Details
-
expect_allany()
tests the columns invars
to see whetherfunc
returnsTRUE
for each of them, and combines the results for each row using the function inallany
. Bothexpect_all()
andexpect_any()
are wrappers aroundexpect_allany()
. -
expect_all()
tests thevars
to see whetherfunc
returnsTRUE
for all of them (i.e. whether the conjunction of results of applyingfunc
to each of thevars
isTRUE
). -
expect_any()
tests thevars
to see whetherfunc
returnsTRUE
for any of them (i.e. whether the disjunction of the results of applyingfunc
to each of thevars
isTRUE
).
Value
expect_*()
functions are mainly called for their side effects. The
expectation signals its result (e.g. "success", "failure"), which is logged
by the current test reporter. In a non-testing
context the expectation will raise an error with class
expectation_failure
if it fails.
See Also
chk_*()
functions such as chk_values()
Other data expectations:
conditional-expectations
,
datacomp-expectations
,
date-expectations
,
exclusivity-expectations
,
expect_depends()
,
label-expectations
,
pattern-expectations
,
proportion-expectations
,
text-expectations
,
uniqueness-expectations
,
value-expectations
Examples
# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches *AND* < 100 horsepower
try(
expect_all(
vars = c(disp, hp),
func = chk_range,
flt = (cyl == 4),
args = list(min = 0, max = 100),
data = mtcars
)
)
# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches *OR* < 100 horsepower
try(
expect_any(
vars = c(disp, hp),
func = chk_range,
flt = (cyl == 4),
args = list(min = 0, max = 100),
data = mtcars
)
)
# Check that all variables are numeric:
try(expect_all(
vars = everything(),
func = is.numeric,
data = iris
))