rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
1.99k stars 200 forks source link

Feature request: Support packed data frame (data frame with data frame columns) #314

Open atusy opened 5 years ago

atusy commented 5 years ago

Upcoming tidyr has pack and unpack to treat data frame columns. Supporting packed data can reduce calling tab_spanner and will improve experience in gt. Here's my prototype implementation.

library(dplyr, warn.conflict = FALSE)
library(tidyr)
library(gt)

x <- data.frame(
  w = c("a", "b", "b"), x = c("A", "B", "B"),
  y1 = 1:3, y2 = 1:3, z1 = 1:3, z2 = 1:3
) %>%
  pack(y = starts_with("y"), z = starts_with("z")) %>%
  group_by(w)

f <- function(x) {
  df_col <- names(x)[vapply(x, is.data.frame, TRUE)]

  .gt <- gt(unpack(x, !!df_col))

  for(i in df_col) {
    .gt <- tab_spanner(.gt, label = i, names(x[[i]]))
  }

  .gt
}
f(x)

Created on 2019-07-07 by the reprex package (v0.3.0)

image

rich-iannone commented 5 years ago

I really like this! Thanks so much for suggesting this improvement (and providing an example implementation). I think this is something we could incorporate soon.

rich-iannone commented 3 months ago

Going to close this as pack() is probably not in common use these days.

hhhh5 commented 3 months ago

Just to give one use case for pack(). When dealing with several metrics that each come with a mean/sample size/confidence interval, packed columns for "Sensitivity" and "Specificity", for example, would itself be tibbles with columns "Estimate", "N", "CI". Packed columns help me to avoid name collisions and make it more convenient to move these grouped data around. And for display, I'm converting each of these to a tab_spanner.

rich-iannone commented 3 months ago

Thanks @hhhh5 for presenting your use of it! I wasn’t sure at all whether that approach had much uptake. I’ll explore this a little and see what could be done in gt to automatically unpack and create spanners.