我在工作中经常使用{gt}包;然而,我必须将多种 colored颜色 应用于几个报告中的多个列.因此,我需要您的帮助来使用{Purrr}包自动执行此操作.下面是一个类似于我每月工作的数据集的示例.

感谢您的好意帮助!

data_tbl <- tibble(
  name = c('deng', 'alier',  'atem', 'garang', 'akuien'), 
  english = c(87, 57, 76, 98, 79), 
  mathematics = c(88, 98, 87, 69, 88), 
  statistics = c(76, 99, 58,  84,  90)
)

# Tabulate the data with gt

my_gt <- data_tbl %>% 
  
  # Initialize a gt table
  gt() %>% 
  
  # Formatting
  tab_style(
    style = list(
      cell_fill(color = "#1dab48")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = english,
      rows = english >= 80
    )
  ) %>% 
  
  tab_style(
    style = list(
      cell_fill(color = "#1dab48")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = mathematics,
      rows = mathematics >= 80
    )
  ) %>% 
  
  tab_style(
    style = list(
      cell_fill(color = "#1dab48")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = statistics,
      rows = statistics >= 80
    )
  ) %>% 
  
  
  # Formatting - yellow
  tab_style(
    style = list(
      cell_fill(color = "#c97928")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = english,
      rows = english >= 70 & english < 80
    )
  ) %>% 
  
  tab_style(
    style = list(
      cell_fill(color = "#c97928")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = mathematics,
      rows = mathematics >= 70 & mathematics < 80
    )
  ) %>% 
  
  tab_style(
    style = list(
      cell_fill(color = "#c97928")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = statistics,
      rows = statistics >= 70 & statistics < 80
    )
  ) %>% 
  
  # Formatting - red
  tab_style(
    style = list(
      cell_fill(color = "#c94e28")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = english,
      rows = english < 70 
    )
  ) %>% 
  
  tab_style(
    style = list(
      cell_fill(color = "#c94e28")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = mathematics,
      rows = mathematics < 70 
    )
  ) %>% 
  
  tab_style(
    style = list(
      cell_fill(color = "#c94e28")
      # cell_text(color = "white")
    ),
    locations = cells_body(
      columns = statistics,
      rows = statistics < 70
    )
  )

我的_gtenter image description here

推荐答案

以下是使用for循环的一种方法:

library(dplyr)
library(gt)

tbl1 <- data_tbl %>%
  # Initialize a gt table
  gt()

nm1 <- names(data_tbl[,2:4])

for(i in seq_along(nm1)) {

  tbl1 <- tbl1 %>%
    tab_style(
      style = list(
        cell_fill(color = "#1dab48")
      ),
      locations = cells_body(
        columns = nm1[i],
        rows = tbl1$`_data`[[nm1[i]]] >= 80
      )
    ) %>%

    tab_style(
      style = list(
        cell_fill(color = "#c97928")

      ),
      locations = cells_body(
        columns = nm1[i],

        rows = tbl1$`_data`[[nm1[i]]] >= 70 & tbl1$`_data`[[nm1[i]]] < 80
      )
    ) %>%

    tab_style(
      style = list(
        cell_fill(color = "#c94e28")

      ),
      locations = cells_body(
        columns = nm1[i],

        rows = tbl1$`_data`[[nm1[i]]] < 70
      )
    )
}

Output

enter image description here

R相关问答推荐

如何计算具有NA的行的更改百分比

R -创建一列,指示另一列是否具有相同的值

在图内移动y轴上的标签

在R中,如何创建时间间隔的图表?

在R中使用GG Plot时如何 suppress 等值线图中的彩色条

根据固定值范围在tible中添加新行

R Markdown中的交叉引用表

使用tidy—select创建一个新的带有mutate的摘要变量

如何在R中对深度嵌套的tibbles中的非空连续行求和?

在另存为PNG之前隐藏htmlwidget绘图元素

R函数,用于生成伪随机二进制序列,其中同一数字在一行中不出现超过两次

将重复项转换为NA

比较理论阿尔法和经验阿尔法

跨列查找多个时间报告

来自程序包AFEX和amp;的类/函数和NICE_TABLE&冲突

扩展R中包含列表的数据框

如何将这个小列表转换为数据帧?

Conditional documentr::R中数据帧的summarize()

名字的模糊匹配

如何在shiny 的应用程序 map 视图宣传单中可视化单点