Stargazer 输出是代码,而不是表格 [英] Stargazer output is code, not a table

查看:60
本文介绍了Stargazer 输出是代码,而不是表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RStudio 中的 stargazer 包来生成我的数据的汇总表.出于某种原因,当我使用 html 或 Latex 代码时,我无法在输出中查看表格,但是当我输出为文本时我可以看到它.

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.

示例:

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

将其设置为乳胶:

stargazer(attitude, type = 'latex')

给出输出:

## 
## % 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}

最后,将其设置为 html:

And finally, setting this up as html:

stargazer(attitude, type = 'html')

给出输出:

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

所以 R 正在生成代码,而不是表格.有谁知道为什么会这样?不幸的是,我在一台不允许我访问互联网的计算机上执行此操作,因此我不得不通过起重机和 USB 驱动器手动安装所有软件包,因此在安装所需的各种软件包时我可能错过了一些东西.

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.

我的最终目标是生成一个表格,该表格将导出为 pdf 或 word 文档,可能作为 r markdown 脚本的一部分.所以我的问题是为什么我不能生成 html 或 latex 表?

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?

推荐答案

要以 pdf 格式呈现观星表,您可以将此代码添加到空的 R markdown (.Rmd) 文件中:

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')

```

显示为:

导出到 word 涉及以下内容(摘自 help(stargazer)):

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

要在 Microsoft Word 文档(例如 .doc 或 .docx)中包含观星表,请按照以下步骤操作:使用 out 参数将输出保存到 .htm 或 .html 文件中.在 Web 浏览器中打开生成的文件.将表格从网络浏览器复制并粘贴到 Microsoft Word 文档中.

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.

或者,如果表格的外观不太重要,您可以将以下内容放入空的 .Rmd 文件中:

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:

这篇关于Stargazer 输出是代码,而不是表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆