简体   繁体   English

如何从Knitr / R RStudio将回归结果表输出到Word? (不稳定,混杂)

[英]How Do I Output Regression Result Tables to Word from Knitr / R RStudio? (mtable, memisc)

I would like to output regression result tables to Word from knitr but I'm having trouble. 我想从knitr将回归结果表输出到Word,但是遇到了麻烦。 Outputting mtable s to \\LaTeX works if I tweak the options but I'm stuck when it comes to Word. 如果我调整选项,则将mtable s输出到\\ LaTeX可以正常工作,但是在Word方面我陷入了困境。 My MWE is below. 我的MWE在下面。

---
output:
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(memisc)
```

## This section contains ugly text, not a table
```{r eval = T, include = T, echo = F}
lm0 <- lm(sr ~ pop15 + pop75,              data = LifeCycleSavings)
lm1 <- lm(sr ~                 dpi + ddpi, data = LifeCycleSavings)
lm2 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)

mt01 <- mtable(lm0,lm1,summary.stats=c("R-squared","N"))
mt12 <- mtable(lm1,lm2,summary.stats=c("R-squared","F","N"))

c("Group 1"=mt01,
"Group 2"=mt12)
```

I've devised an ugly hack that works. 我设计了一个有效的丑陋hack。 I hope it can help someone else in my position. 我希望它可以帮助其他人担任我的职务。 I am fairly certain that an obvious and simpler way to achieve this exists. 我相当确定存在一个明显且更简单的方法来实现这一目标。

---
output:
  word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(memisc)
```

## This section contains a table, not ugly text
```{r eval = T, include = T, echo = F}
lm0 <- lm(sr ~ pop15 + pop75,              data = LifeCycleSavings)
lm1 <- lm(sr ~                 dpi + ddpi, data = LifeCycleSavings)
lm2 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)

mt01 <- mtable(lm0,lm1,summary.stats=c("R-squared","N"))
mt12 <- mtable(lm1,lm2,summary.stats=c("R-squared","F","N"))

x = c("Group 1"=mt01,"Group 2"=mt12)

x = memisc::mtable_format_delim(x)
writeLines(x,"table.csv")
x = read.delim("table.csv", header = F, stringsAsFactors = F)
colnames(x) = x[1,]
x = x[-1,]
rownames(x) = NULL
knitr::kable(x)
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM