Split table containing grouped data with gt package

I have a dataframe grouped with dplyr::group_by and formatted into a table with the gt-package, the output looks like this:

Is there a way to separate this table into multiple or sub-tables using a function of the gt-package, so that every group generated with dplyr::group_by is rendered as separate table (i.e. for printing consecutively)?
Unfortunately, gt_split has no option to do this…

library(tidyverse)
library(gt)

results <- split(iris,~Species) |> imap(\(x,y)gt(x) |> tab_header(title=y))

results[1]
results[2] # etc. 

#another form 
results_in_one <- gt_group(.list=results)

Thank very much.
A good idea the first split, transform to a gt object and then re-group those object.
I just tried it, works fine.

However, I find one downside:
The resulting table have differing widths, that they keep, when re-grouped. Including tab_options(table.width = … ) only fixes this downside as long as … is sufficiently large to cover all resulting table widths. If not, you still get tables of differing width.

You can make them 100% as wide as their html container , by adding to the imap portion

... |> imap(\(x,y)gt(x)|> 
tab_options(table.width = pct(100)) |> 
tab_header(title=y))

Yes, that is what I did, but perhaps did not understand correctly.

Is there a way to pipe the result directly into `gt_group?

I tried in several ways, but always get the error: `data` must be a `gt_tbl` object, not a list.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.