themichjam
themichjam

Reputation: 15

Using gt functions to style gtsummary merged tables

I've been utilising the as_gt() function to format my gtsummary tables and this works fine on each individual table. But when trying to merge tables (tbl_merge) together this error appears:

Error: Error in argument 'x='. Expecting object of class 'gtsummary'

This happens regardless of feeding the gt format options to each individual table, or only to the merged object, same error. Any ideas on workarounds? Repex below.

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

# Works fine for individual tbls
t1 <-
  glm(response ~ trt + grade + age, trial, family = binomial) %>%
  tbl_regression(exponentiate = TRUE) %>%
  as_gt() %>%
      gt::tab_options(table.font.names = "Times New Roman")
t1

t2 <-
  coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
  tbl_regression(exponentiate = TRUE)

# Error appears
tbl_merge_ex1 <-
  tbl_merge(
    tbls = list(t1, t2),
    tab_spanner = c("**Tumor Response**", "**Time to Death**") %>%
      as_gt() %>%
      gt::tab_options(table.font.names = "Times New Roman")
  )

Error: Error in argument 'x='. Expecting object of class 'gtsummary'

Upvotes: 1

Views: 438

Answers (1)

Mike
Mike

Reputation: 4370

you need to remove all as_gt() until the very end after the merge like so:

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



t1 <-
  glm(response ~ trt + grade + age, trial, family = binomial) %>%
  tbl_regression(exponentiate = TRUE) 

t2 <-
  coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
  tbl_regression(exponentiate = TRUE)


tbl_merge_ex1 <-
  tbl_merge(
    tbls = list(t1, t2),
    tab_spanner = c("**Tumor Response**", "**Time to Death**") 
  ) %>%
  as_gt() %>%
  gt::tab_options(table.font.names = "Times New Roman")

Upvotes: 3

Related Questions