使用Rstudio和Knitr的观星者

时间:2013-02-03 07:16:04

标签: r knitr stargazer

我正在努力使用RStudio在knitr中使用stargazer输出。例如,我将下面的代码粘贴到.Rmd文件中,然后单击“编织HTML”。 [和]之间的第一个块呈现为方程式。第二个街区来自观星者。它仍然是代码。当我将第二个块(少于[和]]粘贴到Sweave文件中然后单击“编译为PDF”时,代码将呈现为表格。我安装了MikTex和Stargazer的第3版。

单击编译PDF时,答案inserting stargazer or xable table into knitr document适用于Sweave文件(Rnw)。在Rmd文件中,单击Knit HTML时不会呈现tex。

如何将stargazer输出放入Rmd文件,以便Knit HTML将乳胶代码转换为表格? (我是Latex的新手,我不确定我可以删除哪些代码,所以请为长段道歉。)

\[
\begin{aligned}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{aligned}
\]

\[
\documentclass{article}

\begin{document}


% Table created by StarGazer v.3.0.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sun, Feb 03, 2013 - 11:34:52 AM
\begin{table}[htb] \centering 
  \caption{} 
  \label{} 
\footnotesize 

\begin{tabular}{@{\extracolsep{5pt}}lc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \multicolumn{1}{c}{\textit{Dependent variable:}} \\ 
\cline{2-2} 
\\[-1.8ex] & Rate \\ 
\hline \\[-1.8ex] 
 pole & $0.071^{***}$ \\ 
  & $(0.020)$ \\ 
  & \\ 
 post & $0.095^{***}$ \\ 
  & $(0.019)$ \\ 
  & \\ 
 Constant & $-5.784^{***}$ \\ 
  & $(1.667)$ \\ 
  & \\ 
\hline \\[-1.8ex] 
Observations & $46$ \\ 
Residual Std. Error & $2.378 (df = 43)$ \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\normalsize 
\end{tabular} 
\end{table} 

\end{document}
\]

4 个答案:

答案 0 :(得分:25)

使用以下代码,您将获得正常工作的版本

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

转换为pdf时,以下代码适用于stargazer 4.0:

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

答案 1 :(得分:11)

由于主题有点陈旧,我认为手头的问题是以某种方式使用带有knitr 的stargazer,而不是将stargazer对象转换为HTML。

作为观星者的忠实粉丝,我提出了以下工作流程:

  1. 将我的代码写入.Rmd文件。
  2. 将它编织成.md。 Stargazer表在结果markdown文件中保留为LaTeX代码。
  3. 使用pandoc将降价文件转换为PDF。 Pandoc将LaTeX代码转换为适当的表。或者,可以使用带有knitr插件的LyX来获得以PDF格式输出的观星表。
  4. 如果想在MS Word中使用观星表,我找到的最好方法是使用LaTeX2RTF。虽然最顶部的单元有点扭曲,但修复它是一个删除错误的空单元的问题。对于其余的表,表将被保留,并可以粘贴/导入到Word中。

    这两种策略有助于在LaTeX之外使用观星者。希望能帮助到你。

答案 2 :(得分:5)

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!

答案 3 :(得分:4)

除了上一个答案,也许作为一个更简单的解决方案,stargazer可以在html代码中输出表格,这样当Rmd文件编织成html时,会创建一个表而不是tex代码。我相信stargazer功能现在可以通过设置type = 'html'直接导出到html。

例如,给定模型拟合lm1,您将在Rmd文件中使用以下代码:

stargazer(lm1, type = 'html')

无论您希望最终输出是html还是pdf,这都有效。