2

I'd like to add one more row to the answer posted here:

Determine if DT datatable is clicked in shiny app

Specifically I want to return the "a" field of the row that is clicked so I added

var a = table.rows[row_].cells[col].innerhtml 

but nothing happens.

Here is the code. Is it possible?

library(shiny)
library(DT)
runApp(shinyApp(
  ui = fluidPage(DT::dataTableOutput('table')),
  server = function(input, output, session) {
    output$table <- DT::renderDataTable({
      datatable(data.frame(a = c(1,2),b=c(2,3)), rownames = FALSE, selection = 'none', callback = JS("table.on('click.dt', 'td', function() {
            var row_=table.cell(this).index().row;
            var col=table.cell(this).index().column;
            var rnd= Math.random();
            var a = table.rows[row_].cells[col].innerhtml  
            var data = [row_, col, rnd,a];
           Shiny.onInputChange('rows',data );
    });")
      )}
    )

    observeEvent(input$rows, {


      print(input$rows)
      #print(Sys.time())

    })}
))

Thank you.

1 Answer 1

3

If you want to look at clicks from rows, you could use the following callback function:

callback=JS("table.on('click.dt', 'tr', function() {
    var data=table.row(this).data();
    Shiny.onInputChange('rows',data[0]);
});")

This looks at click events on row (tr) and gets the data directly then returns the value in the first column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.