我有一个R数据表,我使用Shiny将其渲染到仪表板中 .

我想根据另一列的值为列着色 . 按照示例我看到in the DT documentation我可以根据每个单元格中的值为列着色:

library(DT)
options(DT.options = list(pageLength = 5))
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))
# style V6 based on values of V6
datatable(df) %>% formatStyle(
  'V6',
  backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)

根据其值对单个列着色按预期方式工作:

Single column example

同一文档中的另一个例子声称允许我们根据列V6的值对列V1进行着色,并隐藏显示的V6(这正是我想要做的):

# hide V6
output$table1 <- DT::renderDataTable({

  datatable(df, options = list(
      columnDefs = list(list(targets = 6, visible = FALSE))
  )) %>% formatStyle(
    'V1', 'V6',
    backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
  )
})

但是 - 当我在Shiny中使用renderDataTable并且我看到没有颜色的列时(虽然它确实隐藏了列V6),这不起作用:

Non-working example.

我进一步探索,我可以看到styleEqual忽略了我要求它查看V6并试图将styleEqual约束应用于V1的事实 . 如果我将此行更改为 backgroundColor = styleInterval(0, c('gray', 'yellow')) ,那么它将根据V1中的值为V1着色(并且V6参数似乎完全被忽略) .