How do I get the data from the selected rows of a filtered datatable (DT)?

后端 未结 2 1883
逝去的感伤
逝去的感伤 2020-12-29 14:19

The DT package allows you to get the indices of selected rows using input$tableID_rows_selected. This works great for tables that do not have filtered data. How

2条回答
  •  独厮守ぢ
    2020-12-29 14:39

    One way: in your filteredTable_selected() function, where you're creating the data you'll put in your fourth DT, use filter(mtcars, cyl == input$filter) like you did for your third table instead of mtcars. This way, the row indices will match.

    If you're worried about performance issues on larger datsets, just filter the data in a reactive expression, which caches its output. This way, you won't filter more than your input$filter value changes.

    server <- function(input, output, session) {
      filteredTable_data <- reactive({
        filter(mtcars, cyl == input$filter)
      })
    
      output$filteredTable <- DT::renderDataTable({
        datatable(
          filteredTable_data(),
          selection = list(mode = "multiple"),
          caption = "Filtered Table (based on cyl)"
        )
      })
    
      filteredTable_selected <- reactive({
        ids <- input$filteredTable_rows_selected
        filteredTable_data()[ids,]
      })
    
      output$filteredTableSelected <- DT::renderDataTable({
        datatable(
          filteredTable_selected(),
          selection = list(mode = "none"),
          caption = "Table that gets data from unfiltered original data"
        )
      })
    }
    

提交回复
热议问题