Add line break to column group name in reactable

Viewed 167

I am making a table with reactable in R markdown and I want a column group with a line break. Minimal example is here:

---
output: pdf_document
---

```{r, echo=F}
library(reactable)
data = mtcars[1:4, 1:4]

reactable(
  data,
  columnGroups = list(colGroup(name = "This is Line 1 of the group name \\ This is Line 2 of the group name", columns=names(data)))
)

Note that the \\ just gets printed in the final output. I've tried other HTML-y things like <br /> and \n and they get printed in the final output too.

Any thoughts?

Thanks!

1 Answers

If you're not opposed to using kableExtra:

You'll have to make two changes:

  1. Add the header-includes option to your yaml at the top.
  2. Change the reactable code to kable
    ---
    title: "test"
    output: pdf_document
    header-includes:
    - \usepackage{caption}
    
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r, echo = F}
    library(kableExtra)
    data = mtcars[1:4, 1:4]
    
    kable(
      data, 
      caption = "This is Line 1 of the group name\\\\This is Line 2 of the group name"
      )
    ```

This gives us:

You can format it with other kableExtra functions if you want horizontal/vertical lines, specific column widths, etc.

Related