rsplitgtsummary

Split long gtsummary() table to n smaller tables


While creating my descriptive table with the gtsummary() package I get a very long table. Is it possible to split such a table in multiple shorter tables?

With this example dataset I would like to show what I mean:

library(gtsummary)
# make dataset with a few variables to summarize
trial2 <- trial %>% select(age, grade, response, trt)

# summarize the data with our package
table1 <- tbl_summary(trial2)
table1

gives this output:

enter image description here

desired output:

enter image description here

I tried:

library(gtsummary)
# make dataset with a few variables to summarize
trial2 <- trial %>% select(age)
trial3 <- trial %>% select(grade)
trial4 <- trial %>% select(response)
trial5 <- trial %>% select(trt)

# summarize the data with our package
table1 <- tbl_summary(trial2)
table2 <- tbl_summary(trial3)
table3 <- tbl_summary(trial4)
table4 <- tbl_summary(trial5)

table1
table2
table3
table4

Solution

  • UPDATE: This code has been functionalized and put in a package. Here is the help file https://www.danieldsjoberg.com/gtsummary/reference/tbl_split.html

    Interesting, I've never thought about splitting a gtsummary table. It is straight-forward enough to do, and I've written a small function to do it saved in this GitHub Gist https://gist.github.com/ddsjoberg/1f400732f0bf9bc9ae6ad1dd8b1cf914

    The function takes a gtsummary tbl as the input, as well as the variable names where you would like the split to occur. It then returns a list of gtsummary tables, each a subset of the input tbl.

    tt <- 
      trial %>%
      tbl_summary(by = trt) %>%
      add_p()
    
    split_gtsummary_tbl(tt, .split_after = c("marker", "grade"))
    

    If this is something that you think would be helpful to many users, please make a feature request at https://github.com/ddsjoberg/gtsummary/issues/new?assignees=&labels=&template=feature_request.md&title= . We can then incorporate a robust function to split the tbls.