Converting rtables to tinytable

Setup

library(tinytable)
library(rtables)

# Load the S3 method for rtables conversion
source("rtables_to_tinytable.R")

Example 1: Basic Table with Styling

Let’s start with a simple table and apply some visual styling to demonstrate tinytable’s capabilities.

# Create a basic rtables table
lyt1 <- basic_table(
  title = "Summary Statistics by Treatment Arm",
  main_footer = "Data from clinical trial") %>%
  split_cols_by("ARM") %>%
  analyze("AGE", function(x) {
    in_rows("Mean" = mean(x), "SD" = sd(x), "N" = length(x), .formats = c("xx.x", "xx.x", "xx"))
  })

tbl1 <- build_table(lyt1, ex_adsl)

# Convert to tinytable using tt() - S3 dispatch to tinytable.rtables()
tt(tbl1) %>%
  style_tt(i = 1, bold = TRUE, color = "white", background = "#2C5F8D", fontsize = 1.3) %>%
  style_tt(i = 2:3, background = c("lightgrey", "lightblue"))
Summary Statistics by Treatment Arm
A: Drug X B: Placebo C: Combination
Data from clinical trial
Mean 33.8 35.4 35.4
SD 6.6 7.9 7.7
N 134 134 132

Example 2: Nested Column Splits

This example shows how multi-level column headers are preserved with spanning columns.

lyt2 <- basic_table(
  title = "Age Summary by Treatment and Gender"
) %>%
  split_cols_by("ARM") %>%
  split_cols_by("SEX") %>%
  analyze("AGE", mean, format = "xx.x")

tbl2 <- build_table(lyt2, ex_adsl)

# Convert and display
tt2 <- tt(tbl2)
tt2
Age Summary by Treatment and Gender
A: Drug X B: Placebo C: Combination
F M U UNDIFFERENTIATED F M U UNDIFFERENTIATED F M U UNDIFFERENTIATED
mean 32.8 35.6 31.7 28.0 34.1 37.4 31.0 NA 35.2 35.4 35.2 45.0

Example 3: Row Groups with Indentation

This example demonstrates row grouping where variable names become section headers with indented data rows.

lyt3 <- basic_table(
  title = "Demographics Table"
) %>%
  split_cols_by("ARM") %>%
  analyze("AGE", function(x) {
    in_rows(
      "Mean" = mean(x),
      "Median" = median(x),
      .formats = "xx.x"
    )
  }) %>%
  analyze("BMRKR1", function(x) {
    in_rows(
      "Mean" = mean(x),
      "SD" = sd(x),
      .formats = "xx.xx"
    )
  })

tbl3 <- build_table(lyt3, ex_adsl)

# Convert to tinytable
tt(tbl3) %>%
  style_tt(i = groupi, bold = TRUE, background = "#E8F4F8")
Demographics Table
A: Drug X B: Placebo C: Combination
AGE AGE AGE AGE
Mean 33.8 35.4 35.4
Median 33.0 35.0 35.0
BMRKR1 BMRKR1 BMRKR1 BMRKR1
Mean 5.97 5.70 5.62
SD 3.55 3.31 3.49

Example 4: Row Splits with Summary Statistics

Tables with row splits show counts and nested analysis results with proper indentation.

lyt4 <- basic_table(
  title = "Analysis by Gender"
) %>%
  split_cols_by("ARM") %>%
  split_rows_by("SEX", split_fun = drop_split_levels) %>%
  summarize_row_groups() %>%
  analyze("AGE", mean, format = "xx.x")

tbl4 <- build_table(lyt4, ex_adsl)

# Convert to tinytable
tt4 <- tt(tbl4)
tt4
Analysis by Gender
A: Drug X B: Placebo C: Combination
F 79 (59.0%) 77 (57.5%) 66 (50.0%)
mean 32.8 34.1 35.2
M 51 (38.1%) 55 (41.0%) 60 (45.5%)
mean 35.6 37.4 35.4
U 3 (2.2%) 2 (1.5%) 4 (3.0%)
mean 31.7 31.0 35.2
UNDIFFERENTIATED 1 (0.7%) 0 (0.0%) 2 (1.5%)
mean 28.0 NA 45.0

Example 5: Complex Nested Structure

This example demonstrates multiple levels of row nesting with proper hierarchical indentation.

lyt5 <- basic_table(
  title = "Multi-Level Demographics",
  main_footer = "Nested splits by Gender and Stratum"
) %>%
  split_cols_by("ARM") %>%
  split_rows_by("SEX") %>%
  split_rows_by("STRATA1") %>%
  analyze("AGE", function(x) {
    in_rows(
      "n" = length(x),
      "Mean (SD)" = c(mean(x), sd(x)),
      .formats = c("xx", "xx.x (xx.x)")
    )
  })

tbl5 <- build_table(lyt5, ex_adsl)

# Convert to tinytable (first 20 rows for brevity)
tt(tbl5)
Multi-Level Demographics
A: Drug X B: Placebo C: Combination
Nested splits by Gender and Stratum
F F F F
A
n 21 24 18
Mean (SD) 31.1 (4.5) 32.1 (5.5) 34.2 (5.0)
B
n 25 27 21
Mean (SD) 32.8 (7.0) 35.3 (9.1) 36.6 (9.7)
C
n 33 26 27
Mean (SD) 33.7 (6.2) 34.7 (5.7) 34.8 (6.9)
M M M M
A
n 16 19 20
Mean (SD) 35.6 (6.4) 39.4 (8.7) 33.5 (7.1)
B
n 21 17 21
Mean (SD) 35.3 (7.5) 37.1 (9.5) 36.0 (7.4)
C
n 14 19 19
Mean (SD) 35.9 (7.7) 35.8 (7.9) 36.6 (10.2)
U U U U
A
n 1 1 1
Mean (SD) 33.0 (NA) 27.0 (NA) 38.0 (NA)
B
n 1 1 1
Mean (SD) 28.0 (NA) 35.0 (NA) 37.0 (NA)
C
n 1 0 2
Mean (SD) 34.0 (NA) NA 33.0 (2.8)
UNDIFFERENTIATED UNDIFFERENTIATED UNDIFFERENTIATED UNDIFFERENTIATED
A
n 0 0 1
Mean (SD) NA NA 44.0 (NA)
B
n 0 0 0
Mean (SD) NA NA NA
C
n 1 0 1
Mean (SD) 28.0 (NA) NA 46.0 (NA)