【发布时间】: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