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

Reporting regression tables using rmarkdown in Word format

尝试使用 rmarkdown 以 Word 格式报告回归 tables 似乎是不可能的。在尝试了几个小时和 here 之类的几个选项之后,没有人在我的案例中起作用。我想使用 markdown 报告 lm 个模型并渲染到 .doc 文件。

方法一:

使用 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.

方法二:

创建一个 html 对象并包含使用 includeHTML。到目前为止,这是我想要的输出的封闭方法。但是 table 只包含这样一列:

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

上面的代码在 word 文档中产生了这个:

方法三:

使用 xtable 也会产生与上面相同的输出。

有什么建议吗?

使用write_html(来自memisc包):

write_html(mt, "mt.html")

现在在 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()

这是一种存储回归输出的简单方法:

sink("myresult.doc", append=T)

只要指定,所有输出都会自动附加:append=T

HTH