Regression Tables in R Markdown / rmarkdown (html/pdf)

后端 未结 2 1425
星月不相逢
星月不相逢 2021-01-19 12:00

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

2条回答
  •  情歌与酒
    2021-01-19 12:32

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

提交回复
热议问题