Multicolumn Footnote with kableExtra on Quarto document

208 views Asked by At

I have a table generated with kableExtra with a footnote that spans over several columns. If I run it separately in R it displays properly. However, when run inside a Quarto document, it constraints the text of the footnote to the width of the 1st column.

Here is a reproducible example

library(kableExtra)
dt <- mtcars[1:5, 1:4]

# HTML table
kbl(dt, caption = "Demo Table") %>%
  kable_styling(bootstrap_options = "striped",
                full_width = F) %>%
  add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
  footnote(c("Let's make a very long table footnote"))

enter image description here Has any one experienced this before, and if so knows how to fix it?

3

There are 3 answers

3
Quinton.Quagliano On

When knitting Quarto to PDF (via LaTeX) adding threeparttable = TRUE argument to footnote() works:

# In LaTeX format

base::library(package = kableExtra)
    dt <- mtcars[1:5, 1:4]

# PDF table
kableExtra::kbl(
  x        = dt, 
  caption  = "Demo Table",
  booktabs = TRUE
) %>%
  kableExtra::kable_styling(
    bootstrap_options = "striped",
    full_width        = FALSE
  ) %>%
  kableExtra::add_header_above(
    c(
      " ", 
      "Group 1" = 2, 
      "Group 2[note]" = 2
    )
  ) %>%
  kableExtra::footnote(
    general           = "Let's make a very long table footnote",
    threeparttable    = TRUE, # PAY ATTENTION HERE
    footnote_as_chunk = TRUE
  )

This is using the threeparttable LaTeX package to produce the effect that we want. This option is explicitly mentioned here.

With HTML, I'm afraid this problem is much more difficult because you wont have access to LaTeX packages so threeparttable = TRUE won't work. Plyaing around with your code - I noticed that removing your header row actually allowed the text to extend more. So removing this code produced that effect:

  kableExtra::add_header_above(
    c(
      " ", 
      "Group 1" = 2, 
      "Group 2[note]" = 2
    )
  ) %>%

Based on that, the footnote wrapping is somehow due to the formatting of the header column of the groups. I played with arguments of the many functions and was unable to find a definitive way to prevent the text wrapping. It seems like some folks have had some issues with the new(er) Quarto format and this package, maybe worth reading some of the open issues here.

In light of all of this, I'd recommend either switching to PDF format, forgoing the header row, or looking into other packages that may be able to accomplish this (gt maybe?).

1
Julian On

It seems to be bug, I would open an issue. In the meantime, you might try the parse-latex filter and first build the table as a latex table and then let the filter convert it to HTML, i.e.

---
format: html
filters: [parse-latex]
---

```{r}
library(kableExtra)
dt <- mtcars[1:5, 1:4]

# HTML table
kbl(dt,format = "latex", caption = "Demo Table") %>%
  kable_styling(bootstrap_options = "striped",
                full_width = F) %>%
  add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
   footnote(general = "Here is a general comments of the table that might be very long. "
           )
```
0
Pau On

I had the same problem, and the workaround I used is to add a figure caption:

```{r adjacent-treatment-estimates, fig.cap="The title of the table"}
table%>%
footnote(footnote_as_chunk = TRUE,
       threeparttable = TRUE,
       general = "Estimates with baseline TWFE model. Municipality and year FE. Standard Errors clustered at Municipality level.")
```