Using stargazer with Rstudio and Knitr
knitr, r, stargazer
Solution
Since the topic has gone a bit stale, I'll assume the issue at hand is to somehow use stargazer with knitr, and not per se the conversion of the stargazer objects into HTML.
Being an avid fan of stargazer, I have come up with the following workflow:
- Write my code in an .Rmd file.
- Knit it into .md. Stargazer tables remain as LaTeX code in the resulting markdown file.
- Use pandoc to convert the markdown file to PDF. Pandoc translates the LaTeX code into proper tables. Alternatively, one can use LyX with knitr plugin to get stargazer tables nicely output in PDF format.
If one wants stargazer tables in MS Word, the best way I have found is to use LaTeX2RTF. Although the very top cells are distorted a bit, fixing it is a matter of removing an erroneous empty cell. For the rest the table is preserved and can be pasted/imported into Word.
These two strategies help use stargazer outside LaTeX. Hope it helps.
Problem
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.) ``` \[ \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} \] ```