Get Selected Row From DataTable in Shiny App

evolvedmicrobe picture evolvedmicrobe · Feb 2, 2015 · Viewed 37.5k times · Source

I want to modify this application:

https://demo.shinyapps.io/029-row-selection/

so that only one row can be selected at a time, and so that I can acquire the item in the first column of the selected row to plot data with. Does anyone know how to do this?

Answer

NicE picture NicE · Feb 2, 2015

UPDATE: you can now access the selected rows using input$tableId_rows_selected in server.R. See here for more details.

To select a unique row, you can change the callback function of your example to this:

callback = "function(table) {
      table.on('click.dt', 'tr', function() {
            table.$('tr.selected').removeClass('selected');
            $(this).toggleClass('selected');            
        Shiny.onInputChange('rows',
                            table.rows('.selected').data()[0][0]);
      });
    }"

When you click on a row,it basically removes any selected rows (they have the .selected class) and selects the row you clicked on.

I also changed the code in the Shiny.onInputChange function so that it returns the number in the first column.