insert linebreak in to groupname rows in gt_table

First time here, perhaps this is wrong, but I also posted my question on SO. Hope I'm not committing any major faux pas by posting in 2 locations, but was hoping more people might see my question.

the SO post : here

Question: how to insert linebreaks in the groupname_col of the gt() function
I have tried using a string with
, br(), \n without any luck.
I have also played with the row_group.sep parameter, but this still does not get the desired result

Is there a way to target the groupname_col portion of the table?
I know how to insert line breaks into regular columns with fmt_markdown, but can't apply to the group row labels

Here is an example of what I have tried
I would like my group rows to have a line break as indicated in the variable group_label

library(tidyverse)
library(gt)

df <- tibble(
      
          name = c("john", "john", "jerry", "jerry", "jack", "jack", "jim", "jim"), 
          day = c("wed", "wed", "thurs", "thurs", "mon", "mon", "tues", "tues"), 
          lotto = c(12, 42, 54, 57, 234, 556, 34, 23)
      
) 

df %>% 
          mutate(group_label = paste(name, day, sep = "<br>")) %>%
          gt(
                    groupname_col = "group_label", 
                    rowname_col = "day"
                
          ) %>%
          cols_hide(
                
                    columns = vars(name)
                
          ) %>%
          fmt_markdown(
                
                    columns = vars(group_label)
          )

I considered trying to apply line breaks to the gt_group_heading_row class after doing some investigating into the css, but I really am not sure how I would tackle that

Any help much appreciated!

Well did some deep digging and found that you can target the gt_group_heading class with the opt_css() function.

Adding some css and playing with global options really did the trick here...

I added another column, car, to the dataset just to make the table look nicer when you run the code.

library(tidyverse)
library(gt)

df <- tibble(

  name = c("john", "john", "jerry", "jerry", "jack", "jack", "jim", "jim"), 
  day = c("wed", "wed", "thurs", "thurs", "mon", "mon", "tues", "tues"), 
  lotto = c(12, 42, 54, 57, 234, 556, 34, 23), 
  car = c("chevy", "toyota", "honda", "gmc", "tesla", "nissan", "ford", "jeep")

) 

df

options(gt.row_group.sep = "\n")

df %>% 
  gt(

    id = "one",
    groupname_col = c("name", "day"), 
    rownames_to_stub = TRUE, 
    row_group.sep = getOption("gt.row_group.sep", "\n")

  ) %>%

  opt_css(
    css = "

    #one .gt_group_heading {

    white-space:pre-wrap;
    word-wrap:break-word;


    }

    "
  )

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.