使用Word格式的rmarkdown报告回归表

Tit*_*anz 6 r knitr r-markdown

尝试使用rmarkdown以Word格式报告回归表似乎是不可能的.经过几个小时和几个选项之后,没有人在我的情况下工作.我想lm使用markdown 报告模型并渲染到.doc文件.

方法1:

使用memisc包创建mtable对象并使用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.
Run Code Online (Sandbox Code Playgroud)

方法2:

创建一个html对象并包含使用includeHTML.到目前为止,这是我渴望输出的封闭方法.但是,该表只包含一个这样的列:

```{r}
stargazer::stargazer(lm0, lm1, lm2, type = "html", title = "Results", out = "./pp.html")
shiny::includeHTML("pp.html")
```
Run Code Online (Sandbox Code Playgroud)

上面的代码在word文档中生成: 在此输入图像描述

方法3:

使用xtable也会产生相同的输出.

有什么建议?

G. *_*eck 4

使用write_html(来自 memisc 包):

write_html(mt, "mt.html")
Run Code Online (Sandbox Code Playgroud)

现在在 Word 中打开 mt.html 文件。这就是 Word 中的样子。(截图后继续)

截屏

或者使用它将 filePathIn (这是创建的 html 文件的路径)转换为 filePathOut (这是要从中创建的 docx 文件的路径)。这适用于 Windows,甚至在 pandoc 不适用的情况下也是如此,因为它使用 Word 本身来进行从 html 到 docx 的翻译。(如果您想要 doc 文件而不是 docx,请在 filePathOut 定义中将“docx”替换为“doc”,并在 SaveAs 行中将 16 替换为 0。请参阅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()
Run Code Online (Sandbox Code Playgroud)