Search code examples
rgt

Is it possible to split the text of a gt spanner column over multiple lines?


Is it possible to split the text of a gt spanner column over multiple lines? Take the following example:

library(dplyr)
library(gt)

exibble %>%
  dplyr::select(date, time, char, row) %>%
  gt(rowname_col = "row") %>%
  tab_spanner(
    label = "dates and times",
    columns = c(date, time)
  ) 

image of gt table generated by code

Here, I would like "dates and times" to be split over two (or three) lines. Is it possible to do that?

The following, using "\n" does not work:

exibble %>%
  dplyr::select(date, time, char, row) %>%
  gt(rowname_col = "row") %>%
  tab_spanner(
    label = "dates\nand\ntimes",
    columns = c(date, time)
  ) 

Solution

  • Try this:

    library(dplyr)
    library(gt)
    
    exibble %>%
        dplyr::select(date, time, char, row) %>%
        gt(rowname_col = "row") %>%
        tab_spanner(
            label = html("dates<br> and <br>times"),
            columns = c(date, time)
        ) 
    

    enter image description here