2

Context

I am making a table and saving it into Microsoft Word with .docx file.

the table has a variable named PM2.5. I want to subscript 2.5 like PM2.5.

In this answear, I can use the grammar 'PM~2.5~' with as_kable() to use subscrpit in PM2.5.

But when I save the result (tab), it is a blank .docx file.

Question

How can I use subscripts in gtsummary and save it into .docx file?

Reproducible code

library(dplyr)
library(gtsummary)
        
df = data.frame(PM2.5 = 1)

tab = # make a table using gtsummary
  df %>% 
  tbl_summary(label = PM2.5 ~ 'PM~2.5~') %>% # subscript in main table
  modify_table_styling(columns = label,
                       rows = label == 'PM~2.5~',
                       footnote = 'PM~2.5~ in footnote') %>%  # subscript in footnote
  as_kable()

tab %>% flextable::save_as_docx(path = 'test.docx') # a blank .docx file
zhiwei li
  • 1,635
  • 8
  • 26

1 Answers1

1

The reason it is blank is because you're using flextable::save_as_docx() to save the table. That function will only work with flextables...not knitr::kable() tables.

You can put this table in an R markdown or Quarto document with output type Word, and the table will appear.

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28