英文:
shiny reactable index of rows in filtered table are wrong
问题
我已经使用了一个带有选择行选项的reactable。但是在过滤reactable之后,通过getReactableState("table", "selected")获取的行号(或者说值)是错误的。我犯了什么错误?
在未过滤数据的情况下点击一行时,行号是正确的。但是在过滤后,我得到了错误的值。如何获取过滤表格的正确索引?
library(shiny)
library(reactable)
data <- MASS::Cars93[, 1:7]
ui <- fluidPage(
actionButton("select_btn", "Select rows"),
selectInput("filter_type", "Filter type", unique(data$Type), multiple = TRUE),
textInput("row", label = "Selected"),
reactableOutput("table")
)
server <- function(session, input, output) {
selected <- reactive(getReactableState("table", "selected"))
output$table <- renderReactable({
reactable(
data,
filterable = TRUE,
searchable = TRUE,
selection = "single",
)
})
observeEvent(input$select_btn, {
# Select rows
updateReactable("table", selected = c(1, 3, 5))
})
observe({
# Filter data
filtered <- if (length(input$filter_type) > 0) {
data[data$Type %in% input$filter_type, ]
} else {
data
}
updateReactable("table", data = filtered)
})
observe({
updateTextInput(session, "row", value = data$Model[selected()])
})
}
shinyApp(ui, server)
英文:
I have used a reactable with the option for select rows. But after filtering the reactable the row-numbers (respective the values) get from getReactableState("table", "selected") are wrong. Where is my mistake?
By clicking on a row with unfiltered data, the row-numbers are correct. After filtering I get wrong values. How can I get the correct indexes of a filtered table?
library(shiny)
library(reactable)
data <- MASS::Cars93[, 1:7]
ui <- fluidPage(
actionButton("select_btn", "Select rows"),
selectInput("filter_type", "Filter type", unique(data$Type), multiple = TRUE),
textInput("row", label = "Selected"),
reactableOutput("table")
)
server <- function(session, input, output) {
selected <- reactive(getReactableState("table", "selected"))
output$table <- renderReactable({
reactable(
data,
filterable = TRUE,
searchable = TRUE,
selection = "single",
)
})
observeEvent(input$select_btn, {
# Select rows
updateReactable("table", selected = c(1, 3, 5))
})
observe({
# Filter data
filtered <- if (length(input$filter_type) > 0) {
data[data$Type %in% input$filter_type, ]
} else {
data
}
updateReactable("table", data = filtered)
})
observe({
updateTextInput(session, "row", value = data$Model[selected()])
})
}
shinyApp(ui, server)
答案1
得分: 1
一种可能性是:将筛选后的数据放在reactive()
函数中,并使用data_filtered()
更新文本输入。类似地,您可以继续处理所选的值。
library(shiny)
library(reactable)
data <- MASS::Cars93[, 1:7]
ui <- fluidPage(
actionButton("select_btn", "Select rows"),
selectInput("filter_type", "Filter type", unique(data$Type), multiple = TRUE),
textInput("row", label = "Selected"),
reactableOutput("table")
)
server <- function(session, input, output) {
rv <- reactiveValues(data = data)
data_filtered <- reactive({
if (length(input$filter_type) > 0) {
rv$data[rv$data$Type %in% input$filter_type,]
} else {
rv$data
}
})
selected <- reactiveValues(j = 0)
currentSelected <- reactive(getReactableState("table", "selected"))
observeEvent(currentSelected(), priority = 0, {
selected$j <- currentSelected()
})
output$table <- renderReactable({
reactable(
data_filtered(),
filterable = TRUE,
searchable = TRUE,
selection = "single",
)
})
observeEvent(input$select_btn, {
# Select rows
updateReactable("table", selected = c(1, 3, 5))
})
observe({
if (is.null(getReactableState("table", "selected"))) {
updateTextInput(session, "row", value = "")
} else {
updateTextInput(session, "row", value = data_filtered()$Model[selected$j])
}
})
}
shinyApp(ui, server)
英文:
One possibility: Put your filtered data inside a reactive()
and update the text input using data_filtered()
. Similarly you can go on with the selected values.
library(shiny)
library(reactable)
data <- MASS::Cars93[, 1:7]
ui <- fluidPage(
actionButton("select_btn", "Select rows"),
selectInput("filter_type", "Filter type", unique(data$Type), multiple = TRUE),
textInput("row", label = "Selected"),
reactableOutput("table")
)
server <- function(session, input, output) {
rv <- reactiveValues(data = data)
data_filtered <- reactive({
if (length(input$filter_type) > 0) {
rv$data[rv$data$Type %in% input$filter_type,]
} else {
rv$data
}
})
selected <- reactiveValues(j = 0)
currentSelected <-
reactive(getReactableState("table", "selected"))
observeEvent(currentSelected(), priority = 0, {
selected$j <- currentSelected()
})
output$table <- renderReactable({
reactable(
data_filtered(),
filterable = TRUE,
searchable = TRUE,
selection = "single",
)
})
observeEvent(input$select_btn, {
# Select rows
updateReactable("table", selected = c(1, 3, 5))
})
observe({
if (is.null(getReactableState("table", "selected"))) {
updateTextInput(session, "row", value = "")
} else {
updateTextInput(session, "row", value = data_filtered()$Model[selected$j])
}
})
}
shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论