Stargazer output is code, not a table

Dorton picture Dorton · Aug 17, 2017 · Viewed 23.1k times · Source

I am trying to use the package stargazer in RStudio to generate a summary table of my data. For some reason, I am not able to view the table in the output when I use either the html or latex code, but I can see it when I output as text.

An example:

library(stargazer)
stargazer(attitude, type = 'text')

Output looks like (as it is supposed to):

## 
## =====================================
## Statistic  N   Mean  St. Dev. Min Max
## -------------------------------------
## rating     30 64.633  12.173  40  85 
## complaints 30 66.600  13.315  37  90 
## privileges 30 53.133  12.235  30  83 
## learning   30 56.367  11.737  34  75 
## raises     30 64.633  10.397  43  88 
## critical   30 74.767  9.895   49  92 
## advance    30 42.933  10.289  25  72 
## -------------------------------------

Setting this up as latex:

stargazer(attitude, type = 'latex')

Gives the output:

## 
## % Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
## % Date and time: Wed, Aug 16, 2017 - 4:28:34 PM
## \begin{table}[!htbp] \centering 
##   \caption{} 
##   \label{} 
## \begin{tabular}{@{\extracolsep{5pt}}lccccc} 
## \\[-1.8ex]\hline 
## \hline \\[-1.8ex] 
## Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
## \hline \\[-1.8ex] 
## rating & 30 & 64.633 & 12.173 & 40 & 85 \\ 
## complaints & 30 & 66.600 & 13.315 & 37 & 90 \\ 
## privileges & 30 & 53.133 & 12.235 & 30 & 83 \\ 
## learning & 30 & 56.367 & 11.737 & 34 & 75 \\ 
## raises & 30 & 64.633 & 10.397 & 43 & 88 \\ 
## critical & 30 & 74.767 & 9.895 & 49 & 92 \\ 
## advance & 30 & 42.933 & 10.289 & 25 & 72 \\ 
## \hline \\[-1.8ex] 
## \end{tabular} 
## \end{table}

And finally, setting this up as html:

stargazer(attitude, type = 'html')

Gives the output:

## 
## <table style="text-align:center"><tr><td colspan="6" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Statistic</td><td>N</td><td>Mean</td><td>St. Dev.</td><td>Min</td><td>Max</td></tr>
## <tr><td colspan="6" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">rating</td><td>30</td><td>64.633</td><td>12.173</td><td>40</td><td>85</td></tr>
## <tr><td style="text-align:left">complaints</td><td>30</td><td>66.600</td><td>13.315</td><td>37</td><td>90</td></tr>
## <tr><td style="text-align:left">privileges</td><td>30</td><td>53.133</td><td>12.235</td><td>30</td><td>83</td></tr>
## <tr><td style="text-align:left">learning</td><td>30</td><td>56.367</td><td>11.737</td><td>34</td><td>75</td></tr>
## <tr><td style="text-align:left">raises</td><td>30</td><td>64.633</td><td>10.397</td><td>43</td><td>88</td></tr>
## <tr><td style="text-align:left">critical</td><td>30</td><td>74.767</td><td>9.895</td><td>49</td><td>92</td></tr>
## <tr><td style="text-align:left">advance</td><td>30</td><td>42.933</td><td>10.289</td><td>25</td><td>72</td></tr>
## <tr><td colspan="6" style="border-bottom: 1px solid black"></td></tr></table>

So R is generating the code, not the table. Does anyone know why this is happening? Unfortunately I am doing this on a computer which does not allow me access to the internet, so I had to manually install all packages via a cran and a usb drive, so I may have missed something while installing the various packages required.

My end goal here is to generate a table that will be exported as either a pdf or word document, possibly as a part of an r markdown script. So really my question is why can't I generate either an html or latex table?

Answer

markdly picture markdly · Aug 17, 2017

To render a stargazer table in pdf you can add this code to an empty R markdown (.Rmd) file:

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(stargazer)
```

Here is the latex table in a PDF document:

```{r mylatextable, results = "asis"}
stargazer(attitude, type = 'latex')

```

Which appears as:

stargazer table in pdf doc

Exporting to word involves the following (taken from help(stargazer)):

To include stargazer tables in Microsoft Word documents (e.g., .doc or .docx), please follow the following procedure: Use the out argument to save output into an .htm or .html file. Open the resulting file in your web browser. Copy and paste the table from the web browser to your Microsoft Word document.

Alternatively, if the appearance of the table doesn't matter too much you can put the following in an empty .Rmd file:

---
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(stargazer)
```

Stargazer table in microsoft word:

```{r word_table, comment = ''}
stargazer(attitude, type = 'text')

```

Which results in a raw but readable table:

raw stargazer table in word