我有一个闪亮的应用程序,它使用可反应来显示任务的状态。状态列的信息采用颜色编码,数据使用以下方式更新updateReactable()
.
最初,颜色背景是正确的(例如红色表示打开,绿色表示完成),但是当我打电话时updateReactable
,背景颜色不变。
一个简单的解决方案是始终重新渲染表格,但我想保留用户输入(例如排序、过滤、页面选择等),因此我使用 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)
点击后看起来像这样:
请注意,所有“完成”字段都应为绿色。