0

Let's say I have the following app:

library(shiny)
library(DT)

ui <- fluidPage(
  fluidRow(columns = 12, 
           DT::dataTableOutput("my_table"),
           actionButton("resolve", "Resolve Names"))
)

server <- function(input, output) {
  output$my_table <- DT::renderDataTable({
    dat <- data.frame("Names" = c("Bryan", "Byran", "Allison", "Alison"))
    return(DT::datatable(dat))
  })

  observeEvent(input$resolve, {
    # do some stuff using the selected rows from DT
  })
}

shinyApp(ui = ui, server = server)

I want to be able to click on "Bryan" and "Byran" in the table and then click the "Resolve" button and have Shiny make those two values be equal to each other. I don't need help with how to make the values equal to each other, I just need to know how I can capture the data from the DT and return it to the server side to be processed.

More specifically, is there a way to pass JS values back to the server? Because I know I can get the data with JQuery using something like (note, this code might not actually be a working example, but should get the point across):

$('#resolve').click(function(){
  var cells = new Array();
  $('#my_table tr').each(function(){
  if ($(this).attr("class")[1] === "selected") {
    cells.push($(this).html());
});

I've seen a couple other questions on SO about this (here, here, and here), but I can't seem to get their answers to work for me. It looks like I just need to use a callback in the datatable function, but I don't want it to trigger unless the "Resolve" button is pushed.

Any help/insight is appreciated.

Community
  • 1
  • 1
tblznbits
  • 6,602
  • 6
  • 36
  • 66
  • On this page, they talk about row selection: https://rstudio.github.io/DT/shiny.html and also have an example here: https://yihui.shinyapps.io/DT-rows/. Is this what you are looking for? – Gopala Jan 11 '16 at 16:22

1 Answers1

1

On this page, they talk about row selection: rstudio.github.io/DT/shiny.html and also have an example here: yihui.shinyapps.io/DT-rows. Is this what you are looking for?

Gopala
  • 10,363
  • 7
  • 45
  • 77
  • Perfect! It looks like `datatable` automatically captures information about what rows are selected. You can access this with `input$tableID_rows_selected`, assuming that your table is called "tableID". Thanks for the help! – tblznbits Jan 11 '16 at 17:16