Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: Error in argument 'x='. Expecting object of class 'gtsummary': Can't save the table with gtsummary

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading