I have encountered a strange error that appears when I use a combination of kableExtra output and stargazer output. Here is the error message:
! Illegal parameter number in definition of \beamer@doifinframe.
<to be read again>
A minimal working example consists of 3 slides, each with a chunk of R codes. The first slide prints some dataset with kableExtra function. The second slide defines a regression model that has nothing to do with the data printed in the first slide. The third slide prints the table of regression coefficients of the model defined in the second slide.
The problem is that when I click "Knit to PDF Beamer", I get the error message quoted above.
Below is the full Markdown code (you can create an R markdown file and see if you get the same behavior):
---
title: "Minimal example"
author: "Author"
date: "Class 2"
output:
beamer_presentation:
colortheme: seagull
fig_caption: no
fonttheme: structurebold
slide_level: 2
theme: Boadilla
toc: yes
slidy_presentation: default
ioslides_presentation: default
classoption: t
header-includes:
- \usepackage{booktabs}
- \usepackage{colortbl}
- \usepackage{xcolor}
fontsize: 10pt
---
```{r global_options, include=FALSE, message=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(stargazer)
library(kableExtra)
```
Here are some data
```{r}
tibble(X1 = c("A61" = "(-Inf, 100)",
"A62" = "[100, 500)",
"A63" = "[500, 1000)",
"A64" = "[1000, Inf)",
"A65" = "unknown"),
X2 = 1:5) %>%
kable(digits = 3) %>%
kable_styling(latex_options = "striped")
```
---
Here is a logistic model
```{r}
mod_lin <- lm(disp ~ mpg + hp,
data = mtcars)
mod_lin
```
---
```{r results='asis'}
mod_lin %>% stargazer(type = "latex",
header=FALSE,
no.space = TRUE,
omit.table.layout = c("l"),
omit.stat = c("ser", "f", "rsq"))
```
The funny thing is that the error message disappears if I comment out either the first slide (i.e., do not print a dataset) or the third slide (i.e., do not print a table of regression coefficients).
Also, the error disappears if I change the dataset printed in the first slide with some other dataset. For example, replacing the chunk of R code in my example with the following will fix the error and the deck of slides will render correctly:
mtcars %>% select(1:5) %>%
head %>%
kable(digits = 3) %>%
kable_styling(latex_options = "striped")
If I merge the second slide and and the third slide, then the document renders into PDF, but incorrectly - it shows everything on one slide instead of two slides and the regression table does not entirely fit on the slide.
If I knit into HTML ioslides, then the first slide does not appear - the title is followed by the second slide. And the third slide is just blank.
IF I knit into HTML Slidy, then the first and the second slides are there, but the third slide is blank.
I have Macbook Pro with M1 chip, MacOS Monterey 12.0.1, R 4.2.2, RStudio 2023.03.0+386, freshly installed MacTex - I couldn't figure out which version it is, but I just installed the newest one. I don't use tinytex.
So what could be the problem and how do I fix it?