ChriiSchee
ChriiSchee

Reputation: 699

Reset row selection for DT::renderDataTable() in Shiny R

I reproduced an example shiny app written by Yihui Xie (https://yihui.shinyapps.io/DT-rows/). The app uses DT::renderDataTable() which allows a row selection.

Everything works perfectly fine. I was however wondering if it's possible to reset the row selection (i.e. undo the click selection) ? I already tried it with an action button to reset s = input$x3_rows_selected (see script below).

With my current script, s = input$x3_rows_selected does indeed get emptied, I can however not refill it. Also the selected rows are still clicked (shaded)

Does anyone has an idea? Is there an option within DT::renderDataTable() to reset the selection? Or does anyone has an idea for a workaround?

Example form https://yihui.shinyapps.io/DT-rows/) with my modification (action button):

server.R

library(shiny)
library(DT)

shinyServer(function(input, output, session) {
    
    
    # you must include row names for server-side tables
    # to be able to get the row
    # indices of the selected rows
    mtcars2 = mtcars[, 1:8]
    output$x3 = DT::renderDataTable(mtcars2, rownames = TRUE, server = TRUE)
    
    # print the selected indices
    
    selection <- reactive({
        if (input$resetSelection) 
            vector() else input$x3_rows_selected
    })
    
    output$x4 = renderPrint({
        
        if (length(selection())) {
            cat("These rows were selected:\n\n")
            output <- selection()
            cat(output, sep = "\n")
        }
    })
    
})

ui.R

library(shiny)
shinyUI(
    fluidPage(
        title = 'Select Table Rows',

        h1('A Server-side Table'),

        fluidRow(
            column(9, DT::dataTableOutput('x3')),
            column(3, verbatimTextOutput('x4'),
               actionButton('resetSelection',
                   label = "Click to reset row selection"
                             ) # end of action button

              ) #end of column
)))

Upvotes: 10

Views: 10665

Answers (2)

Yihui Xie
Yihui Xie

Reputation: 30104

In the current development version of DT (>= 0.1.16), you can use the method selectRows() to clear selections. Please see the section "Manipulate An Existing DataTables Instance" in the documentation.

Upvotes: 15

Geovany
Geovany

Reputation: 5677

Here is a possible solution, maybe not the best but it works. It is based on re-create the datatable each time the action button is clicked, so the selected rows are removed.

library(shiny)
library(DT)
runApp(list(
    server = function(input, output, session) {
        mtcars2 = mtcars[, 1:8]
        output$x3 = DT::renderDataTable({
            # to create a new datatable each time the reset button is clicked
            input$resetSelection 
            mtcars2
            }, rownames = TRUE, server = TRUE
        )

        # print the selected indices
        selection <- reactive ({
                input$x3_rows_selected
        })

        output$x4 = renderPrint({
            if (length(selection())) {
                cat('These rows were selected:\n\n')
                output <- selection()
                cat(output, sep = '\n')
            }
        })
    },
    ui = shinyUI(fluidPage(
        title = 'Select Table Rows',
        h1('A Server-side Table'),
        fluidRow(
            column(9, DT::dataTableOutput('x3')),
            column(3, verbatimTextOutput('x4'),
                actionButton(  'resetSelection',label = "Click to reset row selection")
            ) #end of column
        )
    ))
))

Upvotes: 2

Related Questions