Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

r - Using stargazer with Rstudio and Knitr

I'm struggling to use stargazer output in knitr, using RStudio. For example, I paste the code below into a .Rmd file, then click Knit HTML. The first block between [ and ] is rendered as equations. The second block is from stargazer. It remains as code. When I paste the second block, less [ and ], into a Sweave file and then click compile as PDF, the code renders as a table. I have MikTex installed and version 3 of Stargazer.

The answer inserting stargazer or xable table into knitr document works for me in a Sweave file (Rnw) when clicking compile PDF. In an Rmd file, the tex is not rendered when clicking Knit HTML.

How can I put stargazer output into a Rmd file so that Knit HTML converts the latex code to a table? (I'm new to Latex, and am not sure what code I can delete, so apologise for the long segment.)

[
egin{aligned}
dot{x} & = sigma(y-x) \
dot{y} & = 
ho x - y - xz \
dot{z} & = -eta z + xy
end{aligned}
]

[
documentclass{article}

egin{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
egin{table}[htb] centering 
  caption{} 
  label{} 
footnotesize 

egin{tabular}{@{extracolsep{5pt}}lc} 
\[-1.8ex]hline 
hline \[-1.8ex] 
 & multicolumn{1}{c}{extit{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] 
extit{Note:}  & multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \ 

ormalsize 
end{tabular} 
end{table} 

end{document}
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...