对于发布的目的,我经常需要同时获得PDF和我的工作包括回归表的HTML版本,我想用[R降价。对于PDF的stargazer
和texreg
包产生奇妙的表。现在试图生成一个同样有吸引力的HTML输出我面对不同的问题。
type='html'
的选项type='pdf'
manually。我不知道是否有可能是将HTML / PDF输出相结合,例如一个更可行的方法的情况下,到外壳开关在texreg
/ stargazer
具有整齐输出?我试过看好pander
-solution,但它似乎并没有被工作了,因为2014年还有pixiedust
北京时间不是非常令人满意,这已经成为在年底有所手动和不正是我想要的。 An other example似乎仅指普通表。
任何帮助极为赏识,谢谢!
这是我尝试在HTML和PDF knitr
的摘要:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r table, results = "asis"}
library(car)
lm1 <- lm(prestige ~ income + education, data=Duncan)
## html
# stargazer
library(stargazer)
stargazer(lm1, type="html", notes="stargazer html")
# htmlreg
library(texreg)
htmlreg(lm1, custom.note="%stars. htmlreg")
## pdf/latex
# stargazer
stargazer(lm1, notes="stargazer latex")
# texreg
texreg::texreg(list(lm1), custom.note="%stars. texreg")
# pixiedust
library(pixiedust)
dust(lm1, caption = "pixiedust")
# pander
library(memisc)
library(pander)
lm1_table <- mtable(lm1)
# pander(lm1_table, style="rmarkdown") # not working
pander(lm1)
```
这里是一个命题:使该检查的输出格式,然后使用根据任一本或斯塔盖泽texreg的功能。我们使用opts_knit$get("rmarkdown.pandoc.to")
检查输出格式。
---
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
rmd_format <- opts_knit$get("rmarkdown.pandoc.to")
## returns "html" or "latex"
```
```{r}
report_regression <- function(model, format, ...){
if(format == "html"){
require(texreg)
htmlreg(model, custom.note="%stars. htmlreg", ...)
} else if(format == "latex"){
require(stargazer)
stargazer(model, notes="stargazer html", ...)
} else {
print("This only works with latex and html output")
}
}
```
```{r table, results = "asis"}
library(car)
lm1 <- lm(prestige ~ income + education, data=Duncan)
report_regression(lm1, format = rmd_format)
```
正如指出的in an answer to a related question,knitr
1.18 introduced以下功能
knitr::is_html_output()
knitr::is_latex_output()
检查输出HTML或LaTeX。适应@ SCOA的出色答卷:
---
output: html_document
---
```{r}
report_regression <- function(model, ...){
if(knitr::is_html_output()){
require(texreg)
htmlreg(model, custom.note="%stars. htmlreg", ...)
} else if(knitr::is_latex_output()){
require(stargazer)
stargazer(model, notes="stargazer html", ...)
} else {
print("This only works with latex and html output")
}
}
```
```{r table, results = "asis"}
library(car)
lm1 <- lm(prestige ~ income + education, data=Duncan)
report_regression(lm1)
```