我有这个闪亮的应用程序,它显示一个可反应的大桌子。我想更改外观,以便所选行的颜色与表的其余部分不同。下面的代码有效。然而,每次我选择一行时,
rowStyle
似乎都会触发表格的重新渲染。有没有办法突出显示所选行而不重新渲染表格?
library(shiny)
library(reactable)
library(nycflights13)
library(dplyr)
ui <- fluidPage(
reactableOutput("table"),
verbatimTextOutput("selectedRowsText")
)
server <- function(input, output, session) {
data <- nycflights13::flights |> filter(month == 1)
selected <- reactive(getReactableState("table", "selected"))
output$table <- renderReactable({
reactable(
data,
selection = "multiple",
onClick = "select",
rowStyle = function(index) {
if (index %in% selected()) {
list(backgroundColor = "#c0d6e4", color = "#000")
}
}
)
})
}
shinyApp(ui, server)
相反,使用reactableTheme来更新样式。
server <- function(input, output, session) {
data <- nycflights13::flights |> filter(month == 1)
selected <- reactive(getReactableState("table", "selected"))
output$table <- renderReactable({
reactable(
data,
selection = "multiple",
onClick = "select",
theme = reactableTheme(
rowSelectedStyle=list(backgroundColor = "#c0d6e4", color = "#000")
)
)
})
}
``