gtsummaryr-flextable

Does the gtsummary package have a function that allows the exported table to include footnotes on every page?


As discussed in the previous question, it seems difficult for gtsummary to include the same footnote on every page when exporting RTF using flextable; the footnote can only appear at the end of the table. I would like to ask whether gtsummary has a function similar to tt_to_flextable in rtables. It seems that tt_to_flextable can do the pagination and include the title/footnote on each page.


Solution

  • You can split a gtsummary table in various ways using the tbl_split_by_*() functions. In the example below, we are splitting a large table into a list of 14 tables. Each of the 14 tables has the titles, footnotes, source notes, etc.

    # pak::pak("ddsjoberg/gtsummary")
    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '2.3.0.9006'
    
    tbl <- 
      cards::ADAE |> 
      tbl_hierarchical(
        variables = c(AESOC, AETERM),
        by = TRTA,
        denominator = cards::ADSL,
        id = USUBJID
      ) |> 
      modify_caption("Table 2. Adverse Events")
    
    # this is a long table, that does not fit on a page
    nrow(tbl)
    #> [1] 265
    
    # split the table with 20 rows on each page.
    tbl_split <-
      tbl_split_by_rows(tbl, row_numbers = seq(20, nrow(tbl), by = 20))
    
    # tbl_split is a list of 14 gtsummary tables, all of which have the footnotes/titles
    length(tbl_split)
    #> [1] 14
    

    Created on 2025-08-22 with reprex v2.1.1

    If you then take this list of tables and print them with a page break between each, you'll get the result you're after.