Output several regression tables into multiple pages of a Word document in R
Asked Answered
C

1

4

My goal is to create a multi-page Microsoft Word document with many formatted regression table outputs on consecutive pages. Ideally this would be done using R Markdown.

I have had some luck making nicely formatted regression tables in word using sjPlot::tab_model

So for example, I can do

mod2 <- lm(Petal.Length ~ Species + Petal.Width, data=iris) 
sjPlot::tab_model(mod2, file = "~/Desktop/model2.doc")

to create a nice-looking regression table in a new Word document.

If I do

mod3 <- lm(Petal.Length ~ Species + Petal.Width, data=iris) 
sjPlot::tab_model(mod2, file = "~/Desktop/model3.doc")

I can make a second Word document with another nicely formatted regression table.

The goal here is to create a single Word document containing both model outputs on consecutive pages.

I know that in R Markdown, I can make multi-page regression tables using Stargazer and Knit, e.g.,

stargazer(mod2)

But these only render in PDF or HTML.

I know that sjPlot doesn't directly knit to Word, so that's not an option.

Possible solutions include:

  1. Using a different package to render Word-compatible regression output tables in Markdown
  2. Finding a clever way to combine the tab_model outputs into a single Word document.
  3. Some other way I'm not thinking of.
Cyclorama answered 8/8, 2019 at 4:8 Comment(3)
Have you tried Flextable? community.rstudio.com/t/nice-tables-when-knitting-to-word/3840/…Statistical
datascienceplus.com/…Methodism
+1 on flextable. My workflow is to use huxreg from the huxtable package and convert to flextable with huxtable::as_flextable().Crossover
C
3

I figured it out. The best way is to stick with sjPlot, knit to HTML, then copy and paste from HTML to Word. The table renders nicely that way.

Thanks to Andrew Reece (andrew_reece) for this one.

Cyclorama answered 8/8, 2019 at 14:35 Comment(2)
I found that copy and pasting from HTML to word results in the columns not lining up correctly and the bars at the top of the table not being long enough etc. Did you find a way to preserve the formating?Thegn
Is there a neat way of automating this?Trochaic

© 2022 - 2024 — McMap. All rights reserved.