我正在努力使用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}
\]
答案 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。
作为观星者的忠实粉丝,我提出了以下工作流程:
答案 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,这都有效。