Reputation: 6778
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.
Upvotes: 0
Views: 460
Reputation: 10473
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?
Upvotes: 1