Check that criteria only refer to columns present in the data
Source:R/check-criteria.R
check_criteria.RdA pre-flight check run before any step is evaluated. For every step in
criteria, the variables used by a formula predicate, the by column(s)
and the arms column must exist in data. If any are missing,
check_criteria() aborts with one line per offending step, naming the step
number, its label and the missing column(s), instead of letting evaluation
fail later with object 'x' not found.
Value
NULL, invisibly. Called for its side effect of aborting when a
criterion refers to a column not in data.
Details
apply_criteria() and continue_criteria() call this automatically
(after the expected / join step, so joined columns count as present).
Call it directly to validate a criteria set against a dataset without
applying it.
What is checked
Variables in a formula predicate count as present when they are a column of
data, or when they resolve in the formula's environment (e.g. a local
min_age <- 18 used as ~ age >= min_age), matching how predicates are
evaluated. A consequence is that a missing column whose name matches an
object on the search path (e.g. c or t) is not reported, because
evaluation would find that object too. The dplyr helpers n(), n_distinct(), cur_group() and
cur_group_id() and the .data / .env pronouns are always allowed;
.data$x is checked as column x. Function names, pkg::fn references
and the right-hand side of obj$field are not treated as columns.
Function predicates cannot be inspected statically and are skipped (their
by / arms columns are still checked); the error message notes which
steps were skipped.
See also
apply_criteria(), continue_criteria().
Other criteria builders:
cf_criteria(),
cf_criterion(),
exclude(),
group_exclude(),
group_include(),
include(),
randomise(),
select_within()
Examples
dat <- mock_parallel_rct(n_participants = 50, seed = 1)
crit <- cf_criteria() |>
include(~ age >= 18, label = "Adults only") |>
include(~ !is.na(consent_date), label = "Consent recorded")
check_criteria(crit, dat) # passes silently
# A column that is not in the data is reported with its step
bad <- crit |> exclude(~ lost_to_follow_up, label = "Lost to follow-up")
try(check_criteria(bad, dat))
#> Error in .check_criteria(criteria, data) :
#> Criteria refer to column(s) not found in `data`.
#> ✖ Criterion 3 "Lost to follow-up" refers to column(s) not in data: lost_to_follow_up.