set.seed(2026)
n <- 5000
admitted <- as.Date("2026-01-01") + sample(300, n, TRUE)
raw <- data.frame(
id = seq_len(n),
age = sample(18:90, n, TRUE),
sbp = round(rnorm(n, 130, 15)),
dbp = round(rnorm(n, 75, 8)),
ward = sample(c("ED", "ICU", NA), n, TRUE),
admitted = admitted,
discharged = admitted + sample(0:20, n, TRUE)
)
raw$age[4012] <- 17
raw$dbp[c(41, 907)] <- c(230, 300)
raw$discharged[c(12, 1180)] <- as.Date("2025-12-31")
raw$discharged[sample(n, 31)] <- NA13 Checking data
r8check() holds every row of a table against rules, and reports what it finds. It refuses nothing, so that data are checked as they were read, before they are converted or cleaned. The examples use the admissions of a study, as a data frame:
13.1 Rules
A set of rules is a list, and the name of a rule is its ID:
rules <- list(
adult = rule(age >= 18, message = "Patients are adults."),
pressures = rule(dbp < sbp),
stay = rule(admitted <= discharged),
ward = rule(ward %in% c("ED", "ICU"))
)
report <- r8check(raw, rules)
report1 of 4 rules holds on 5,000 rows adult 1 row fails (row 4,012) pressures 7 rows fail (rows 41, 907, 1,747, ...) stay 2 rows fail, 31 rows without an answer (rows 12, 1,180) ward holds, 1,614 rows without an answer $problems lists 10 problems
A row with a missing cell has no answer, and is counted apart from the rows that fail, so that “holds” says how much it was asked. Rules that require a cell say so, rule(!is.na(ward)).
The report is an R8 value of two tables. rules has the counts, which are exact:
report$rules<R8::ReportRules table> 4 rows ⨉ 6 columns rule requirement checked failed missing errors character character integer integer integer integer 1 adult age >= 18 5000 1 0 0 2 pressures dbp < sbp 5000 7 0 0 3 stay admitted <= discharged 5000 2 31 0 4 ward ward %in% c("ED", "ICU") 5000 0 1614 0
problems has a row for each rule and row that fails, with the columns, the values found, and the explanation, for at most limit rows of each rule:
r8select(report$problems, c("rule", "row", "found", "message"))<raven table> 10 rows ⨉ 4 columns rule row found message character integer character character 1 adult 4012 age = 17 Patients are adults. 2 pressures 41 dbp = 230, sbp = 145 dbp < sbp 3 pressures 907 dbp = 300, sbp = 128 dbp < sbp 4 pressures 1747 dbp = 90, sbp = 89 dbp < sbp 5 pressures 1748 dbp = 87, sbp = 87 dbp < sbp 6 pressures 2602 dbp = 90, sbp = 89 dbp < sbp 7 pressures 2903 dbp = 89, sbp = 88 dbp < sbp 8 pressures 3384 dbp = 83, sbp = 80 dbp < sbp 9 stay 12 admitted = 2026-04-21... admitted <= discharged 10 stay 1180 admitted = 2026-04-20... admitted <= discharged
Both are tables, for filters and the other functions:
raw[r8filter(report$problems, rule(rule == "pressures"))$row, c("id", "sbp", "dbp")] id sbp dbp
41 41 145 230
907 907 128 300
1747 1747 89 90
1748 1748 87 87
2602 2602 89 90
2903 2903 88 89
3384 3384 80 83
A rule that names a column that the table does not have, or that does not fit its type, is not checked, and the report says why. A rule that reads whole columns only, such as rule(mean(age) > 40), is checked once:
r8check(raw, list(old = rule(mean(age) > 40), typo = rule(agee > 18)))1 of 2 rules holds on 5,000 rows old holds typo is not checked: `agee` is not a column of the table; the nearest column is `age` $problems lists 1 problem
Rules are saved as any R object, with saveRDS() or r8write_rds(), and are checked against any table with the columns they name. A report is a value as any other, and has a JSON document.
13.2 A type
Held against a declared table, data are checked column by column, and then by the rules of the type. A rule is read where its columns read, though other cells of the row are refused:
Admissions <- new_table_type(
"Admissions",
columns = list(
id = column(type_integer(min = 1), unique = TRUE),
age = column(type_double(min = 18, max = 120), unit = "a"),
sbp = column(type_double(min = 0, max = 400), unit = "mm[Hg]"),
dbp = column(type_double(min = 0, max = 250), unit = "mm[Hg]"),
ward = option(type_factor(c("ED", "ICU"))),
admitted = type_date(),
discharged = option(type_date())
),
rules = rules[c("pressures", "stay")]
)
r8check(raw, Admissions)4 of 9 checks of demo::Admissions hold on 5,000 rows id holds age 1 row fails (row 4,012) sbp holds dbp 1 row fails (row 907) ward is not checked: must be a factor admitted holds discharged holds pressures 7 rows fail (rows 41, 907, 1,747, ...) stay 2 rows fail, 31 rows without an answer (rows 12, 1,180) $problems lists 12 problems
The report shows what a conversion needs: here, ward as a factor, and the ten rows that fail. Admissions() constructs a value once nothing is left:
raw$ward <- factor(raw$ward, c("ED", "ICU"))
bad <- unique(r8check(raw, Admissions)$problems$row)
clean <- Admissions(raw[-bad, ] |> data.frame(row.names = NULL))
dim(clean)[1] 4990 7
13.3 Files
The package R8.io checks a file against a type before it is read, with the same report and the texts of the file that do not read as their column’s type: r8check_file(), in Texts that do not read.
13.4 Requiring
r8validate() holds an object against the same rules or types, and raises an error where r8check() reports. Without rules or a type, it checks an R8 value against its own type:
r8validate(raw, rules["pressures"])Error:
! Invalid `raw`: The rule does not hold on 5,000 rows
pressures 7 rows fail (rows 41, 907, 1,747, ...)
$problems lists 5 of 7 problems
r8validate(clean)