Gt: arsenal::tableby() objects?

Created on 11 Jan 2019  ยท  4Comments  ยท  Source: rstudio/gt

It is common to use arsenal::tableby() to produce table 1 in reports of clinical trials.
Can these objects be handled/formatted with gt?

library(arsenal)
tab1 <- tableby(arm ~ sex + age, data=mockstudy)
โ‡ Question

Most helpful comment

Hi eheinzen - great to see you are watching gt. I am a big fan of arsenal::tableby for clinical studies. Still hoping to eventually see a pipeline for gt objects via .Rmd to Word and PPT

All 4 comments

@higgi13425 I just experimented with this a bit. The end result is not too bad but I think that a function to do some handling for tableby object would make this better. Still, the very basic statements below create a reasonable output table:

library(arsenal)
library(tidyverse)
library(gt)

tableby(arm ~ sex + age, data = mockstudy) %>%
  as.data.frame() %>%
  gt(
    groupname_col = "variable",
    rowname_col = "term"
  )

tableby

This could be much better with some systematic formatting. I'd like to look at more of these table 1 reports to see what can be done to improve to layout and formatting.

Very cool. It appears that the formatting in tableby is preserved when piped into gt. Or is the striping from gt defaults? Would be curious to know if you can format digits, etc. in tableby and have the results pipe smoothly into gt.

Author/maintainer of arsenal here. I'd consider a hack using as.data.frame(summary(.)) instead (see, e.g., eheinzen/arsenal#60). Something like this should do the trick:

````
library(arsenal)
library(tidyverse)
library(gt)

tab <- tableby(arm ~ sex + age, data = mockstudy) %>%
summary(term.name = "term", text = NULL)

format_for_gt <- function(df)
{
df$variable <- df$label[1]
df[-1, ]
}

tab$object[[1]] <- tab$object[[1]] %>%
group_by(variable) %>%
do(format_for_gt(.)) %>%
ungroup()

tab %>%
as.data.frame() %>%
mutate(variable = tab$object[[1]]$variable) %>%
gt(
rowname_col = "term",
groupname_col = "variable"
)
````

This should give you something like this:
image

As a side note, we're definitely aware of gt (I found this issue by complete accident), and I was super excited to see it at rstudio::conf, too. I'd very seriously consider using it to format our tables once it's on CRAN.

cc @bethatkinson @sinnweja

Hi eheinzen - great to see you are watching gt. I am a big fan of arsenal::tableby for clinical studies. Still hoping to eventually see a pipeline for gt objects via .Rmd to Word and PPT

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rich-iannone picture rich-iannone  ยท  5Comments

vincentarelbundock picture vincentarelbundock  ยท  4Comments

jthomasmock picture jthomasmock  ยท  3Comments

higgi13425 picture higgi13425  ยท  3Comments

rich-iannone picture rich-iannone  ยท  4Comments