在Rmd文件中包含apsrtable(或stargazer)输出

时间:2021-09-04 06:08:52

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?

我试图在Rmd文件中包含lm对象的摘要,使用如下代码,但它不起作用。你能帮我吗?

```{r summary_lm, results='asis', echo=FALSE, comment=NA}

library(apsrtable)
my_model <- lm(y ~ x, data = data.frame(y = rnorm(10), x = 1:10))
res <- apsrtable(my_model) # my_model is a linear regression model (lm)

cat("$$latex \n",res,"\n$$ \n")

```

2 个解决方案

#1


6  

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.

$语法只适用于数学表达式,您试图在其中放一个表,但它不能工作。据我所知,适用于乳胶,但乳胶和Markdown是非常不同的——用Markdown完全重做乳胶几乎没有希望。我认为人们发明了Markdown $语法,是因为MathJax很支持它,而且还注意到有许多基于原始Markdown的变体/变体。

At the moment you may consider:

此时你可以考虑:

  • use the xtable or ascii or R2HTML package to generate HTML tables
  • 使用xtable或ascii或R2HTML包生成HTML表
  • request the package author of apsrtable to support HTML tables
  • 请求apsrtable的包作者来支持HTML表

#2


3  

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

包括my_model减价格式怎么样的老鸨˙:

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

或PHP MarkdownExtra/rmarkdown格式:

> 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

#1


6  

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.

$语法只适用于数学表达式,您试图在其中放一个表,但它不能工作。据我所知,适用于乳胶,但乳胶和Markdown是非常不同的——用Markdown完全重做乳胶几乎没有希望。我认为人们发明了Markdown $语法,是因为MathJax很支持它,而且还注意到有许多基于原始Markdown的变体/变体。

At the moment you may consider:

此时你可以考虑:

  • use the xtable or ascii or R2HTML package to generate HTML tables
  • 使用xtable或ascii或R2HTML包生成HTML表
  • request the package author of apsrtable to support HTML tables
  • 请求apsrtable的包作者来支持HTML表

#2


3  

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

包括my_model减价格式怎么样的老鸨˙:

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

或PHP MarkdownExtra/rmarkdown格式:

> 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