Home > Software engineering >  saving modelsummary() output to Excel
saving modelsummary() output to Excel

Time:09-22

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

CodePudding user response:

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")

I opened an issue on Github with a feature request to make it easier to save directly to Excel using the output argument. You can follow progress here: https://github.com/vincentarelbundock/modelsummary/issues/554

  • Related