带有条件字体颜色的kablextra在正确位置不正确绘制正确的颜色

发布于 2025-02-06 15:13:37 字数 997 浏览 3 评论 0原文

我在data.table中有一个以下基因列表表;

t.mini
    cluster1 cluster15 cluster31
      <fctr>    <fctr>    <fctr>
 1:   Slc6a6      Cd8a    Retnla
 2:   Sema3g     Cd8b1    Cxcl15
 3:  Cyp26b1     Pdcd1   Scgb3a2
 4:    H2-Q6      Cd3g     Sftpb
 5:    Klhl5    Sh2d2a   Scgb1a1
 6:    Dnah8     Cxcr6    Sftpa1
 7:   Tm4sf1      Cd3e      Cbr2
 8: Cdc42bpb     Rgs16     Sftpd
 9:   Cxcl12      Nkg7     Wfdc2
10:   Sptan1    Ms4a4b    Cyp2f2

我想通过使用KableExtra将任何CXCL基因的字体颜色输出为红色,然后提出了以下代码;

lapply(1:3, function(y) {
  kbl(t.mini) |> 
  kable_classic_2(full_width=F) |> 
  column_spec(column = y, color = fifelse(unlist(t.mini[, lapply(.SD, function(x){grepl(glob2rx("Cx*"), x)})]), "red", "black"))
})

但是,所得表的红色字体不仅映射到了错误的基因,而且只有一个基因。

任何指针都将不胜感激。

I have a following gene list table in the data.table;

t.mini
    cluster1 cluster15 cluster31
      <fctr>    <fctr>    <fctr>
 1:   Slc6a6      Cd8a    Retnla
 2:   Sema3g     Cd8b1    Cxcl15
 3:  Cyp26b1     Pdcd1   Scgb3a2
 4:    H2-Q6      Cd3g     Sftpb
 5:    Klhl5    Sh2d2a   Scgb1a1
 6:    Dnah8     Cxcr6    Sftpa1
 7:   Tm4sf1      Cd3e      Cbr2
 8: Cdc42bpb     Rgs16     Sftpd
 9:   Cxcl12      Nkg7     Wfdc2
10:   Sptan1    Ms4a4b    Cyp2f2

I would like to output the table with the font color of any Cxcl genes to red by using kableextra, and I came up with a code as follows;

lapply(1:3, function(y) {
  kbl(t.mini) |> 
  kable_classic_2(full_width=F) |> 
  column_spec(column = y, color = fifelse(unlist(t.mini[, lapply(.SD, function(x){grepl(glob2rx("Cx*"), x)})]), "red", "black"))
})

However, the resulting table has red font mapped not only to a wrong gene but only one gene as in this table.

enter image description here

Any pointers will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后eg是否自 2025-02-13 15:13:38

使用cell_spec并首先将数据传递到kbl可以工作 -

library(kableExtra)
library(data.table)

cols <- names(t.mini)
t.mini[, (cols) := lapply(.SD, 
   function(x) cell_spec(x, color = fifelse(grepl('Cxcl', x), 'red', 'black'))), 
       .SDcols = cols]

t.mini |>
  kbl(escape = FALSE) |>
  kable_classic_2(full_width=F)

“在此处输入图像描述”

Using cell_spec and transforming the data first before passing it to kbl can work -

library(kableExtra)
library(data.table)

cols <- names(t.mini)
t.mini[, (cols) := lapply(.SD, 
   function(x) cell_spec(x, color = fifelse(grepl('Cxcl', x), 'red', 'black'))), 
       .SDcols = cols]

t.mini |>
  kbl(escape = FALSE) |>
  kable_classic_2(full_width=F)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文