在R中使用stargazer而对乳胶一无所知

时间:2021-03-16 06:13:25

I'm about to teach an R course for social scientists. They are likely to know nothing about LaTeX, and reluctant to hear about it (R is complex enough for them). Yet, they are likely to love the sort of tables that the stargazer package creates to represent their models.

我即将为社会科学家教授R课程。他们很可能对LaTeX一无所知,也不愿意听到它(R对他们来说足够复杂)。然而,他们很可能喜欢观星包装所代表他们的模型所创造的那种桌子。

Is there any wrapper or other simple procedure that will enable them to use stargazer to directly create a pdf of the table (or other format) that they can then insert into their word documents as an image?

是否有任何包装器或其他简单的程序使他们能够使用stargazer直接创建表格(或其他格式)的pdf,然后他们可以作为图像插入到word文档中?

3 个解决方案

#1


2  

R-Studio would be an option. Students could use the compile notebook button to easily create HTML documents. R-Studio also contains HTML format document creation, which required only few clicks and minimal knowledge of any other programming language than R. Here is one way of doing it in R studio:

R-Studio是一个选择。学生可以使用编译笔记本按钮轻松创建HTML文档。 R-Studio还包含HTML格式文档创建,它只需要几次点击,并且对R之外的任何其他编程语言知之甚少。以下是在R studio中执行此操作的一种方法:

If HTML is good enough, this is easy. R code:

如果HTML足够好,这很容易。 R代码:

X = data.frame(a = 1:10, b = 21:30)
mod <- lm(a ~ b, X)
library(stargazer)
stargazer(mod, type = "html")

Next click File -> New File -> R HTML

然后单击文件 - >新文件 - > R HTML

Remove everything except the <htlm> tags. Copy the output from stargazer function:

删除除 标记之外的所有内容。复制stargazer函数的输出:

<html>
<table style="text-align:center"><tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td><em>Dependent variable:</em></td></tr>
<tr><td></td><td colspan="1" style="border-bottom: 1px solid black"></td></tr>
<tr><td style="text-align:left"></td><td>a</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">b</td><td>1.000<sup>***</sup></td></tr>
<tr><td style="text-align:left"></td><td>(0.000)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">Constant</td><td>-20.000<sup>***</sup></td></tr>
<tr><td style="text-align:left"></td><td>(0.000)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Observations</td><td>10</td></tr>
<tr><td style="text-align:left">R<sup>2</sup></td><td>1.000</td></tr>
<tr><td style="text-align:left">Adjusted R<sup>2</sup></td><td>1.000</td></tr>
<tr><td style="text-align:left">Residual Std. Error</td><td>0.000 (df = 8)</td></tr>
<tr><td style="text-align:left">F Statistic</td><td>11,406,627,545,111,658,741,817,889,783,808.000<sup>***</sup> (df = 1; 8)</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Note:</em></td><td style="text-align:right"><sup>*</sup>p<0.1; <sup>**</sup>p<0.05; <sup>***</sup>p<0.01</td></tr>
</table>
</html>

Next click Knit HTML and choose where to save the file. You'll get a HTML file, which looks like this in my internet browser:

然后单击Knit HTML并选择保存文件的位置。您将获得一个HTML文件,在我的互联网浏览器中看起来像这样:

在R中使用stargazer而对乳胶一无所知

You can open the HTML file in Word and copy the table to another document. In this way the table can be formatted. If you want exact stargazer formatting, you could take a screen capture of the table and paste it to Word. Also pdfs can be easily created in R-Studio, but they do require some knowledge of LaTeX.

您可以在Word中打开HTML文件并将表复制到另一个文档。通过这种方式,可以格式化表格。如果你想要精确的观星者格式,你可以截取表格的屏幕截图并将其粘贴到Word。此外,pdf可以在R-Studio中轻松创建,但它们确实需要一些LaTeX知识。

#2


2  

The stargazer package has an out argument in which you can specify the path to which the output can be saved.

stargazer包有一个out参数,您可以在其中指定可以保存输出的路径。

If you specify type="html" and a valid path as the out argument, you don't have to use the KNIT option that is mentioned by Mikko.

如果指定type =“html”并将有效路径指定为out参数,则不必使用Mikko提到的KNIT选项。

Thus, you can simply do:

因此,你可以简单地做:

X = data.frame(a = 1:10, b = 21:30)
mod <- lm(a ~ b, X)
library(stargazer)
stargazer(mod, type = "html", out="C:/Your/Path/Name.html")

Open this html file within MS Word and you are good to go.

在MS Word中打开这个html文件,你很高兴。

#3


0  

So, given the discussion above, I came up with this wrapper:

所以,鉴于上面的讨论,我提出了这个包装器:

gazer2pdf = function(...,filename = "default") {

  fn = paste(filename,"tex",sep=".")
  mo = stargazer(...,out=fn,out.header=T)
  texi2pdf(fn)
}

usage:

用法:

X = data.frame(a = 1:10, b = 21:30)
mod <- lm(a ~ b, X)
gazer2pdf(mod)

now default.pdf should have the table

现在default.pdf应该有表

any comments on this? will this work? what should I instruct my students to install (besides r)? is there a simple installer for tex? does it get installed as part of R or a certain package in r?

对此有何评论?这会有用吗?我应该指导我的学生安装什么(除了r)? tex有简单的安装程序吗?它是作为R的一部分安装的还是r中的某个包?

#1


2  

R-Studio would be an option. Students could use the compile notebook button to easily create HTML documents. R-Studio also contains HTML format document creation, which required only few clicks and minimal knowledge of any other programming language than R. Here is one way of doing it in R studio:

R-Studio是一个选择。学生可以使用编译笔记本按钮轻松创建HTML文档。 R-Studio还包含HTML格式文档创建,它只需要几次点击,并且对R之外的任何其他编程语言知之甚少。以下是在R studio中执行此操作的一种方法:

If HTML is good enough, this is easy. R code:

如果HTML足够好,这很容易。 R代码:

X = data.frame(a = 1:10, b = 21:30)
mod <- lm(a ~ b, X)
library(stargazer)
stargazer(mod, type = "html")

Next click File -> New File -> R HTML

然后单击文件 - >新文件 - > R HTML

Remove everything except the <htlm> tags. Copy the output from stargazer function:

删除除 标记之外的所有内容。复制stargazer函数的输出:

<html>
<table style="text-align:center"><tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td><em>Dependent variable:</em></td></tr>
<tr><td></td><td colspan="1" style="border-bottom: 1px solid black"></td></tr>
<tr><td style="text-align:left"></td><td>a</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">b</td><td>1.000<sup>***</sup></td></tr>
<tr><td style="text-align:left"></td><td>(0.000)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">Constant</td><td>-20.000<sup>***</sup></td></tr>
<tr><td style="text-align:left"></td><td>(0.000)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Observations</td><td>10</td></tr>
<tr><td style="text-align:left">R<sup>2</sup></td><td>1.000</td></tr>
<tr><td style="text-align:left">Adjusted R<sup>2</sup></td><td>1.000</td></tr>
<tr><td style="text-align:left">Residual Std. Error</td><td>0.000 (df = 8)</td></tr>
<tr><td style="text-align:left">F Statistic</td><td>11,406,627,545,111,658,741,817,889,783,808.000<sup>***</sup> (df = 1; 8)</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Note:</em></td><td style="text-align:right"><sup>*</sup>p<0.1; <sup>**</sup>p<0.05; <sup>***</sup>p<0.01</td></tr>
</table>
</html>

Next click Knit HTML and choose where to save the file. You'll get a HTML file, which looks like this in my internet browser:

然后单击Knit HTML并选择保存文件的位置。您将获得一个HTML文件,在我的互联网浏览器中看起来像这样:

在R中使用stargazer而对乳胶一无所知

You can open the HTML file in Word and copy the table to another document. In this way the table can be formatted. If you want exact stargazer formatting, you could take a screen capture of the table and paste it to Word. Also pdfs can be easily created in R-Studio, but they do require some knowledge of LaTeX.

您可以在Word中打开HTML文件并将表复制到另一个文档。通过这种方式,可以格式化表格。如果你想要精确的观星者格式,你可以截取表格的屏幕截图并将其粘贴到Word。此外,pdf可以在R-Studio中轻松创建,但它们确实需要一些LaTeX知识。

#2


2  

The stargazer package has an out argument in which you can specify the path to which the output can be saved.

stargazer包有一个out参数,您可以在其中指定可以保存输出的路径。

If you specify type="html" and a valid path as the out argument, you don't have to use the KNIT option that is mentioned by Mikko.

如果指定type =“html”并将有效路径指定为out参数,则不必使用Mikko提到的KNIT选项。

Thus, you can simply do:

因此,你可以简单地做:

X = data.frame(a = 1:10, b = 21:30)
mod <- lm(a ~ b, X)
library(stargazer)
stargazer(mod, type = "html", out="C:/Your/Path/Name.html")

Open this html file within MS Word and you are good to go.

在MS Word中打开这个html文件,你很高兴。

#3


0  

So, given the discussion above, I came up with this wrapper:

所以,鉴于上面的讨论,我提出了这个包装器:

gazer2pdf = function(...,filename = "default") {

  fn = paste(filename,"tex",sep=".")
  mo = stargazer(...,out=fn,out.header=T)
  texi2pdf(fn)
}

usage:

用法:

X = data.frame(a = 1:10, b = 21:30)
mod <- lm(a ~ b, X)
gazer2pdf(mod)

now default.pdf should have the table

现在default.pdf应该有表

any comments on this? will this work? what should I instruct my students to install (besides r)? is there a simple installer for tex? does it get installed as part of R or a certain package in r?

对此有何评论?这会有用吗?我应该指导我的学生安装什么(除了r)? tex有简单的安装程序吗?它是作为R的一部分安装的还是r中的某个包?