0

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

tci
  • 69
  • 7
  • It would be helpful if you supplied your code and note where it fails. – SteveM Sep 21 '22 at 13:55
  • based on the rdocumentation of ```modelsummary()``` The ```output``` argument only states what kind of output it will be. This also what the error is telling you: if you want excel your argument should be ```output = "xlsx" ``` – Omniswitcher Sep 21 '22 at 14:25
  • Thank you for the reply, @Omniswitcher. The argument `output = "xlsx"` produces the same error. – tci Sep 21 '22 at 14:34
  • How about `x <- modelsummary(models, stars = TRUE, output = "gt")` and then `openxlsx::write.xlsx(x, "foo.xlsx")`? – bretauv Sep 21 '22 at 15:04

1 Answers1

1

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")
Vincent
  • 15,809
  • 7
  • 37
  • 39
  • If you want formatting you can also output to huxtable and then use `huxtable::as_Workbook()`, which again uses openxlsx on the backend. – dash2 Oct 21 '22 at 18:23