Select DT rows when input IDs are unknown

980 views
Skip to first unread message

Isam Haddad

unread,
Aug 4, 2015, 4:28:04 AM8/4/15
to Shiny - Web Framework for R
Hey there,

I create an unknown number of datatables dynamically and encapsulate them in a tagList which is rendered by renderUI. Using this approach, the outputIDs of the tables are unknown. How can I then access the selected rows?

Typically this would work:

server = function(input, output) {
  output$tbl
<- renderDataTable(
    DT
::datatable(data.frame("data"))
 
)
  observeEvent
({
input$tbl_rows_selected },
   
...
 
)
})

However, In my case I have no idea how to achieve this. Here is a minimal example:

shinyApp(
  ui
=fluidPage(
    mainPanel
(
      uiOutput
("tabs"),
      dataTableOutput
("you_need_me")
   
)
 
),
  server
=function(input, output) {
    output$tabs
<- renderUI({
      tables
<- lapply( list("dt1", "dt2", "dt3"), function(x){
        renderDataTable
( DT::datatable( data.frame( "data" )))
     
})
      tagList
(tables)
   
})
   
    observeEvent
({
     
# this does not work since the ids are unkown
      input$dt1_rows_selected
      input$dt2_rows_selected
      input$dt3_rows_selected
   
},{
     
# ... do something with the selected row
   
})
 
})
)



Any help is appreciated.
Thanks, Isam


 

Yihui Xie

unread,
Aug 4, 2015, 5:05:48 PM8/4/15
to Isam Haddad, Shiny - Web Framework for R
You are supposed to use UI functions (e.g. dataTableOutput()) inside
renderUI(). I don't understand what it means to use renderDataTable()
in renderUI(). renderDataTable() is supposed to be assigned to
output$tableId (where tableId may be dt1, dt2, dt3 in your case).

Regards,
Yihui

Yihui Xie

unread,
Aug 4, 2015, 6:43:52 PM8/4/15
to Isam Haddad, Shiny - Web Framework for R
Just for the record: http://stackoverflow.com/q/31787709/559676

Regards,
Yihui

Isam Haddad

unread,
Aug 4, 2015, 8:36:43 PM8/4/15
to Shiny - Web Framework for R, mail.i...@gmail.com
Thank you so much for clarifying and pointing me into the right direction. The example I showed was influenced by another problem I had earlier and which was answered here: https://stackoverflow.com/questions/31749339/reactivity-of-dynamic-number-of-ui-elements

I managed to solve both problems with the following solution:

shinyApp(
  ui = fluidPage(
    mainPanel(
      uiOutput("tabs")
    )
  ),
  server = function(input, output) {
    output$tabs <- renderUI({
      tables <- lapply( list("dt1", "dt2", "dt3"), function(x){
        local({
          output[[x]] <- renderDataTable( DT::datatable( data.frame("data") ) )
        })
        observeEvent({
          input[[paste( x, "rows_selected", sep="_" )]]
        },{
          print( paste( x, ":", input[[paste( x, "rows_selected", sep="_" )]] ) )
        })
        dataTableOutput( x )
      })
      tagList(tables)
    })
  }
)

If no one sees another problem with this code, for completeness sake I'd answer both questions on SO accordingly.

Thanks!

Yihui Xie

unread,
Aug 4, 2015, 8:55:18 PM8/4/15
to Isam Haddad, Shiny - Web Framework for R
Thanks for the solution! I'd suggest you move renderDataTable() and
observeEvent() out of renderUI(), although in this particular case it
probably does not matter:

library(shiny)
shinyApp(
ui = fluidPage(
mainPanel(
uiOutput("tabs")
)
),
server = function(input, output) {
output$tabs <- renderUI({
tables <- lapply( list("dt1", "dt2", "dt3"), function(x){
DT::dataTableOutput( x )
})
tagList(tables)
})
lapply( list("dt1", "dt2", "dt3"), function(x) {
output[[x]] <- DT::renderDataTable( DT::datatable( data.frame("data") ) )
observeEvent({
input[[paste( x, "rows_selected", sep="_" )]]
},{
print( paste( x, ":", input[[paste( x, "rows_selected", sep="_" )]] ) )
})
})
}
)

Regards,
Yihui
> --
> You received this message because you are subscribed to the Google Groups
> "Shiny - Web Framework for R" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shiny-discus...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/shiny-discuss/a018dcd4-f39a-49dc-92fa-3331eb265cb5%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Isam Haddad

unread,
Aug 4, 2015, 9:05:26 PM8/4/15
to Shiny - Web Framework for R, mail.i...@gmail.com
But this brings me back to my original problem: list("dt1", "dt2", "dt3") changes dynamically. If I move the lapply over this list to the top level of the server function I'll get:

Error in .getReactiveEnvironment()$currentContext() :
 
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Any idea how to prevent this else?

Yihui Xie

unread,
Aug 4, 2015, 9:23:19 PM8/4/15
to Isam Haddad, Shiny - Web Framework for R
Okay, in that case, I think your original solution makes sense.

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages