rgtsummary

Modifying Statistics for Specific Rows in gtsummary


I want to generate an Adverse Event Table showing Total Events and Total Subjects with Events. Below is my code and the current result:

adae <- random.cdisc.data::cadae
adsl <- random.cdisc.data::cadsl

ard_event_count <- adae %>%
  mutate(event_flag = "Total Events") %>%
  ard_tabulate(
    data = .,
    variables = event_flag,
    by = "TRT01P",
    denominator = adsl
  ) %>%
  filter(variable_level == "Total Events")

ard_event <- adae %>%
  distinct(USUBJID, TRT01P) %>%
  mutate(event_flag = "Total Subjects with Events") %>%
  ard_tabulate(
    data = .,
    variables = event_flag,
    by = "TRT01P",
    denominator = adsl
  ) %>%
  filter(variable_level == "Total Subjects with Events")

ard_hier <- ard_stack_hierarchical(
  data = adae,
  variables = c(AESOC, AEBODSYS, AESEV),
  by = "TRT01P",
  denominator = adsl,
  id = "USUBJID"
)

ard <- bind_ard(ard_event_count, ard_event, ard_hier)

tbl <- tbl_ard_hierarchical(
  ard,
  variables = c(event_flag, AESOC, AEBODSYS, AESEV),
  by = "TRT01P"
) %>%
  modify_header(
    label = "",
    all_stat_cols() ~ "**{level}**  \n**N = {n}**"
  ) %>%
  modify_footnote(everything() ~ NA)
  
tbl

enter image description here

I’m wondering if there’s a good way to remove the percentage part of Total Events, because when I modify the first part of code to keep only N and n, an error occurs later in tbl_ard_hierarchical.

ard_event_count <- adae %>%
  mutate(event_flag = "Total Events") %>%
  ard_tabulate(
    data = .,
    variables = event_flag,
    by = "TRT01P",
    denominator = adsl,
    statistic = everything() ~ c("n", "N"),
  ) %>%
  filter(variable_level == "Total Events")

enter image description here


Solution

  • In tbl_ard_hierarchical you can specify the statistics depending on the existence of p. That way you avoid the error raised by a missing p in ard_event_count:

    1. drop p in ard_event_count as suggested:
    ard_event_count <- adae %>%
      mutate(event_flag = "Total Events") %>%
      ard_tabulate(
        ## ...
        statistic = everything() ~ c("n", "N"),
      ) %>%
      filter(variable_level == "Total Events")
    
    1. use p only if it exists:
    tbl <- tbl_ard_hierarchical(
      statistic = ~ '{ifelse(exists("p"),
                             sprintf("%s (%s %%)", n, p),
                             n
                             )
                    }',
      ard,
      variables = c(event_flag, AESOC, AEBODSYS, AESEV),
      by = "TRT01P"
    ) %>%
    ## ...
    

    Note the use of a) single quotes around the whole glue expression and b) the double % to escape the special meaning of % in sprintf

    tbl_ard_hierarchical with separate statistics per incoming card