R Shiny: How to add data tables to dynamically created tabs

后端 未结 1 809
栀梦
栀梦 2021-01-04 22:47

I am currently trying to create dynamically-created data tables that each have its own tab. The number of tabs is determined by the user. I have used the code from this post

相关标签:
1条回答
  • 2021-01-04 23:24

    To do what you want, you need to add dataTableOutput to your tabPanel as you dynamically generate them, and then you need to dynamically generate the corresponding renderDataTable.

    Do this in your server:

    library(DT) # need datatables package
    server <- shinyServer(function(input, output, session) {
    
      output$mytabs <- renderUI({
        nTabs = length(input$decision)
        # create tabPanel with datatable in it
        myTabs = lapply(seq_len(nTabs), function(i) {
          tabPanel(paste0("dataset_",i),
            DT::dataTableOutput(paste0("datatable_",i))       
                   )
          })
    
        do.call(tabsetPanel, myTabs)
      })
    
      # create datatables
      observe(
        lapply(seq_len(length(input$decision)), function(i) {
          output[[paste0("datatable_",i)]] <- DT::renderDataTable({
            as.data.frame(get(input$decision[i]))
          })
        })  
      )  
    })
    
    0 讨论(0)
提交回复
热议问题