Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny datatable: popup data about selected row in a new window

I have a datatable in shiny. When a user selects a certain row, I want to display some other data based on the selected row in a new window. I tried to use shinyBS package but I could not use it without action button and I don't want to include action button. I want the pop up to display when a row is selected. Any ideas?

mymtcars = head(mtcars)
for_pop_up = 1:6

app <- shinyApp(
  ui = fluidPage(

  DT::dataTableOutput("mydatatable")
   ),


 server =  shinyServer(function(input, output, session) {

   mycars = head(mtcars)
   output$mydatatable = DT::renderDataTable(mycars, selection = 'single',  
                              rownames = FALSE, options = list(dom = 't'))

output$popup = renderPrint({
  for_pop_up[input$mydatatable_rows_selected]
  })


 })
)

runApp(app)
like image 499
Fisseha Berhane Avatar asked Jan 29 '23 23:01

Fisseha Berhane


1 Answers

You could use an observeEvent and a modal dialog, like this:

mymtcars = head(mtcars)
for_pop_up = 1:6

app <- shinyApp(
  ui = fluidPage(

    DT::dataTableOutput("mydatatable")
  ),


  server =  shinyServer(function(input, output, session) {

    mycars = head(mtcars)
    output$mydatatable = DT::renderDataTable(mycars, selection = 'single',  
                                             rownames = FALSE, options = list(dom = 't'))

    observeEvent(input$mydatatable_rows_selected,
                 {
                   showModal(modalDialog(
                     title = "You have selected a row!",
                     mycars[input$mydatatable_rows_selected,]
                   ))
    })



  })
)

Hope this helps!

like image 136
Florian Avatar answered Feb 03 '23 05:02

Florian