【问题标题】:How to change color of cells in a DT::dataTable in shiny, based on cells in another column?如何根据另一列中的单元格在闪亮的 DT::dataTable 中更改单元格的颜色?
【发布时间】:2021-10-02 09:29:29
【问题描述】:

我是 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('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, 1), c('red', 'yellow'))
)

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

附: 如果我添加此代码

dt_d9=datatable(d9) %>% formatStyle(
'R/Y/G', 'Y',
backgroundColor = styleEqual(c(0, 1), c('red', 'yellow'))
)

替换

output$x9 = render_dt(d9, 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis')))

output$x9 = render_dt(dt_d9, 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis')))

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

【问题讨论】:

    标签: r shiny dt rstudio-server


    【解决方案1】:

    您的代码给出以下错误:

    当 expr 生成数据表对象时,renderDataTable 会忽略 ... 参数;

    因为您现在将 DT::datatable 对象返回给 renderDT 而不是数据框。这有点类似于this Q/A。所以现在你必须将所有参数移动到 DT::datatable 构造函数:

    render_dt = function(data) {
     renderDT(data)
    }
    

      dt_d9 <- datatable(d9, editable = 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis'))) %>% 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, 1), c('red', 'yellow'))
     )
    
     output$x9 = render_dt(dt_d9)
    

    【讨论】:

    • 我正在尝试在“R”、“Y”或“G”列的值发生更改后立即更改“R/Y/G”列的单元格颜色。颜色会发生变化,但是仅当我编辑单元格值并关闭应用程序并再次重新打开它时。但是一旦我编辑单元格值,它就不会改变。这就是我正在做的事情:
    • d9$tcolor 2500000, 2, ifelse(d9$Y > 2000000 & d9$Y % formatStyle( 'R/Y/G', 'tcolor', backgroundColor = styleEqual(c(0,1,2), c('yellow', 'green', 'red')),fontWeight = 'bold' ) 输出$x9 = render_dt(dt_d9)
    • 成功了,谢谢。必须删除这个 d9$tcolor
    • 'R/Y/G' 列有红色、绿色和黄色三种颜色。如何在单元格颜色从绿色变为黄色或从黄色变为红色时设置电子邮件提醒?
    猜你喜欢
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2020-12-02
    • 2021-10-08
    • 2023-02-07
    • 1970-01-01
    相关资源
    最近更新 更多