Let's say I have a data table:
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
Now I would like to make a table with kable like this:
tableDf <- df[]%>%
select(everything())%>%
kable("html", escape = F) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left")
I would like the diagonal values to be bold in (5, 3, 0). I guess I have to work with cell_spec and have to make a condition in which df$lttrs == colnames(df), however, I have not managed to fix this. Anyone has a solution to it?
We can use cell_spec
from library(kableExtra)
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)
If you really want things to stand out, you can also change colors and fonts etc. For example,
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")
}