% kable_styling(bootstr-6ren">
gpt4 book ai didi

r - 电缆表 R : diagonal cells in bold

转载 作者:行者123 更新时间:2023-12-04 10:21:59 27 4
gpt4 key购买 nike

假设我有一个数据表:

lttrs <- c('a', 'b', 'c')
a <- c(5, 9, 2)
b <- c(3, 3, 8)
c <- c(14, 2, 0)

df <- as.data.table(cbind(lttrs, a, b, c))

view(df)

lttrs a b c
1: a 5 3 14
2: b 9 3 2
3: c 2 8 0

现在我想像这样用 kable 制作一张 table :

tableDf <- df[]%>%
select(everything())%>%
kable("html", escape = F) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left")

给我以下图片 enter image description here

我希望 (5, 3, 0) 中的对角线值加粗。我想我必须使用 cell_spec 并且必须创建 df$lttrs == colnames(df) 的条件,但是,我还没有设法解决这个问题。谁有解决办法?

最佳答案

我们可以使用library(kableExtra)中的cell_spec

for (i in df[, seq_len(.N)]) {
df[i, i+1L] <- cell_spec(df[i, i+1L, with=F], "html", bold=T)
}

kable(df, "html", escape = F) %>%
kable_styling(bootstrap_options = "striped", full_width = F)

enter image description here

如果你真的想让事情脱颖而出,你还可以改变颜色和字体等。例如,

for (i in df[, seq_len(.N)]) {
df[i, i+1L] <- cell_spec(df[i, i+1L, with=F], "html", bold=T, background = "red", color = "white")
}

enter image description here

关于r - 电缆表 R : diagonal cells in bold,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60811107/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com