[英]Include apsrtable (or stargazer) output in an Rmd file
I tried to include the summary of an lm
object in an Rmd file, using code like the following but it didn't work. 我试图在Rmd文件中包含lm
对象的摘要,使用如下代码,但它不起作用。 Could you help me do that? 你能帮帮我吗?
```{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")
```
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. 据我所知, apsrtable
只适用于LaTeX,但是LaTeX和Markdown非常不同 - 你很少有希望可以完全使用Markdown重做LaTeX。 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发明了$$
语法,因为它得到了MathJax的良好支持,并且还注意到有许多基于原始Markdown的变体/风格。
At the moment you may consider: 目前您可以考虑:
xtable
or ascii
or R2HTML
package to generate HTML tables 使用xtable
或ascii
或R2HTML
包生成HTML表 apsrtable
to support HTML tables 请求apsrtable
的包作者支持HTML表 What about including my_model
in Markdown format with `pander˙ : 怎么样,包括my_model
在Markdown格式与`潘德 :
> library(pander)
> pander(my_model)
--------------------------------------------------------------
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)
| | 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
Cross-posting my answer to Table of multiple lm() models using apsrtable in Rmarkdown : 在Rmarkdown中使用apsrtable交叉发布我对多个lm()模型表的 回答 :
It can be done in a pdf_document
with apsrtable and also stargazer, which additionally supports HTML. 它可以在带有apsrtable和stargazer的pdf_document
中完成,它还支持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")
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.