微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何根据另一列中的单元格在闪亮的 DT::dataTable 中更改单元格的颜色?

如何解决如何根据另一列中的单元格在闪亮的 DT::dataTable 中更改单元格的颜色?

我是 Shiny/r 的新手,并试图根据另一个单元格的值更改单元格(DT 表)的背景颜色。我尝试使用 https://rstudio.github.io/DT/010-style.html

中的这个示例
datatable(df) %>% formatStyle(
  'V1','V6',backgroundColor = styleEqual(c(0,1),c('gray','yellow'))
)

但不知何故它似乎对我不起作用

这是我的代码

dt_output = function(title,id) {
        fluidRow(column(
        12,h1(paste0(title)),hr(),DTOutput(id)
        ))
        }

 render_dt = function(data,editable = 'cell',server = TRUE,...) {
    renderDT(data,selection = 'none',server = server,editable = editable,...)
    }

ui = fluidPage(
downloadButton("mcp_csv","Download in CSV",class="but"),dt_output('CantiLever/Membrane Probe Report','x9'),)

server = function(input,output,session) {
d1 = readRDS("cmp.rds")
d9 = d1

output$x9 = render_dt(d9,'cell',rownames = FALSE,extensions = 'Buttons',options = list(dom = 'Bfrtip',buttons = I('colvis')))
    
observeEvent(input$x9_cell_edit,{
d9 <<- editData(d9,input$x9_cell_edit,'x9',rownames = FALSE)
saveRDS(d9,'cmp.rds',version = 2)
})

datatable(d9) %>% formatStyle(
'R/Y/G','Y',#'R/Y/G' is the column cell I'm trying to change values for based on column 'Y'
backgroundColor = styleEqual(c(0,c('red','yellow'))
)

我不确定我做错了什么。也许它在错误的地方,我不知道。另外,如果我需要根据动态输入(不是像 0 和 1 那样硬编码),基于三个不同的列(R、Y、G)更改“R/Y/G”列的颜色,我将如何实现? 谢谢

附言 如果我添加代码

dt_d9=datatable(d9) %>% formatStyle(
'R/Y/G','yellow'))
)

并替换

output$x9 = render_dt(d9,buttons = I('colvis')))

output$x9 = render_dt(dt_d9,buttons = I('colvis')))

我确实得到了 R/Y/G 列上的颜色,但编辑单元格功能停止工作。可以在此处找到编辑单元格功能https://yihui.shinyapps.io/DT-edit/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。