Test whether variables in a data frame contain only certain values.

expect_values(
  vars,
  ...,
  miss = getOption("testdat.miss"),
  flt = TRUE,
  data = get_testdata()
)

expect_range(vars, min, max, ..., flt = TRUE, data = get_testdata())

Arguments

vars

<tidy-select> A set of columns to test.

...

Vectors of valid values.

miss

A vector of values to be treated as missing. The testdat.miss or testdat.miss_text option is used by default.

flt

<data-masking> A filter specifying a subset of the data frame to test.

data

A data frame to test. The global test data is used by default.

min

Minimum value for range check.

max

Maximum value for range check.

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.

Examples


sales <- data.frame(
  sale_id = 1:5,
  date = c("20200101", "20200101", "20200102", "20200103", "20220101"),
  sale_price = c(10, 20, 30, 40, -1)
)

try(expect_values(date, 20000000:20210000, data = sales)) # Dates between 2000 and 2021
#> Error : `sales` has 1 records failing value check on variable `date`.
#> Filter: None
#> Arguments: `<int: 20000000L, 20000001L, 20000002L, 20000003L, 20000004L, ...>,`
#> `sales` has 1 records failing value check on variable `date`.
#> Filter: None
#> Arguments: `miss = <chr: NA, "">`
try(expect_range(sale_price, min = 0, max = Inf, data = sales)) # Prices non-negative
#> Error : `sales` has 1 records failing range check on variable `sale_price`.
#> Filter: None
#> Arguments: `min = 0, max = Inf`