3

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!

user1713174
  • 307
  • 2
  • 7
  • Are you using `reactable` for a specific reason? `kableExtra` plays much nicer with pdfs – Matt Feb 01 '22 at 21:10

1 Answers1

0

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.

Matt
  • 7,255
  • 2
  • 12
  • 34
  • Thank you! I was using `reactable` entirely because it's what I know (I'm new to making pdf tables) -- `kableExtra` is much better for pdfs! Thanks!!!! – user1713174 Feb 03 '22 at 23:25