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