R Markdown/rmarkdown(html/pdf)中的回归表 [英] Regression Tables in R Markdown / rmarkdown (html/pdf)

查看:342
本文介绍了R Markdown/rmarkdown(html/pdf)中的回归表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于发布的目的,我经常需要PDF和HTML版本的我的作品(包括回归表),并且我想使用R Markdown.对于PDF,stargazertexreg软件包产生了精美的表.现在,尝试生成同样吸引人的HTML输出时,我面临着不同的问题.

For the purpose of publishing I often need both a PDF and a HTML version of my work including regression tables and I want to use R Markdown. For PDF the stargazer and the texreg packages produce wonderful tables. Now trying to generate an equally attractive HTML output I'm facing different issues.

  1. HTML输出的两种方法在注释中均缺少显着性标记.由于它们是自动生成的,所以我不知道如何逃避它们. (我认为这可能是一个小问题,因此我不想将其分解为单独的问题.) 注意:子问题已得到回答

  1. Both methods for HTML output are lacking significance stars in the notes. As they are automatically generated I don't know how to escape them. (I think this might be a minor problem and therefore I didn't want to split it into seperate questions.) Note: Sub-question has been answered here.

在创建确定的输出之前,我经常必须更改数据或进行一些格式化.我总是总是手动将type='html'type='pdf'之间的选项翻转很烦人.我想知道是否可能有一种更可行的方法来组合html/pdf输出,例如texreg/stargazer中的大小写转换输出是否整齐?

Before creating the definite output I often have to change my data or do some formatting. I find it quite annoying to always flip-flop the options between type='html' to type='pdf'manually. I wonder if there might be a more feasible way to combine the html/pdf output , e.g. a case-to-case switch in texreg / stargazer with a tidy output?

我尝试了有前途的 pander-解决方案,但自2014年以来似乎不再起作用.此外,另一个示例似乎只引用普通表.

I tried the promising pander-solution, but it seems not to be working anymore since 2014. Also pixiedust ist not very satisfying, it's becoming somewhat manual at the end and not exactly what I want. An other example seems to refer only to normal tables.

非常感谢您的帮助!

以下是我尝试使用HTML和PDF进行knitr的摘要:

Here is a summary of my attempts for knitr in HTML and PDF:

```{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)
```

推荐答案

这是一个命题:建立一个检查输出格式的函数,然后根据此函数使用stargazer或texreg.我们使用opts_knit$get("rmarkdown.pandoc.to")来检查输出格式.

Here is a proposition: make a function that checks the output format and then uses either stargazer or texreg depending on this. We use opts_knit$get("rmarkdown.pandoc.to") to check the output format.

---
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)
```

这篇关于R Markdown/rmarkdown(html/pdf)中的回归表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆