2

The modelsummary package allows to export summary tables of models do word documents. However, I want to use export multiple tables on different pages. However, when I repeat modelsummary with my new models and choose the same output, the tables just get overwritten. I am wondering, if it is possible?

modelsummary(models1, 
             output = "table.docx")

modelsummary(models2, 
             output = "table.docx")
flxflks
  • 498
  • 2
  • 13

1 Answers1

3

modelsummary can combine a list of models into one table.

To add a separate table for every model summary, you can use package {officer} like this:

library(modelsummary)
library(officer)
  • use a named model list (the names will be used for temporary file names later on):
the_models <- list(
  m1 = lm(Sepal.Length ~ Species, iris),
  m2 = lm(Sepal.Length ~ Sepal.Width + Species, iris)
)
  • create an empty doc representation:
all_tables_doc <- read_docx()
  • save one (temporary) docx file per model summary, and add these files to the main document's representation (here: all_tables_doc):
names(the_models) |>
  Map(f = \(name){
    file_name <- file.path(tempdir(), paste0(name,'.docx'))
    modelsummary(the_models[[name]], file_name)
    all_tables_doc <- all_tables_doc |>
      body_add_docx(file_name)
    name
  }) 

(In above pipe, you can add further elements like page preaks or captions per table.)

  • save result as docx:
print(all_tables_doc, 'all_tables.docx')
I_O
  • 4,983
  • 2
  • 2
  • 15