Search code examples
rofficer

Best practice to add tables for model objects to officer/ReporteRs


flextable::body_add_flextable enables inserting flextable to Word document created by officer. The examples included in the packages are for creating tables from data frames. My question is, what the best practice of creating tables from model objects (e.g. from lm)? It is possible to convert model objects to data frames with, for example, broom::tidy, but it supports limited types of models and it is messy to get it to do side-by-side models comparison, while packages like texreg and stargazer already have great support for this. Is there an efficient way to convert texreg::htmlreg (or texreg::screereg) outputs to a flextable and insert it into Word document? Or more generally, is there a way to insert html contents into a Word document via officer?

(I know it is possible to create/customize my own flextables with whatever information I want however I want it, but it seems a huge wasted effort to repeat the work packages like texreg have already done well.) Thanks!

For demo purpose, this is what I'm trying to get into a Word document:

library(texreg)
fit_m1 <- lm(mpg ~ wt, data=mtcars)
fit_m2 <- lm(mpg ~ wt + cyl, data=mtcars)
screenreg(list(fit_m1, fit_m2))
htmlreg(list(fit_m1, fit_m2))

Solution

  • It is kind of a workaround, but as far as I know the package huxtable has a function huxreg(). You can convert the huxtable with as_flextalbe() or just use it within FlexTable(). Or use pixiedust you can output dataframes and these can be used in vanilla.table(), FlexTable().

    I hope that helps.