knitr::opts_chunk$set(echo = TRUE)
library(r2rtf)
library(dplyr)
library(tidyr)

Example

This example shows how to create a simplified adverse events summary table as below.

knitr::include_graphics("pdf/ae_example.pdf")

Step 1: Create data for RTF table

data(r2rtf_adae)
ae_t1 <- r2rtf_adae %>%
  group_by(TRTA) %>%
  mutate(n_subj = n_distinct(USUBJID)) %>%
  group_by(TRTA, AEDECOD) %>%
  summarise(
    n_ae = n_distinct(USUBJID),
    pct = round(n_ae / unique(n_subj) * 100, 2)
  ) %>%
  dplyr::filter(n_ae > 5) %>%
  # only show AE terms with at least 5 subjects in one treatment group.
  pivot_longer(cols = c(n_ae, pct), names_to = "var", values_to = "value") %>%
  unite(temp, TRTA, var) %>%
  pivot_wider(names_from = temp, values_from = value, values_fill = 0)
knitr::kable(ae_t1)

Step 2: Define table format

ae_tbl <- ae_t1 %>%
  rtf_title(
    "Analysis of Subjects With Specific Adverse Events",
    c(
      "(Incidence > 5 Subjects in One or More Treatment Groups)",
      "ASaT"
    )
  ) %>%
  rtf_colheader(" | Placebo | Drug High Dose | Drug Low Dose",
    col_rel_width = c(4, rep(2, 3))
  ) %>%
  rtf_colheader(" | n | (%) | n | (%) | n | (%)",
    col_rel_width = c(4, rep(1, 6)),
    border_top = c("", rep("single", 6)),
    border_left = c("single", rep(c("single", ""), 3))
  ) %>%
  rtf_body(
    col_rel_width = c(4, rep(1, 6)),
    text_justification = c("l", rep("c", 6)),
    border_left = c("single", rep(c("single", ""), 3))
  ) %>%
  rtf_footnote(c("{^\\dagger}This is footnote 1", "This is footnote 2"), ) %>%
  rtf_source("Source: xxx")

Step 3: Output

# Output .rtf file
ae_tbl %>%
  rtf_encode() %>%
  write_rtf("rtf/ae_example.rtf")


Merck/r2rtf documentation built on April 18, 2024, 11:51 a.m.