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

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

shiny reactable index of rows in filtered table are wrong

问题

我已经使用了一个带有选择行选项的reactable。但是在过滤reactable之后,通过getReactableState("table", "selected")获取的行号(或者说值)是错误的。我犯了什么错误?
在未过滤数据的情况下点击一行时,行号是正确的。但是在过滤后,我得到了错误的值。如何获取过滤表格的正确索引?

  1. library(shiny)
  2. library(reactable)
  3. data <- MASS::Cars93[, 1:7]
  4. ui <- fluidPage(
  5. actionButton("select_btn", "Select rows"),
  6. selectInput("filter_type", "Filter type", unique(data$Type), multiple = TRUE),
  7. textInput("row", label = "Selected"),
  8. reactableOutput("table")
  9. )
  10. server <- function(session, input, output) {
  11. selected <- reactive(getReactableState("table", "selected"))
  12. output$table <- renderReactable({
  13. reactable(
  14. data,
  15. filterable = TRUE,
  16. searchable = TRUE,
  17. selection = "single",
  18. )
  19. })
  20. observeEvent(input$select_btn, {
  21. # Select rows
  22. updateReactable("table", selected = c(1, 3, 5))
  23. })
  24. observe({
  25. # Filter data
  26. filtered <- if (length(input$filter_type) > 0) {
  27. data[data$Type %in% input$filter_type, ]
  28. } else {
  29. data
  30. }
  31. updateReactable("table", data = filtered)
  32. })
  33. observe({
  34. updateTextInput(session, "row", value = data$Model[selected()])
  35. })
  36. }
  37. 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?

  1. library(shiny)
  2. library(reactable)
  3. data &lt;- MASS::Cars93[, 1:7]
  4. ui &lt;- fluidPage(
  5. actionButton(&quot;select_btn&quot;, &quot;Select rows&quot;),
  6. selectInput(&quot;filter_type&quot;, &quot;Filter type&quot;, unique(data$Type), multiple = TRUE),
  7. textInput(&quot;row&quot;, label = &quot;Selected&quot;),
  8. reactableOutput(&quot;table&quot;)
  9. )
  10. server &lt;- function(session, input, output) {
  11. selected &lt;- reactive(getReactableState(&quot;table&quot;, &quot;selected&quot;))
  12. output$table &lt;- renderReactable({
  13. reactable(
  14. data,
  15. filterable = TRUE,
  16. searchable = TRUE,
  17. selection = &quot;single&quot;,
  18. )
  19. })
  20. observeEvent(input$select_btn, {
  21. # Select rows
  22. updateReactable(&quot;table&quot;, selected = c(1, 3, 5))
  23. })
  24. observe({
  25. # Filter data
  26. filtered &lt;- if (length(input$filter_type) &gt; 0) {
  27. data[data$Type %in% input$filter_type, ]
  28. } else {
  29. data
  30. }
  31. updateReactable(&quot;table&quot;, data = filtered)
  32. })
  33. observe({
  34. updateTextInput(session, &quot;row&quot;, value = data$Model[selected()])
  35. })
  36. }
  37. shinyApp(ui, server)

答案1

得分: 1

一种可能性是:将筛选后的数据放在reactive()函数中,并使用data_filtered()更新文本输入。类似地,您可以继续处理所选的值。

  1. library(shiny)
  2. library(reactable)
  3. data <- MASS::Cars93[, 1:7]
  4. ui <- fluidPage(
  5. actionButton("select_btn", "Select rows"),
  6. selectInput("filter_type", "Filter type", unique(data$Type), multiple = TRUE),
  7. textInput("row", label = "Selected"),
  8. reactableOutput("table")
  9. )
  10. server <- function(session, input, output) {
  11. rv <- reactiveValues(data = data)
  12. data_filtered <- reactive({
  13. if (length(input$filter_type) > 0) {
  14. rv$data[rv$data$Type %in% input$filter_type,]
  15. } else {
  16. rv$data
  17. }
  18. })
  19. selected <- reactiveValues(j = 0)
  20. currentSelected <- reactive(getReactableState("table", "selected"))
  21. observeEvent(currentSelected(), priority = 0, {
  22. selected$j <- currentSelected()
  23. })
  24. output$table <- renderReactable({
  25. reactable(
  26. data_filtered(),
  27. filterable = TRUE,
  28. searchable = TRUE,
  29. selection = "single",
  30. )
  31. })
  32. observeEvent(input$select_btn, {
  33. # Select rows
  34. updateReactable("table", selected = c(1, 3, 5))
  35. })
  36. observe({
  37. if (is.null(getReactableState("table", "selected"))) {
  38. updateTextInput(session, "row", value = "")
  39. } else {
  40. updateTextInput(session, "row", value = data_filtered()$Model[selected$j])
  41. }
  42. })
  43. }
  44. 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行索引是错误的。

  1. library(shiny)
  2. library(reactable)
  3. data &lt;- MASS::Cars93[, 1:7]
  4. ui &lt;- fluidPage(
  5. actionButton(&quot;select_btn&quot;, &quot;Select rows&quot;),
  6. selectInput(&quot;filter_type&quot;, &quot;Filter type&quot;, unique(data$Type), multiple = TRUE),
  7. textInput(&quot;row&quot;, label = &quot;Selected&quot;),
  8. reactableOutput(&quot;table&quot;)
  9. )
  10. server &lt;- function(session, input, output) {
  11. rv &lt;- reactiveValues(data = data)
  12. data_filtered &lt;- reactive({
  13. if (length(input$filter_type) &gt; 0) {
  14. rv$data[rv$data$Type %in% input$filter_type,]
  15. } else {
  16. rv$data
  17. }
  18. })
  19. selected &lt;- reactiveValues(j = 0)
  20. currentSelected &lt;-
  21. reactive(getReactableState(&quot;table&quot;, &quot;selected&quot;))
  22. observeEvent(currentSelected(), priority = 0, {
  23. selected$j &lt;- currentSelected()
  24. })
  25. output$table &lt;- renderReactable({
  26. reactable(
  27. data_filtered(),
  28. filterable = TRUE,
  29. searchable = TRUE,
  30. selection = &quot;single&quot;,
  31. )
  32. })
  33. observeEvent(input$select_btn, {
  34. # Select rows
  35. updateReactable(&quot;table&quot;, selected = c(1, 3, 5))
  36. })
  37. observe({
  38. if (is.null(getReactableState(&quot;table&quot;, &quot;selected&quot;))) {
  39. updateTextInput(session, &quot;row&quot;, value = &quot;&quot;)
  40. } else {
  41. updateTextInput(session, &quot;row&quot;, value = data_filtered()$Model[selected$j])
  42. }
  43. })
  44. }
  45. 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:

确定