How to convert gtsummary object to gt table and/or to tibble table?

79 Views Asked by At

I have got tbl_regression output table which I want to convert to gt() table and then to tibble object if this is possible.
Or directly tbl_regression table into tibble. Is it doable ?


library(gtsummary)
library(tidyverse)
library(gt)

set.seed(1000)
my_data <- rbind(
  data.frame(time = "Pre", treatment = "Control", response = rnorm(100, mean=1)),
  data.frame(time = "Pre", treatment = "Treatment", response = rnorm(100, mean=2)),
  data.frame(time = "Post", treatment = "Control", response = rnorm(100, mean=1)),
  data.frame(time = "Post", treatment = "Treatment", response = rnorm(100, mean=2))
) %>% mutate(time = factor(time, levels = c("Pre", "Post"))) %>%
mutate(treatment = factor(treatment, levels = c("Control", "Treatment")))

model3 <- lm(response ~ time * treatment, data = my_data)


gtsummary::tbl_regression(model3,
  pvalue_fun = ~ style_pvalue(.x, digits = 2),
  estimate_fun = ~ style_number(.x, digits = 4)
) %>%
  add_global_p() %>%
  bold_p(t = 0.10) %>%
  bold_labels() %>%
  italicize_levels() -> gts

I have tried as follows:

gts %>% gtsummary::as_gt() %>% as.data.frame() # not working

# This works a bit, but the output is chaotic:
gts %>%  as_tibble()

gts %>% broom::tidy() %>% as_tibble() # not working

There is also a function: as.data.frame.gt_tbl()
https://rdrr.io/cran/gt/man/as.data.frame.gt_tbl.html , but I am not that expert to run it properly.
Any suggestions would be much appreciated, how to do it ?

0

There are 0 best solutions below