1

我正在尝试在 gt_table 中的 groupname_col 的标签内创建换行符
有没有办法针对这块表?
我知道如何使用 fmt_markdown 在常规列中插入换行符,但不能应用于行标签
感谢帮助

这是我尝试过的示例
我希望我的组行有一个换行符,如变量 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)
          )
4

1 回答 1

0

做了一些深入的挖掘,发现您可以使用 opt_css() 函数定位 gt_group_heading 类。

添加一些 CSS 并使用全局选项确实起到了作用......

我在数据集中添加了另一列 car,只是为了在运行代码时使表格看起来更漂亮。

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;


    }

    "
  )
于 2021-02-14T03:47:45.347 回答