4

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
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • do you want to keep it as one table and then have that table wrap at a certain length? – Mike Feb 09 '21 at 14:35
  • The issue is that my original table lasts over 3 pages. My idea was to split the table in 2 pieces and show them side by side. I was wondering if this is possible per code. – TarJae Feb 09 '21 at 14:52
  • 1
    I would also check whether the output types, i.e. gt, flextable, huxtable, etc., Have any built-in ways to split the tables across pages. Although, these solutions wouldn't respect the variable groupings. – Daniel D. Sjoberg Feb 09 '21 at 16:08

1 Answers1

2

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.

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • This is perfect Daniel D. Sjoberg. Indeed, in certain situations, especially at the beginning of data analysis summary tables with all variables are an important feature. I like the design and the functionality of gtsummary tables so I asked for this feature. One more question: This may be off topic, but would you provide a saving option within your code of the tables (like table1.png, table2.png etc...). Thank you for your great job. – TarJae Feb 09 '21 at 16:06
  • 1
    Here's an example how to can save a gtsummary to a png. http://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html#images-1 This method works for saving to RTF, HTML, etc as well. – Daniel D. Sjoberg Feb 09 '21 at 16:21
  • With my dataframe I get this error: `Fehler: Can't coerce element 1 from a double to a integer`. Can you help? I can traceback or debug. But I think the comment area is not appropriate for this. – TarJae Feb 09 '21 at 19:48
  • Create a reprex and I can take a look – Daniel D. Sjoberg Feb 10 '21 at 02:42
  • Thank you for your reply: I have started a new question here: – TarJae Feb 10 '21 at 10:55
  • Hi @DanielD.Sjoberg, thanks for the fix, but the function didn't seem to make it into your bstfun packaage that you linked, unless the name changed, as far as I can tell. Is it available anywhere else? – Jake L Feb 24 '22 at 05:04
  • Updated the link. The function has been migrated to gtsummary – Daniel D. Sjoberg Feb 24 '22 at 14:48