Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using stargazer with Rstudio and Knitr

Tags:

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} &amp; = \sigma(y-x) \\ \dot{y} &amp; = \rho x - y - xz \\ \dot{z} &amp; = -\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} \] 
like image 358
Isaiah Avatar asked Feb 03 '13 07:02

Isaiah


People also ask

How do you use the stargazer in RStudio?

This can be done by typing “install. packages(“stargazer”)”, and then “library(stargazer)” in the next line. Installing Stargazer will only need to be done once, but the second command, which loads the package will need to be typed each session you wish to use it.

How do you use the knitr in RStudio?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

How can you compile the R Markdown document using knitr package?

The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.

What does Stargazer do in R?

stargazer is an R package that creates LATEX code, HTML code and ASCII text for well-formatted regression tables, with multiple models side-by-side, as well as for summary statistics tables, data frames, vectors and matrices.


1 Answers

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') ``` 
like image 79
Junchen Avatar answered Sep 28 '22 11:09

Junchen