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