Trying to report regression tables in Word format using rmarkdown seems impossible. After trying hours and several options like here no one worked in my case. I want to report lm
models using markdown and render to a .doc file.
Using memisc
package to create a mtable
object and render using pander
:
> lm0 <- lm(hp ~ wt, mtcars)
> lm1 <- lm(qsec ~ hp, mtcars)
> lm2 <- lm(qsec ~ wt, mtcars)
>
> library(memisc)
>
> mt <- mtable(lm0, lm1, lm2)
>
> pander::pander(mt)
Error in x[[i]] : subíndice fuera de los límites
Además: Warning message:
In pander.default(mt) :
No pander.method for "memisc_mtable", reverting to default.
Create a html object and include using includeHTML
. So far this is the closed method to my desire output. However the table only contains one column like this:
```{r}
stargazer::stargazer(lm0, lm1, lm2, type = "html", title = "Results", out = "./pp.html")
shiny::includeHTML("pp.html")
```
Code above produces this in a word document:
Using xtable
also produces the same output above.
Any suggestions?
Still, in presenting the results for any multiple regression equation, it should always be clear from the table: (1) what the dependent variable is; (2) what the independent variables are; (3) the values of the partial slope coefficients (either unstandardized, standardized, or both); and (4) the details of any test of ...
Under Options in Tools, choose the options for R Markdown, change the tick for "Show output inline...." to "untick". Good luck!
Use write_html
(from the memisc package):
write_html(mt, "mt.html")
Now open the mt.html file in Word. This is what it looks like in Word. (continued after screenshot)
Alternately use this to convert filePathIn (which is the path to the html file created) to filePathOut (which is the path to the docx file to be created from it). This works on Windows and even in cases where pandoc does not as it uses Word itself to do the translation from html to docx. (If you want a doc file rather than docx then replace "docx" with "doc" in the filePathOut definition and replace 16 with 0 in the SaveAs line. See wdSaveFormat Enumeration.)
library(RDCOMClient)
filePathIn <- file.path(getwd(), "mt.html")
filePathOut <- sub("html$", "docx", filePathIn)
w <- COMCreate("Word.Application")
doc <- w[["Documents"]]
od <- doc$Open(filePathIn)
od$SaveAs(filePathOut, 16)
w$Quit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With