Chris Beeley
Chris Beeley

Reputation: 601

Accessing row clicks in data table in Shiny modal

This is cross-posted from here (https://community.rstudio.com/t/accessing-row-clicks-in-data-table-in-modal/8961), no replies yet.

I'm drawing a data table using the DT package in Shiny inside a modal. This is great for my UI, but I want users to be able to click it and access which row they clicked. The usual input$tablename_rows_clicked won't work, because it isn't given a slot in the UI.

Is there a clever DT type way of doing this? Or is there maybe a clever JavaScript way? Anyone any suggestions?

Thanks!

Upvotes: 1

Views: 1895

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

You can do something like this:

library(DT)
library(shiny)

ui <- fluidPage(
  actionButton("Submit","Submit")
)

server <- function(input, output, session) {

  output$Table <- renderDataTable({datatable(mtcars, selection = 'single')})

  Clicked <- eventReactive(input$Table_rows_selected,{
    input$Table_rows_selected
  })

  output$selected <- renderText({paste0("You Selected Row: ",Clicked())})

  observeEvent(input$Submit,{
    showModal(modalDialog( h2("Row Selection Example"),DT::dataTableOutput('Table'),size = "l",br(),textOutput("selected")))
  })
}

shinyApp(ui, server)

enter image description here

Upvotes: 5

Related Questions