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.)
print(all_tables_doc, 'all_tables.docx')