在 gt_table 中的组名行中插入换行符
insert linebreak in to groupname rows in gt_table
我正在尝试在 gt_table
中的 groupname_col 的标签内创建换行符
有没有办法针对 table 的这一部分?
我知道如何使用 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)
)
深入挖掘后发现您可以使用 opt_css() 函数定位 gt_group_heading class。
添加一些 css 并使用全局选项确实起到了作用...
我在数据集中添加了另一列汽车,只是为了让 table 在您 运行 代码时看起来更好。
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;
}
"
)
我正在尝试在 gt_table
中的 groupname_col 的标签内创建换行符
有没有办法针对 table 的这一部分?
我知道如何使用 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)
)
深入挖掘后发现您可以使用 opt_css() 函数定位 gt_group_heading class。
添加一些 css 并使用全局选项确实起到了作用...
我在数据集中添加了另一列汽车,只是为了让 table 在您 运行 代码时看起来更好。
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;
}
"
)