using stargazer in R without knowing anything about latex
Asked Answered
C

3

13

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?

Creodont answered 5/4, 2014 at 7:40 Comment(0)
C
15

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.

Christianize answered 12/11, 2016 at 23:44 Comment(1)
Amazing feature! It saves a lot of time to get those beautifull regression tables.Castaway
K
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:

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.

Ketchup answered 5/4, 2014 at 8:3 Comment(10)
I thought more in the direction of writing a script that creates a full latex document and compile it in the background to pdf and then showing it to the students. but thought that someone might have done something similar, or better.Creodont
It is possible to plot tables in device. See grid.table from gridExtra package and addtable2plotfrom plotrix package.Ketchup
Never mind the comment above. I see that both functions require data frames and would not work with stargazer. Maybe EDIT2 could be a solution?Ketchup
thanks for the suggestions so far. In fact, html may look nice but from my experience is not extremely useful to social scientists who write/submit papers in word/pdf documents and need to embed tables and graphs within such documents. I've also looked at the 'type="text"' option of stargazer - it is not bad, but still not as great as the result of a latex document. I guess I'll have to write that script myself and make sure students have pdflatex in the background installed somewhere. is there a package in R that installs a latex to pdf module that runs in the background so I can use it?Creodont
Sweawe and knitr does that, but then it is LaTeX stuff. R-Studio is very easy introduction for sweawing/knitting as you can run everything from there. If you look at those 10s or even 100s of closed questions here at SO and TeX, you will notice that converting LaTeX/R to Word is not easy. r-statistics.com/2013/03/…. The bottom line: there is no amazing solution for this. As most people able to create a solution do not care about MS Word, seeing an easy solution in the near future is not that likely either.Ketchup
see my answer for the continuation of the discussion (it was too long for a comment. sorry)Creodont
When you posted the HTML, did it originally have the character entity &lt;rather than the ordinary less-than symbol "<" in the last part of the table? Apart from the problem , the HTML displays here in Word almost identically to the way you show it, except you may have to reduce the font size to prevent wrapping of the F statistic row.Mcpherson
@bibadia I cannot find &lt; character entities, but I just copy-pasted the code as it was. No changes. And I agree with you that actually HTML table does display almost identical to the one above. I guess I got confused, because Word would open web-page template, but you can actually change that or copy the table to any Word document.Ketchup
Not a big deal, but what I meant was that "<" is only allowed at the beginning of an HTML element, and if it is elsewhere it should be replaced by &lt;. At least, as far as I know. So if something is generating such "<" characters rather than &lt; then technically speaking, you shouldn't expect the browser to display it properly. And Word doesn't. But maybe I am wrong about the standard, and/or some "tolerate" "<" when it cannot be mistaken for the start of a tag. Word definitely won't display any arbitrary HTML correctly but perhaps in this case it does it well enough.Mcpherson
Ah, I see. Thanks for the comment. I am not sure what is going on there, but it does seem to work.Ketchup
C
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

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?

Creodont answered 5/4, 2014 at 11:18 Comment(1)
This is an old thread but I feel sympathy for the problem. And your function works on for me if I use tools::texi2pdf instead just texi2pdf. The result is not perfect though, as the table is printed to a full page with a page number, which is not ideal for pasting. I'd be interested how you dealt with the issue at the end of the day.Companionway

© 2022 - 2024 — McMap. All rights reserved.