rules <- list(
pressures = rule(dbp < sbp),
stay = rule(admitted <= discharged)
)
Vitals <- new_table_type(
"Vitals",
columns = list(
id = column(type_integer(min = 1), unique = TRUE),
age = column(type_double(min = 0, max = 130), unit = "a"),
sbp = column(type_double(min = 0, max = 400), unit = "mm[Hg]"),
dbp = column(type_double(min = 0, max = 300), unit = "mm[Hg]"),
admitted = type_date(),
discharged = option(type_date()),
ward = column(type_factor(c("ED", "ICU", "ward")), required = FALSE)
),
open = TRUE,
rules = rules
)12 Declaring a table type
new_table_type() declares a table type: the columns that every value has, as type_table() does for a field, and rules that every row satisfies. Vitals holds the admissions of a study:
| A column | Declares |
|---|---|
type_double(min = 0, max = 130) |
the type of its cells, and their range |
option(type_date()) |
that cells may be missing |
column(..., unit = "a") |
the unit of its cells, a UCUM code by convention |
column(..., unique = TRUE) |
that its cells are distinct |
column(..., required = FALSE) |
that a value may leave it out |
With open = TRUE, a value has other columns too, after the declared ones, of any name and of any column type.
v <- Vitals(data.frame(
id = 1:2,
age = c(40, 52),
sbp = c(120, 140),
dbp = c(80, 85),
admitted = as.Date(c("2026-01-04", "2026-01-09")),
discharged = as.Date(c("2026-01-07", NA)),
smoker = c(FALSE, TRUE)
))
v<demo::Vitals table> 2 rows ⨉ 7 columns id age sbp dbp admitted discharged smoker integer double double double Date Date logical [a] [mm[Hg]] [mm[Hg]] 1 NA 1 1 40 120 80 2026-01-04 2026-01-07 FALSE 2 2 52 140 85 2026-01-09 NA TRUE
12.1 Checking data
A value is checked as a whole when it is made. An error lists the problems of every column, and of every rule once the columns are valid, with their rows and the cells that were read:
Vitals(data.frame(
id = c(1L, 2L, 2L),
age = c(40, 152, 61),
sbp = c(120, 118, NaN),
dbp = c(80, 130, 95),
admitted = as.Date(c("2026-01-04", "2026-01-09", "2026-02-02"))
))Error:
! Invalid `demo::Vitals`: 4 problems
- `discharged` is required. [missing_column]
- `id[[3]]`: 2 appears more than once. [not_unique]
- `age[[2]]` must be at most 130; found 152. [above_max]
- `sbp[[3]]` must be a number; found NaN. 1 of 3 elements is NaN. Where a calculation may give no number, `nan = TRUE` in the type admits NaN. [nan_value]
Rules were not evaluated: 4 fields are invalid.
raw <- Raven(
id = 1:3,
age = c(40, 52, 61),
sbp = c(120, 118, 155),
dbp = c(80, 130, 95),
admitted = as.Date(c("2026-01-04", "2026-01-09", "2026-02-02")),
discharged = as.Date(c("2026-01-07", "2026-01-02", NA))
)Vitals(raw)Error:
! Invalid `demo::Vitals`: 2 problems
- Rule pressures: dbp < sbp; found dbp = 130, sbp = 118. [rule_violation at dbp[[2]]]
- Rule stay: admitted <= discharged; found admitted = 2026-01-09, discharged = 2026-01-02. [rule_violation at admitted[[2]]]
An error lists the first 100 problems. r8check() reports every one, and refuses nothing (Checking data).
12.2 Changing a value
A declared column that is replaced is checked, with the rules that read it:
v$sbp <- c(120, 60)Error:
! Invalid `demo::Vitals` from `x$sbp <- value`: 1 problem
- Rule pressures: dbp < sbp; found dbp = 85, sbp = 60. [rule_violation at dbp[[2]]]
`sbp` is declared by `demo::Vitals`. A transformed column takes another name, or Raven(x) gives the columns without the type.
The result of a calculation is another quantity, and takes another name, or the table gives up its type:
standardized <- (v$age - mean(v$age)) / sd(v$age)
v$age_z <- standardized
r <- Raven(v)
r$age <- standardized
r8columns(r)$unit[1] NA NA "mm[Hg]" "mm[Hg]" NA NA NA NA
v$age <- standardizedError:
! Invalid `demo::Vitals` from `x$age <- value`: 1 problem
- `age[[1]]` must be at least 0; found -0.7071067811865476. [below_min]
Rules were not evaluated: 1 field is invalid.
`age` is declared by `demo::Vitals`. A transformed column takes another name, or Raven(x) gives the columns without the type.
Rows, columns, and bound rows of valid tables are valid, and are not checked again, except that rbind() checks the columns whose cells are distinct. A value keeps its type while it has the columns that the type requires. Without one of them it is a raven, with the units of the declaration:
class(v[1, ])[1][1] "demo::Vitals"
class(v[c("age", "smoker")])[1][1] "R8::Raven"
12.3 A table among other types
A table is a type as any other: a field holds one, a trait is implemented for one, and a generic dispatches on one:
Fit <- new_struct("Fit", fields = list(cases = Vitals, more = option(Raven)))
fit <- Fit(cases = v, more = r)
fit$cases$age[1] 40 52
implement(summary, Vitals) <- function(object, ...) {
c(cases = nrow(object), mean_age = mean(object$age))
}
summary(fit$cases) cases mean_age
2 46