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.
Any pointers will be appreciated.
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)