这个简单闪亮的应用程序创建一个两栏表格。当我按下 Set Filter 按钮时,我想将第一列 Name 中的 filterValue 设置为 Bob。 目前,该表已显示,但当我按 Set Filter 按钮时没有任何反应。
library(shiny)
library(reactable)
# JavaScript function to set the filter
js <- "
function setReactableFilter(tableId, columnName, value) {
var reactable = Reactable.getReactable(tableId);
if (reactable) {
reactable.setFilter(columnName, value);
}
}
Shiny.addCustomMessageHandler('setFilter', function(message) {
setReactableFilter(message.tableId, message.columnName, message.value);
});
"
# Define UI for application
ui <- fluidPage(
tags$head(tags$script(HTML(js))),
reactableOutput("table"),
actionButton("set_filter", "Set Filter")
)
# Define server logic required to render the reactable
server <- function(input, output, session) {
# Define some sample data
data <- data.frame(
Name = c("John", "Alice", "Bob", "Jane"),
Age = c(30, 25, 35, 40)
)
# Render the reactable
output$table <- renderReactable({
reactable(data, filterable = TRUE)
})
# When button is pressed, set the filter value to 'Bob' for the 'Name' column
observeEvent(input$set_filter, {
print("HELLO")
session$sendCustomMessage("setFilter", list(
tableId = "table",
columnName = "Name",
value = "Bob"
))
})
}
# Run the application
shinyApp(ui = ui, server = server)
我尝试通过按“设置过滤器”按钮将第一列的过滤器值设置为鲍勃。事件已注册但没有任何反应。
library(shiny)
library(reactable)
# JavaScript function to set the filter
js <- "
Shiny.addCustomMessageHandler('setFilter', function(message) {
Reactable.setFilter(message.tableId, message.columnName, message.value);
});
"
# Define UI for application
ui <- fluidPage(
tags$head(tags$script(js)),
reactableOutput("table"),
actionButton("set_filter", "Set Filter")
)
# Define server logic required to render the reactable
server <- function(input, output, session) {
# Define some sample data
data <- data.frame(
Name = c("John", "Alice", "Bob", "Jane"),
Age = c(30, 25, 35, 40)
)
# Render the reactable
output$table <- renderReactable({
reactable(data, filterable = TRUE)
})
# When button is pressed, set the filter value to 'Bob' for the 'Name' column
observeEvent(input$set_filter, {
session$sendCustomMessage("setFilter", list(
tableId = "table",
columnName = "Name",
value = "Bob"
))
})
}
# Run the application
shinyApp(ui = ui, server = server)