13
votes

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.

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?

3

3 Answers

12
votes

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

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.

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.

2
votes

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:

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

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

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

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

enter image description here

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.

0
votes

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

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?