Using stargazer with Rstudio and Knitr

被刻印的时光 ゝ 提交于 2019-11-27 13:18:05

Since the topic has gone a bit stale, I'll assume the issue at hand is to somehow use stargazer with knitr, and not per se the conversion of the stargazer objects into HTML.

Being an avid fan of stargazer, I have come up with the following workflow:

  1. Write my code in an .Rmd file.
  2. Knit it into .md. Stargazer tables remain as LaTeX code in the resulting markdown file.
  3. Use pandoc to convert the markdown file to PDF. Pandoc translates the LaTeX code into proper tables. Alternatively, one can use LyX with knitr plugin to get stargazer tables nicely output in PDF format.

If one wants stargazer tables in MS Word, the best way I have found is to use LaTeX2RTF. Although the very top cells are distorted a bit, fixing it is a matter of removing an erroneous empty cell. For the rest the table is preserved and can be pasted/imported into Word.

These two strategies help use stargazer outside LaTeX. Hope it helps.

Junchen

Use the following code and you get a working version

```{r, results='asis'}
stargazer(model)
```

When converting to pdf, the following code works perfectly for stargazer 4.0:

```{r, results='asis'}
stargazer(model, header=FALSE, type='latex')
```

Returning to this question.

I want to use the same markdown files to produce html and pdf outputs in RStudio with knitr. That is, in RStudio I want to push the knit button, and have the options of either knitting a HTMl output, or a pdf output. I have no great interest, at the moment, in knitting a word/OpenOffice document.

I used the amazingly useful stargazer cheatsheet from Jake Russ. This exercises most of stargazer's functions. It is an R MArkdown file, with the chunk option results='asis' set for those chunks producing stargazer output.

The stargazer command itself has an argument 'type'. The default is type='latex' In Jake Russ's cheatsheet, which is intended to produce a webpage, type='html' is used throughout.

This does not work at all if you try to knit it into a pdf. Tables come out as long lists, one table cell per line, with no formatting, and occupying many pages, with no formatting.

The smallest change that I can make to allow me to produce nice pdf's within RStudio is to globally replace all the

type='html'

with

type='latex'

(note that both occur in the text of the document, as well as in the stargazer commands, so care is needed!)

This works! As far as I can see the pdf is a faithful replica of the webpage, which is exactly what I want.

Trying to knit OpenOffice documents, if I leave

type='latex'

Each table in the output is replaced by this text:-

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu % Date and time: Tue, Sep 01, 2015 - 22:22:29

If I restore the

type='html'

then each table is written, one cell per line, down the side of the page with no formatting!

In addition to the previous answer, and maybe as a simpler solution, it is possible for stargazer to output the table in html code so that when the Rmd file is knitted into html, a table is created rather than the tex code. I believe that the stargazer function can now export directly to html by setting type = 'html'.

So for example, given model fit lm1, you would use the following code in your Rmd file:

stargazer(lm1, type = 'html')

This works whether you want your final output to be html or pdf.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!