Home > Blockchain >  Error: Error in argument 'x='. Expecting object of class 'gtsummary': Can't
Error: Error in argument 'x='. Expecting object of class 'gtsummary': Can't

Time:07-17

I'm working with gtsummary, but I don't know why I can't save the table as Word document. The table is visualized well, although the problem is just with the saving process. This is the code I'm using for the table:

demographic_table<-df %>% 
  dplyr::select(Group, Diagnose,Gender,Age,Handedness,Cpz_equivalent)%>%
  mutate(
    Gender = case_when(Gender == "female" ~ "Female",
                       Gender == "male" ~ "Male",))%>%
  mutate(
    Group = case_when(Group == "control" ~ "Control",
                      Group == "patient" ~ "Patient",))%>%
  mutate(
    Handedness=case_when(Handedness=='right'~'Right',
                         Handedness=='left'~'Left',))%>%
  tbl_summary(
    by = Diagnose,
    statistic = list(all_continuous() ~ "{mean} ({sd})",
                     all_categorical() ~ "{n} ({p}%)",
                     all_dichotomous()~"{n}"),
    digits = all_continuous() ~ 2,
    type = all_dichotomous()~"categorical",
    label = Cpz_equivalent ~ "Clozapine dose",
    missing = 'no'
    )%>%
  add_p()%>%
  modify_spanning_header(c("stat_1", "stat_2", 'stat_3') ~ "**Diagnose**") %>%
  add_overall()%>%
  bold_labels()%>%
  as_gt()%>%
  gt::tab_options(table.font.names = "Times New Roman")

When I try to save it:

demographic_table %>%
  as_flex_table() %>%
  flextable::save_as_docx()

I'm getting this error: 'Error: Error in argument 'x='. Expecting object of class 'gtsummary'

CodePudding user response:

A gtsummary table can be converted to many types: gt, flextable, huxtable, kable, kableExtra, tibble, etc.

In your first chunk of code, you convert the gtsummary object to gt with as_gt(). After you convert to gt, you no longer have a gtsummary table (you have a gt table). Since you no longer have gtsummary table, you cannot use a gtsummary function to convert your gt table to flextable. Hope that makes sense!

Instead of converting it to gt, convert to flextable and use flextable functions to change the font. Then export the table.

  • Related