1

给定一个小数据集如下:

library(gt)
library(tidyverse)

id <- c(1,2,3,4,5)
res1 <- c("true", "true", "false", "true", "false")
res2 <- c("false", NA, NA, "true", "true")
df <- data.frame(id, res1, res2)

df %>% 
  gt()

出去:

在此处输入图像描述

对于列res1res2,假设值内容为trues,我需要用red背景颜色突出显示该单元格,如果false是s,用green颜色突出显示,对于其他情况,保持它们的颜色为原始颜色。

我如何在 R 中使用 gt 包来实现这一点?

注意:下面链接中的示例代码和结果适用于values,而不是这种情况下的字符串。

在此处输入图像描述

参考:

https://gt.rstudio.com/reference/data_color.html

4

1 回答 1

1

按照手册使用- data_color

df %>% 
  gt() %>% 
  data_color(
    columns = c("res1", "res2"),
    colors = c("green", "red"),
    apply_to = "fill",
    autocolor_text = FALSE)

在此处输入图像描述

于 2022-01-18T12:58:22.487 回答