10

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 as a framework.

I am able to create the tabs dynamically, but I can't figure out how to add data tables to the tabs. The data tables are determined by user input too. So, for example, say in ui.R, the user has the choice to pick which data sets they want to see:

ui.R

 library(shiny)
 shinyUI(fluidPage(
      titlePanel("Example"),
      sidebarLayout(
           sidebarPanel(
                selectInput("decision", label = "Choose Dataset", 
                            choices = list("mtcars" = "mtcars", 
                                           "iris" = "iris", 
                                           "precip" = "precip", 
                                           "quakes" = "quakes"), 
                            selected = NULL, multiple = TRUE)
           ),
           mainPanel(
                uiOutput('mytabs')
           )
      )
 ))

server.R

 library(shiny)
 library(ggplot2)

 shinyServer(function(input, output, session) {
      output$mytabs <- renderUI({
           nTabs = length(input$decision)
           myTabs = lapply(paste('dataset', 1:nTabs), tabPanel)
           do.call(tabsetPanel, myTabs)
      })
 })

So, I would like the corresponding data sets rendered into data tables under each tab separately.

Thank you in advance for all of the help!

1 Answer 1

15

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]))
      })
    })  
  )  
})
1
  • Great solution. If the data is defined in server instead of a global or build-in data, it's possible to simply add that mandatory () at the end outside of get. For example get(input$decision[i])(). Took me 15 minutes to figure out.
    – Martin
    Oct 15, 2019 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.