r - 在 gt_table 中的 groupname 行中插入换行符
问题描述
我正在尝试在 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)
)
解决方案
做了一些深入的挖掘,发现您可以使用 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;
}
"
)
推荐阅读
- c# - UPDATE 语句与 Visual Studio 2019 的 FOREIGN KEY 约束冲突
- javascript - 从另一个输入值自动填充输入
- python - 如何减少训练过程中模型损失的方差?
- postgresql - 在 PostgreSQL 中当 1 为空时比较 2 个表
- c# - 如何检查是否显示 Windows 登录屏幕(在 .NET 中)
- react-native - 世博会新闻发布会如何解决
- abap - 如何使 ABAP 代码在任何基于 ABAP 的系统中工作
- sql-server - SQL:来自 cte 的 xml.nodes 非常缓慢
- java - 每次收听RSS并实时获取更新的好方法是什么?
- cors-anywhere - 不同域的CORS ORIGIN问题