Creates a single data rule
Usage
rule(expr, name = NA, allow_na = FALSE, negate = FALSE, ...)
# S3 method for class 'rule'
print(x, ...)Arguments
- expr
an expression which dictates which determines when a rule is good. Note that the expression is evaluated in
check_data(), within the given framework. That means, for example if a the data given tocheck_data()is anarrowdataset, the expression must be mappable fromarrow(see also arrow documentation). The expression can be given as a string as well.- name
an optional name for the rule for reference
- allow_na
does the rule allow for NA values in the data? default value is FALSE. Note that when NAs are introduced in the expression,
allow_nahas no effect. Eg when the ruleas.numeric(vs) %in% c(0, 1)finds the values ofvsasc("1", "A"), the rule will throw a fail regardless of the value ofallow_naas the NA is introduced in the expression and is not found in the original data. However, when the values ofvsarec("1", NA),allow_nawill have an effect.- negate
is the rule negated, only applies to the expression not allow_na, that is, if
expr = mpg > 10,allow_na = TRUE, andnegate = TRUE, it would match allmpg <= 10as well as NAs.- ...
additional arguments that are carried along for your documentation, but are not used. Could be for example date, person, contact, comment, etc
- x
a rule to print
Examples
r <- rule(mpg > 10)
r
#> <Verification Rule>
#> expr: 'mpg > 10'
#> name: 'Rule for: mpg'
#> allow NA: FALSE
#> negated: FALSE
r2 <- rule(mpg > 10, name = "check that mpg is reasonable", allow_na = TRUE,
negate = FALSE, author = "me", date = Sys.Date())
r2
#> <Verification Rule>
#> expr: 'mpg > 10'
#> name: 'check that mpg is reasonable'
#> allow NA: TRUE
#> negated: FALSE
#> author: 'me'
#> date: '2026-04-10'
check_data(mtcars, r)
#> check_type name expr allow_na negate tests pass fail warn
#> <char> <char> <char> <lgcl> <lgcl> <int> <int> <int> <char>
#> 1: row_rule Rule for: mpg mpg > 10 FALSE FALSE 32 32 0
#> error time
#> <char> <difftime>
#> 1: 0.0004093647 secs
rs <- ruleset(
rule(mpg > 10),
rule(cyl %in% c(4, 6)), # missing 8
rule(qsec >= 14.5 & qsec <= 22.9)
)
rs
#> <Verification Ruleset with 3 elements>
#> [1] 'Rule for: mpg' matching `mpg > 10` (allow_na: FALSE)
#> [2] 'Rule for: cyl' matching `cyl %in% c(4, 6)` (allow_na: FALSE)
#> [3] 'Rule for: qsec' matching `qsec >= 14.5 & qsec <= 22.9` (allow_na: FALSE)
check_data(mtcars, rs)
#> check_type name expr allow_na negate tests
#> <char> <char> <char> <lgcl> <lgcl> <int>
#> 1: row_rule Rule for: mpg mpg > 10 FALSE FALSE 32
#> 2: row_rule Rule for: cyl cyl %in% c(4, 6) FALSE FALSE 32
#> 3: row_rule Rule for: qsec qsec >= 14.5 & qsec <= 22.9 FALSE FALSE 32
#> pass fail warn error time
#> <int> <int> <char> <char> <difftime>
#> 1: 32 0 0.0003030300 secs
#> 2: 18 14 0.0010342598 secs
#> 3: 32 0 0.0003006458 secs