1

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?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94

1 Answers1

0

There are several distinct issues here. Let's unpack them:

1. Error triggered by first chunk of code

The unclosed square brackets in the dataset triggered some issues when knitting to beamer. I'm not well-versed in LaTeX, but somehow this is mitigated if [ is not the first character in a table row. Based on your example, which passes a named vector into X1, I suppose the names c(A61, ... A65) have some meaning & you'd like to include them in the results.

Suggested solution: Replacing tribble() with data.frame(), because the former will include row names by default while the latter is... opinionated.

Other alternatives along similar vein: Insert another column before X1, or pass the tibble to xtable(type = "latex") and set the chunk's option to result="asis". (I think the latter is overkill, though, and less flexible if you want to switch between different output formats. Also see point 4 below.)

2. Appearance of PDF after merging slides 2 & 3

Not sure if I'm missing something, but I think that's expected behaviour? If you merge the content from the two slides, they will be rendered sequentially on the same slide, with possible spillover when faced with space constraints.

3. Absence of first slide when knitting to HTML ioslides

You didn't include a separator before the first slide, so it didn't know that should begin a new slide, after the title page.

Suggested solution: Add --- in a new line before the first slide.

4. Blank slide 3 when knitting to HTML ioslides / Slidy

The type option for stargazer() function is "latex" by default (even if you didn't state it).

Suggested solution: Change type to "html", and the results should render correctly. (Do note that unlike kable(), which can automatically determine format between latex / html / pipe / etc. if it's called within a knitr document, stargazer() needs this to be stated explicitly in your code. So if you are switching between beamer & ioslides/slidy output formats, you should change switch here too.)

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Thanks a lot. Replacing "tibble" with "data.frame" worked - now PDF renders correctly. And thank you for mentioning type="hmtl" in stargazer() - I forgot about it. I still don't get two things: 1) I kind of thought about the square bracket "[" being the issue here, but if I produce square brackets with cut(include.lowest = TRUE), then no error happens. 2) In the real example that I need, I load the data with read_csv, which produces tibble. But changing read_csv to read.csv does not help. The actual dataset comes from here: https://archive.ics.uci.edu/ml/datasets/statlog+(german+credit+data) – Fedor Duzhin Mar 30 '23 at 04:33
  • You can try using `as.data.frame()` to coerce the tibble into a data frame. As for the square bracket, I can't see what you did in your actual use case, and the default levels from `cut` should typically start with round brackets instead of square brackets anyway, so I have some trouble imaging what's actually going on. You may want to open a new question on this if you want to investigate further. – Z.Lin Mar 30 '23 at 04:45
  • It's alright, thank you anyway. I was just curious about it. – Fedor Duzhin Mar 30 '23 at 05:33