在筛选表格中,闪亮的reactable行索引是错误的。

huangapple go评论127阅读模式
英文:

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 &lt;- MASS::Cars93[, 1:7]

ui &lt;- fluidPage(
  actionButton(&quot;select_btn&quot;, &quot;Select rows&quot;),
  selectInput(&quot;filter_type&quot;, &quot;Filter type&quot;, unique(data$Type), multiple = TRUE),
  textInput(&quot;row&quot;, label = &quot;Selected&quot;),
  reactableOutput(&quot;table&quot;)
)

server &lt;- function(session, input, output) {
  selected &lt;- reactive(getReactableState(&quot;table&quot;, &quot;selected&quot;))
  
  output$table &lt;- renderReactable({
    reactable(
      data,
      filterable = TRUE,
      searchable = TRUE,
      selection = &quot;single&quot;,
    )
  })
  
  observeEvent(input$select_btn, {
    # Select rows
    updateReactable(&quot;table&quot;, selected = c(1, 3, 5))
  })

  observe({
    # Filter data
    filtered &lt;- if (length(input$filter_type) &gt; 0) {
      data[data$Type %in% input$filter_type, ]
    } else {
      data
    }
    updateReactable(&quot;table&quot;, data = filtered)
  })
  
  observe({
    updateTextInput(session, &quot;row&quot;,  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)

在筛选表格中,闪亮的reactable行索引是错误的。

英文:

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.

在筛选表格中,闪亮的reactable行索引是错误的。

library(shiny)
library(reactable)

data &lt;- MASS::Cars93[, 1:7]

ui &lt;- fluidPage(
    actionButton(&quot;select_btn&quot;, &quot;Select rows&quot;),
    selectInput(&quot;filter_type&quot;, &quot;Filter type&quot;, unique(data$Type), multiple = TRUE),
    textInput(&quot;row&quot;, label = &quot;Selected&quot;),
    reactableOutput(&quot;table&quot;)
)

server &lt;- function(session, input, output) {
    rv &lt;- reactiveValues(data = data)
    
    data_filtered &lt;- reactive({
        if (length(input$filter_type) &gt; 0) {
            rv$data[rv$data$Type %in% input$filter_type,]
        } else {
            rv$data
        }
    })
    
    selected &lt;- reactiveValues(j = 0)
    
    currentSelected &lt;-
        reactive(getReactableState(&quot;table&quot;, &quot;selected&quot;))
    
    observeEvent(currentSelected(), priority = 0, {
        selected$j &lt;- currentSelected()
    })
    
    output$table &lt;- renderReactable({
        reactable(
            data_filtered(),
            filterable = TRUE,
            searchable = TRUE,
            selection = &quot;single&quot;,
        )
    })
    
    observeEvent(input$select_btn, {
        # Select rows
        updateReactable(&quot;table&quot;, selected = c(1, 3, 5))
    })
    
    observe({
        if (is.null(getReactableState(&quot;table&quot;, &quot;selected&quot;))) {
            updateTextInput(session, &quot;row&quot;, value = &quot;&quot;)
        } else {
            updateTextInput(session, &quot;row&quot;, value = data_filtered()$Model[selected$j])
        }
    })
    
}

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年8月9日 01:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定