我有一个shiny 的应用程序,它使用reactable来显示任务的状态.信息为状态列进行 colored颜色 编码,数据使用updateReactable()
更新.
最初,背景 colored颜色 是正确的(例如,红色表示打开,绿色表示完成),但当我调用updateReactable
时,背景 colored颜色 不变.
一个简单的解决方案是始终重新呈现表,但我希望保留用户输入(例如排序、筛选、页面 Select 等),因此我使用updateReactable.
MWE如下所示:
library(shiny)
library(reactable)
ui <- fluidPage(
reactableOutput("table"),
actionButton("go", "Go")
)
d <- data.frame(x = sample(letters, 10, replace = TRUE), status = "Open")
d[1, "status"] <- "Done"
cols_bg <- c("Open" = "red", "Done" = "green")
cols_font <- c("Open" = "black", "Done" = "white")
server <- function(input, output, session) {
# render this only once, update values later
output$table <- renderReactable({
reactable(d,
columns = list(
status = colDef(name = "Status",
style = function(value) {
list(background = cols_bg[[value]],
fontWeight = 600, color = cols_font[[value]])
})
)
)
})
# on button-click replace some values with Done and update table
observeEvent(input$go, {
ids <- sample(nrow(d), 0.7*nrow(d))
d$status[ids] <- "Done"
updateReactable("table", d)
})
}
shinyApp(ui, server)
单击后,看起来像这样:
请注意,所有"完成"字段都应为绿色.