Regression Tables in R Markdown / rmarkdown (html/pdf)
Asked Answered
P

2

5

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

  2. 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?

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.

Any help is extremely appreciated, thanks!

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)
```
Prebend answered 9/6, 2017 at 11:59 Comment(1)
Does this help: https://mcmap.net/q/378864/-in-knitr-how-can-i-test-for-if-the-output-will-be-pdf-or-word ; you could set type in stargazer depending on the output formatTuberosity
T
4

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)
```
Tuberosity answered 12/6, 2017 at 9:56 Comment(2)
Awsome, thx!! I'm pretty excited that customization even works using the function in chunks: lm1 <- lm(prestige ~ income + education, data=Duncan); lm2 <- lm(prestige ~ income + education + income:education, data=Duncan); report_regression(list(lm1, lm2), format = rmd_format, custom.model.names = c("West", "East"), custom.coef.names = c("(C. 1)", "I. 1", "E. 1", "Inter. 1"), custom.note = "%stars. foo", star.symbol = "&#42;") I suggest to change lm1 to model in function and rather use texreg than stargazer 'cause it's more sophisticated. stargazer still best choice for the console.Prebend
rmd_format <- opts_knit$get("rmarkdown.pandoc.to") was just what the doctor ordered! You can pass type = rmd_format directly to stargazer() without the need for a function, as far as I could tell in my limited experience.Crake
N
3

As pointed out in an answer to a related question, knitr 1.18 introduced the following functions

knitr::is_html_output()
knitr::is_latex_output()

to check if the output is HTML or LaTeX. Adapting @scoa's excellent answer:

---
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)
```
Northwesterly answered 7/2, 2019 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.