Tables for Clinical Reports
The output end of a clinical submission is tables, listings and graphs, and the
tables are the conservative part: same shells decade after decade, one row per
characteristic, one column per arm, counts and percentages with the denominators
shown. Two Roche packages build them. rtables is the engine: you declare a layout
as a tree of row and column splits and analysis steps, and the layout is then applied
to data. tern is the clinical layer on top: it knows what a demographics table’s
rows actually are, so analyze_vars on AGE and SEX produces the n, mean (SD),
median, range and count-with-percent rows a regulator expects, instead of a bare
mean.
Both packages use analysis data whose population and treatment variables are already defined. This page reads the independent synthetic study used by the previous page. Its shared generator derives ADSL and ADAE with base R. An event is treatment-emergent when its start falls between the first dose and seven days after the last dose, inclusive. The fixture includes repeated events and a pretreatment event for each treated subject.
The observations, event labels, and treatment arms are invented for method instruction. The data use CC0 1.0 and support no clinical comparison between arms.
library(rtables)library(tern)library(dplyr)study <- readRDS("/opt/fixtures/pharmaverse/synthetic_study.rds")adsl <- study$ref_adsl
arms <- c("Placebo", "Active Low", "Active High")
# Tables want factors: the level set must be the same under every arm,# which character columns converted on the fly cannot guarantee.adsl <- adsl %>% filter(SAFFL == "Y") %>% mutate( ACTARM = factor(ACTARM, levels = arms), SEX = factor(SEX), RACE = factor(RACE) )A demographics table in rtables
Section titled “A demographics table in rtables”The layout is the program. split_cols_by declares one column per arm,
add_colcounts puts the arm sizes into the header, and analyze applies a
statistic to a variable within each cell of the split. Nothing is computed until
build_table meets the data, and the same layout would run unchanged on another
study’s ADSL.
tbl <- basic_table( title = "Table 14-1.1", subtitles = "Demographics and Baseline Characteristics, Safety Population") %>% split_cols_by("ACTARM") %>% add_colcounts() %>% analyze("AGE") %>% build_table(adsl)
tblTable 14-1.1Demographics and Baseline Characteristics, Safety Population
————————————————————————————————————————— Placebo Active Low Active High (N=10) (N=10) (N=10)—————————————————————————————————————————Mean 39.60 40.40 41.50The same table, the clinical version, with tern
Section titled “The same table, the clinical version, with tern”rtables’ analyze gives one statistic per call. tern’s analyze_vars gives the
standard set in one step, and it types the variable for you: a numeric gets n, mean
(SD), median and range, a factor gets counts with percentages. The percentages come
from the cell counts in the header, which is why add_colcounts stays in the
layout.
tbl <- basic_table() %>% split_cols_by("ACTARM") %>% add_colcounts() %>% analyze_vars(vars = c("AGE", "SEX", "RACE")) %>% build_table(adsl)
tbl Placebo Active Low Active High (N=10) (N=10) (N=10)—————————————————————————————————————————————————————————————————————AGE n 10 10 10 Mean (SD) 39.6 (8.9) 40.4 (9.3) 41.5 (9.1) Median 39.5 40.5 41.5 Min - Max 27.0 - 53.0 26.0 - 54.0 28.0 - 55.0SEX n 10 10 10 F 4 (40%) 6 (60%) 5 (50%) M 6 (60%) 4 (40%) 5 (50%)RACE n 10 10 10 ASIAN 9 (90%) 1 (10%) 0 BLACK OR AFRICAN AMERICAN 1 (10%) 9 (90%) 0 WHITE 0 0 10 (100%)An adverse event table with the right denominators
Section titled “An adverse event table with the right denominators”The AE summary has one trap: the analysis dataset is long, one row per event, and a
subject with five events appears five times. The count the table must show is
subjects with at least one event, and the percentage must divide by the subjects at
risk, not by rows. Two pieces of the call handle that: count_occurrences counts
subjects, and alt_counts_df hands the layout the ADSL arm sizes as the
denominators. Here also is the treatment-emergent flag, the ADaM column that keeps
pre-treatment events out of the summary.
adae <- study$adae
adae_em <- adae %>% filter(SAFFL == "Y", TRTEMFL == "Y") %>% mutate(ACTARM = factor(ACTARM, levels = arms))
tbl_ae <- basic_table() %>% split_cols_by("ACTARM") %>% add_colcounts() %>% count_occurrences(vars = "AEDECOD") %>% build_table(adae_em, alt_counts_df = adsl)
# Print one row per invented event term.tbl_aecat("event terms in the full table:", nrow(tbl_ae), "\n") Placebo Active Low Active High (N=10) (N=10) (N=10)————————————————————————————————————————————————————————Synthetic event A 10 (100%) 10 (100%) 10 (100%)Synthetic event B 6 (60.0%) 4 (40.0%) 5 (50.0%)Synthetic event C 7 (70.0%) 8 (80.0%) 8 (80.0%)Synthetic event D 8 (80.0%) 8 (80.0%) 8 (80.0%)event terms in the full table: 4GSK’s piece: separating computation from display with tfrmt
Section titled “GSK’s piece: separating computation from display with tfrmt”rtables computes and formats in one step. GSK’s tfrmt takes the opposite position:
the analysis program emits a long, machine-readable results table, an ARD, and a
separate display specification formats it. The same ARD can then feed an RTF, an
HTML table or a figure without recomputing. The ARD here is the AE summary computed
directly, one row per term per arm per statistic.
top_terms <- adae_em %>% count(AEDECOD, sort = TRUE) %>% slice_head(n = 4) %>% pull(AEDECOD)
denom <- adsl %>% count(ACTARM, name = "N")
ard <- adae_em %>% filter(AEDECOD %in% top_terms) %>% distinct(USUBJID, AEDECOD, ACTARM) %>% count(AEDECOD, ACTARM, name = "n") %>% left_join(denom, by = "ACTARM") %>% mutate(pct = 100 * n / N) %>% select(AEDECOD, ACTARM, n, pct) %>% tidyr::pivot_longer( cols = c("n", "pct"), names_to = "param", values_to = "value" ) %>% mutate(label = AEDECOD, column = ACTARM) %>% select(label, param, column, value)
as.data.frame(ard) label param column value1 Synthetic event A n Placebo 102 Synthetic event A pct Placebo 1003 Synthetic event A n Active Low 104 Synthetic event A pct Active Low 1005 Synthetic event A n Active High 106 Synthetic event A pct Active High 1007 Synthetic event B n Placebo 68 Synthetic event B pct Placebo 609 Synthetic event B n Active Low 410 Synthetic event B pct Active Low 4011 Synthetic event B n Active High 512 Synthetic event B pct Active High 5013 Synthetic event C n Placebo 714 Synthetic event C pct Placebo 7015 Synthetic event C n Active Low 816 Synthetic event C pct Active Low 8017 Synthetic event C n Active High 818 Synthetic event C pct Active High 8019 Synthetic event D n Placebo 820 Synthetic event D pct Placebo 8021 Synthetic event D n Active Low 822 Synthetic event D pct Active Low 8023 Synthetic event D n Active High 824 Synthetic event D pct Active High 80The tfrmt specification then says, declaratively, which ARD columns play which
display role and how the values render. frmt_combine("{n} ({pct}%)") is the
familiar n (x.x%) display, defined once and applied everywhere.
library(tfrmt)library(ggplot2)
ae_tfrmt <- tfrmt( label = "label", param = "param", column = "column", value = "value", body_plan = body_plan( frmt_structure( group_val = ".default", label_val = ".default", frmt_combine("{n} ({pct}%)", n = frmt("xx"), pct = frmt("xx.x")) ) ))
# gt renders to HTML; print_to_ggplot renders the same display as a figure,# which is the form this page can embed.p <- print_to_ggplot(ae_tfrmt, ard)ggsave( "outputs/tfrmt-ae.png", plot = p, width = 8, height = 2.5, dpi = 150, bg = "white")
The figure, the ARD, and the tern table use the same subject counts and safety population denominators. The generator assigns event A to every treated subject and selects the other events through arithmetic rules on synthetic identifiers. The repeated event A records count only once per subject. The differences between arms follow those invented rules and provide no evidence about a medicine.
The computation occurs in the ARD, while the display specification controls its format. The same numeric table can support another output format without another count of the clinical records.
The rest of the TLG vocabulary
Section titled “The rest of the TLG vocabulary”chevron packages whole standard outputs as templates on top of rtables and
tern, rlistings covers listings, and teal turns the same ADaM data into
exploratory Shiny apps. tfrmtbuilder is a Shiny app for writing a tfrmt
specification interactively, and docorator handles the page furniture, headers,
footers and file formats, around a finished display. None of them needs new
concepts: each is the layout model or the ARD split at a larger scale.
Further reading
Section titled “Further reading”-
The build_table reference explains how alt_counts_df supplies column totals. This is the denominator needed when the event table contains several rows per subject.
-
The count_occurrences reference defines subject identifiers and denominator choices. It explains why an event count and a count of affected subjects answer different questions.
-
The reference defines the label, parameter, value, and column roles. Its body_plan interface explains how one numeric results table produces the formatted count and percentage cells.
-
The specification defines the treatment-emergent window and repeated event cases. The same shared fixture supplies the SDTM and ADaM lessons.