tci
tci

Reputation: 81

saving modelsummary() output to Excel

I have a list of models. The modelsummary documentation mentions being able to save to Excel format (https://cran.r-project.org/web/packages/modelsummary/modelsummary.pdf). I can't get it to work. If Excel isn't possible, is there another 'spreadsheet'-type format? I like to be able to compactly annotate my output within different rows and columns and make notes. Docx output is way too clunky for that. I realize I can output as .html and then open and save it as Excel ... but that's a lot of clicking ... I'm running a lot of models.

An example:

library(modelsummary)
models = list(
  "mpg" = lm(mpg ~ vs, data = mtcars),
  "am" = glm(am ~ vs, family = binomial, data = mtcars),
  "wt" = lm(wt ~ vs, data = mtcars)
    )

modelsummary(models, stars = TRUE, output = "models.xlsx")

An error is then thrown:

Error in sanitize_output(output) : The output argument must be default, gt, kableExtra, flextable, huxtable, html, jupyter, latex, latex_tabular, markdown, dataframe, data.frame, modelsummary_list, or a valid file path with one of these extensions: html, tex, md, txt, docx, pptx, rtf, jpg, png

Upvotes: 0

Views: 1136

Answers (1)

Vincent
Vincent

Reputation: 17715

You can output to data.frame and then write the file to CSV (using write.csv) or XLSX files (using the openxls package). Excel will be able to open files in either format:

library(modelsummary)
library(openxlsx)

models = list(
  "mpg" = lm(mpg ~ vs, data = mtcars),
  "am" = glm(am ~ vs, family = binomial, data = mtcars),
  "wt" = lm(wt ~ vs, data = mtcars))

tab <- modelsummary(models, stars = TRUE, output = "data.frame")

write.csv(tab, "models.csv")

write.xlsx(tab, "models.xlsx")

Edit:

With the development version of modelsummary, this is now possible:

modelsummary(models, output = "models.csv")
modelsummary(models, output = "models.xlsx")

Upvotes: 1

Related Questions