Include apsrtable (or stargazer) output in an Rmd file

后端 未结 3 807
北海茫月
北海茫月 2021-01-13 00:24

I tried to include the summary of an lm object in an Rmd file, using code like the following but it didn\'t work. Could you help me do that?

```         


        
相关标签:
3条回答
  • 2021-01-13 00:46

    The $$ syntax only applies to math expressions, and you were trying to put a table in it, which will not work. The apsrtable, as far as I understand, is for LaTeX only, but LaTeX and Markdown are very different -- there is little hope you can redo LaTeX entirely with Markdown. I think people invented the $$ syntax for Markdown due to the fact that it is well supported by MathJax, and also note there are many variants/flavors based on the original Markdown.

    At the moment you may consider:

    • use the xtable or ascii or R2HTML package to generate HTML tables
    • request the package author of apsrtable to support HTML tables
    0 讨论(0)
  • 2021-01-13 00:52

    Cross-posting my answer to Table of multiple lm() models using apsrtable in Rmarkdown:

    It can be done in a pdf_document with apsrtable and also stargazer, which additionally supports HTML.

    ---
    title: "stargazer"
    author: "hplieninger"
    date: "3 August 2018"
    output: pdf_document
    header-includes:
        - \usepackage{dcolumn}
    ---
    
    ```{r}
    m1 <- lm(Fertility ~ Education , data = swiss)
    m2 <- lm(Fertility ~ Education + Agriculture, data = swiss)
    m3 <- lm(Fertility ~ . , data = swiss)
    ```
    
    ```{r, results='asis'}
    apsrtable::apsrtable(m1, m2, m3, Sweave = TRUE)
    ```
    
    ```{r, results='asis'}
    # If output: pdf_document
    stargazer::stargazer(m1, m2, m3)
    # If output: html_document
    # stargazer::stargazer(m1, m2, m3, type = "html")
    ```
    
    0 讨论(0)
  • 2021-01-13 01:00

    What about including my_model in Markdown format with `pander˙:

    > library(pander)
    > pander(my_model)
    
    --------------------------------------------------------------
         &nbsp;        Estimate   Std. Error   t value   Pr(>|t|) 
    ----------------- ---------- ------------ --------- ----------
          **x**         0.1174      0.1573     0.7465     0.4767  
    
     **(Intercept)**   -0.2889      0.9759     -0.296     0.7748  
    --------------------------------------------------------------
    
    Table: Fitting linear model: y ~ x
    

    Or in PHP MarkdownExtra/rmarkdown format:

    > panderOptions('table.style', 'rmarkdown')
    > pander(my_model)
    
    
    |      &nbsp;       |  Estimate  |  Std. Error  |  t value  |  Pr(>|t|)  |
    |:-----------------:|:----------:|:------------:|:---------:|:----------:|
    |       **x**       |   0.1174   |    0.1573    |  0.7465   |   0.4767   |
    |  **(Intercept)**  |  -0.2889   |    0.9759    |  -0.296   |   0.7748   |
    
    Table: Fitting linear model: y ~ x
    
    0 讨论(0)
提交回复
热议问题