vincentarelbundock / tinytable

Simple and Customizable Tables in `R`
https://vincentarelbundock.github.io/tinytable
GNU General Public License v3.0
211 stars 18 forks source link

How to automatically add spanner for groups #316

Closed lorenzoFabbri closed 3 months ago

lorenzoFabbri commented 3 months ago

Sorry for the dumb question but I did not find any example in the tutorials. I would like to replicate the following piece of code with tinytable:

df <- tibble::as_tibble(mtcars) |>
  dplyr::mutate(am = as.factor(am))

gt::gt(df, groupname_col = "am")

That is, I'd like to be able to use a column of my dataset to create groups in the table.

lorenzoFabbri commented 3 months ago

@vincentarelbundock I received a notification for a reply from you, but I cannot seem to see it here!

I tried to use group_tt, but how can I add the spanners without specifying the indices?

vincentarelbundock commented 3 months ago

Yes, I deleted the answer because I had not understood the question.

You can do this now, but not automatically without specifying the indices. It would be trivial to implement, but I'm not sure what makes sense for the user interface.

lorenzoFabbri commented 3 months ago

For completeness:

df <- tibble::as_tibble(mtcars) |>
  dplyr::mutate(am = as.factor(am))

splits <- split(seq_along(df$am), df$am)
splits <- lapply(splits, function(x) {x[1]})

tinytable::tt(df) |>
  tinytable::group_tt(i = splits)

Maybe there's a cleaner version but this should work for simple cases.

vincentarelbundock commented 3 months ago

Thanks, this looks good.

Your code does raise a question, though: The implementation is so trivial that I'm not sure I should increase code complexity. Perhaps an example in the documentation would be sufficient.

lorenzoFabbri commented 3 months ago

I completely agree. I thought it was more difficult to implement. Feel free to close the issue!

vincentarelbundock commented 3 months ago

Cool. I added a base R version of your example to the docs. This is a nice addition, thanks!

https://vincentarelbundock.github.io/tinytable/vignettes/group.html#automatic-row-groups