Rmarkdown Stargazer: LaTeX Error if align is set to TRUE

I am working with stargazer and I want to create LaTeX output for a simple lm object. The problem is that I cannot set align = TRUE without receiving an error.

Error LaTeX: \ caption outside float.

I checked it and what the message says is wrong. Copying stargazer output directly to a Latex document works great. Copying it into an rmarkdown document leads to the same error (which is not surprising, but I just wanted to be sure). After playing a little, I realized that it works in rmarkdown if the values โ€‹โ€‹of the stars (*) are removed (or for the exact ^{***} ). However, stargazer creates them by default, and they are also an important part of the output.

Is there any way to make it work?

 --- header-includes: - \usepackage{dcolumn} output: pdf_document --- ## R Markdown ```{r, include = FALSE} library(stargazer) df <- data.frame(x = 1:10 + rnorm(100), y = 1:10 + rnorm(100)) reg <- lm(y ~ x, data = df) ``` ```{r, results='asis', echo = FALSE} stargazer(reg, header = FALSE, align = TRUE) ``` 
+5
source share
1 answer

On linux systems, wrapping a stargazer inside an invisible or suppressMessages works to suppress garbage that otherwise gets rendered. Unfortunately, this solution does not seem to work on Windows computers.

 --- header-includes: - \usepackage{dcolumn} output: pdf_document --- ## R Markdown ```{r, include = FALSE} library(stargazer) df <- data.frame(x = 1:10 + rnorm(100), y = 1:10 + rnorm(100)) reg <- lm(y ~ x, data = df) ``` ```{r, results='asis', echo = FALSE} invisible(stargazer(reg, header = FALSE, align = TRUE)) # suppressMessages(stargazer(reg, header = FALSE, align = TRUE)) # also works ``` 

enter image description here

The reason is that (from the help page)

stargazer uses cat () to output LaTeX / HTML code or ASCII text for a table. To provide further processing for this output, stargazer also returns the same output discreetly as a character vector.

We use suppressMessages or invisible to ensure that only the first output (generated by cat) is displayed. The output of a character vector becomes garbage when rmarkdown tries to display it using print , not cat

+1
source

All Articles