【问题标题】:Changing the color of the datatable rows based on multiple conditions根据多个条件更改数据表行的颜色
【发布时间】:2019-12-25 16:07:28
【问题描述】:

我有一个包含许多行的数据表,为了方便起见,我只使用两列。 下面是数据表中的两列。

rawdata <- data.frame(
  id = c(1,1,2,2,2,3,3),
  time = c(45,92,30,100,79,199,248)
)

我想根据某些条件为 dt 的整行着色。

  1. 如果 id=1 应该可以申请 backgroundColor = styleInterval(c(30,50), c('#C6EFCE', '#FFEB9C','#FFC7CE'))

  2. 如果 id=2 应该可以申请 backgroundColor = styleInterval(c(45,90), c('#e60000, '#000000','#ffffff))

  3. 如果 id=3 应该可以申请 backgroundColor = styleInterval(c(x,y), c('a, 'b,'c))

请找到我当前的数据表代码

library(DT)
datatable(
  rawdata,
  rownames = FALSE,
  options=list(
    pageLength = 20,
    scrollX = TRUE, 
    dom = 'prlti',
    initComplete =JS(
      "function(settings, json) {",
      "$(this.api().table().header()).css({'font-size': '20px','background-color': '#000', 'color': '#fff'});",
      "}"),
    columnDefs = list(list(className = 'dt-center', targets ="_all")),autowidth=T)
) %>%
  formatStyle(
    colnames(rawdata)[1:NCOL(rawdata)],target = 'row',
    # color =styleInterval(c(30,35), c('black', 'black', 'black')),
    backgroundColor =styleInterval(c(48,120), c('#C6EFCE', '#FFEB9C','#FFC7CE'))

  ) %>%
  formatStyle(columns = c(1:NCOL(rawdata)),'font-size' = '25px',fontWeight = 'Bold')

【问题讨论】:

  • 我编辑了rawdata 可用的代码。我仍然得到Error in name2int(name, names, rownames) 。您可能在第一个 formatStyle() 中有错字 - 这里从 11 开始没有意义
  • 嗯,这不是错字。就像我说的,我只是给你我原始数据表中的一组列。我只是想知道如何处理它的逻辑。

标签: r shiny dt


【解决方案1】:

在这种情况下,我会将你想要的颜色存储在表格本身的每一行中,然后使用styleEqual 来设置每一行的颜色。 (我们还想隐藏包含颜色的列。)

为每一行添加您希望显示的颜色的列:

library(dplyr)
rawdata <- data.frame(
  id = c(1,1,2,2,2,3,3),
  time = c(45,92,30,100,79,199,248)
) %>%
  mutate(row.color = case_when(id == 1 & time <= 30 ~ "#C6EFCE",
                               id == 1 & time <= 50 ~ "#FFEB9C",
                               id == 1 ~ "#FFC7CE",
                               id == 2 & time <= 45 ~ "#E60000",
                               id == 2 & time <= 90 ~ "#000000",
                               id == 2 ~ "#FFFFFF",
                               T ~ "#888888"))

(我使用灰色表示id = 3,因为原始帖子省略了该条件的实际颜色。)

使用styleEqual 设置time 列中的单元格颜色,使用columnDefs 隐藏具有十六进制颜色值的列:

library(DT)
datatable(
  rawdata,
  rownames = FALSE,
  options=list(
    pageLength = 20,
    scrollX = TRUE, 
    dom = 'prlti',
    initComplete =JS(
      "function(settings, json) {",
      "$(this.api().table().header()).css({'font-size': '20px', 'background-color': '#000', 'color': '#fff'});",
      "}"),
    columnDefs = list(list(className = 'dt-center', targets = "_all"),
                      list(targets = 2, visible = F)),
    autowidth = T)
) %>%
  formatStyle(
    c("time"), "row.color",
    backgroundColor = styleEqual(sort(unique(rawdata$row.color)), sort(unique(rawdata$row.color)))

  ) %>%
  formatStyle(columns = c(1:NCOL(rawdata)),'font-size' = '25px',fontWeight = 'Bold')

我们得到以下结果:

如果您想在黑色行中呈现白色文本,您可以使用text.color 添加另一列,并将styleEqualcolor 选项一起使用。

【讨论】:

  • 当我删除“target=row”时,我只能获取指定列的颜色,它似乎不起作用。知道为什么吗?
  • 为什么它没有给我想要的结果?
  • 为了确保我理解 - 您只希望 time 列(可能还有完整数据集中的其他一些列)具有 row.color 指定的颜色?
  • 是的,这正是我要说的
  • 编辑了帖子,只有time 列有颜色;您可以向 c("time") 添加更多列名,以在完整数据集中包含/排除正确的列集。
猜你喜欢
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 2011-04-23
  • 2017-05-20
相关资源
最近更新 更多