我想使用 kableExtra 有條件地格式化包含資料的表格。
讓我們使用 iris 資料集作為 reprex。另外,讓我們假設,我想根據物種為行著色。
這是我嘗試使用的代碼 - 檔案中的一個示例:
iris %>% mutate(Species = cell_spec(Species, color = spec_color(1:10, option = "A"), link = "#",tooltip = paste0("Sepal Length: ", Sepal.Length))) %>% kable("html", escape = F, align = "c") %>% kable_styling("condensed", full_width = F)
但是,它不會根據物種為行著色。任何人都可以幫忙嗎?
uj5u.com熱心網友回復:
不確定spec_color函式是否正確,來自檔案:“spec_color 會將連續變數映射到任何 viridis 調色板。”;
您希望將離散變數映射到所有變數。
這可能是一種方法:
(注意:如果要為背景著色,請用“背景”代替“顏色”。)
library(kableExtra)
library(dplyr)
# make a minimal data frame:
iris1 <-
iris %>%
group_by(Species) %>%
slice_head(n = 4) %>%
ungroup()
iris1 %>%
mutate(across(everything(), ~cell_spec(.x, color = factor(iris1$Species, labels = c("red", "green", "blue")),
link = "#",
tooltip = paste0("Sepal Length: ", Sepal.Length)))) %>%
kable("html", escape = F, align = "c") %>%
kable_styling("condensed", full_width = F)
結果是:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/378675.html
標籤:r kableextra
上一篇:如何看待現在的鏈游和NFT?
