Rmarkdown of Stargazer: LaTeX Error if align is set to TRUE
Asked Answered
C

2

7

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

LaTeX Error: \caption outside float.

I checked it and what the message says is wrong. Copying the Stargazer output directly into an Latex document works fine. Copying it into an rmarkdown document produces the same error (which is no surprise but I just wanted to be sure). After playing around a bit I figured out that it is working in rmarkdown if the significance stars(*) are removed (or to precise the ^{***}). However, stargazer is producing them by default and they are also an important part of the output.

Is there a 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)
```
Congest answered 8/8, 2016 at 19:5 Comment(3)
shouldn't align be either a list of left, right or center (for each column)?Zara
@Zara No. It must be logical. It denotes whether it is aligned according to the decimal mark or not.Congest
To me, this looks like pandoc escapes the LaTeX code generated by stargazer if align=TRUE.Skirret
O
3

On linux systems, wrapping stargazer inside either invisible or suppressMessages works to suppress the 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 the table. To allow for further processing of this output, stargazer also returns the same output invisibly as a character vector.

We use suppressMessages or invisible to ensure that only the first output (produced by cat) is rendered. The character vector output turns to garbage when rmarkdown attempts to render it using print, rather than cat

Oxalate answered 9/8, 2016 at 4:29 Comment(5)
Somehow it still does not work for me. Can you just copy the latex code from the console into the document?Congest
Tested and working on RStudio Version 0.99.484(64 bit linux), stargazer 5.2, knitr 1.13. What happens when you try my code? Did you save the .Rmd before running knit? For me, it fails if I don't save first.Oxalate
I still get the the same error as before. I am working with the windows version of R studio. Not sure if that could be the reason. I guess that it is an pandoc error because the code works fine when directly copied into LaTeX. What error do you get if you run stargazer without the wrapper?Congest
I've had a chance to play with this on a windows systems, and can confirm that I see the same problem as you there. I tried with both pdflatex and xetex engines - same issue. Seems to be most likely a pandoc bug. Could you use linux instead? If so, my solution above will do what you need if a better solution for windows doesn't get posted.Oxalate
Thanks for the confirmation of the error. However, I will not change the OS since the problem is not so serious and I do not depent on stargazer. I also found a workaround using regex. When I have time I will post what I did for people who are interested in how to modify stargazer output beyond the provided function arguments.Congest
S
0

I had the same problem. I tried installing the latest version of pandoc and it solved the problem.

Sucker answered 18/3, 2020 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.