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

孤者浪人 提交于 2021-02-07 18:16:31

问题


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)

回答1:


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!



来源:https://stackoverflow.com/questions/45151436/shiny-datatable-popup-data-about-selected-row-in-a-new-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!