rgtsummary

How to modify table headers in the gtsummary package?


Very simple question, but I can't find a straightforward way of doing it. I want to change the default text of a tbl_summary object to say "Variable" instead of "Characteristic" in the first column. Example:

library(gtsummary)
library(dplyr)
head(trial)
trial2 =
  trial %>%
  dplyr::select(trt, marker, stage)
tbl_summary(trial2)

Looking through the documentation, it looks like this information is stored in a .$table_header object and I could just modify it directly, but I was wondering if there is a simple way to change the header names. The function modify_header() seemed promising, but it doesn't appear to be able to change the names of the columns.


Solution

  • You are correct! The modify_header() function is the way to go to update the column headers.

    library(gtsummary)
    library(dplyr)
    
    trial %>%
      select(trt, marker, stage) %>%
      tbl_summary() %>%
      modify_header(label = "**Variable**")
    

    enter image description here