How can I add multiple data tables in Shiny , having them be selected with a dropdown menu?

Hi everyone,

I've got a very basic data table in Shiny, acheived using just a few lines (followed from a tutorial I found). The associated csv file is called DATASET1.csv (code is pasted below).

The output is a table that looks like this, which is just what I wanted :

However, I need to now integrate DATASET2.csv and DATASET3.csv. See where the dropdown menu is prompting to show 10 (15/25/etc) entries? I'm looking for a way to have another dropdown menu beside it that allows for selection of DATASET1, DATASET2, or DATASET3.

Any ideas?

Thanks!

Code for DATASET1 :

...library(shiny) #  Shiny web app
library(DT)    #  for data tables

# user interface just shows the table
ui <- fluidPage(fluidRow(column(12, div(dataTableOutput("dataTable")))))

# server is where all calculations are done, tables are pre-rendered
server <- function(input, output, session) {

  # load CSV file
  myCSV <- read.csv('DATASET1.csv')

  #-----------------------------------------------------------------------------
  #  render data table
  #-----------------------------------------------------------------------------

  output$dataTable <- renderDT(
    myCSV, # data
    class = "display nowrap compact", # style
    filter = "top" # location of column filters
  )

}

# run the app
shinyApp(ui, server)

Here is a solution with hard coded file names. The drop down list to choose the file is not part of the table but is in a row on its own row above the table. I do not know how to integrate a widget into the table. You could also allow the user to select a file with a fileInput widget.

library(shiny) #  Shiny web app
library(DT)    #  for data tables

# user interface just shows the table
ui <- fluidPage(fluidRow(selectInput("TheFile", "File", 
                                     choices = c("csv1.csv", "csv2.csv", "Dummy.csv"))),
                fluidRow(column(12, div(dataTableOutput("dataTable")))))

# server is where all calculations are done, tables are pre-rendered
server <- function(input, output, session) {
  
  # load CSV file
  myCSV <- reactive({
    read.csv(input$TheFile)
  })
  #-----------------------------------------------------------------------------
  #  render data table
  #-----------------------------------------------------------------------------
  
  output$dataTable <- renderDT(
    myCSV(), # data
    class = "display nowrap compact", # style
    filter = "top" # location of column filters
  )
  
}

# run the app
shinyApp(ui, server)

This exactly answered my question, and the implementation worked perfectly. Thanks for taking the time to comment!

Thanks for the help! I actually had to try and merge this with a tutorial I was following in order to get the 'copy' and 'download' buttons working correctly. Now I'm happy with the result but I've lost the 'server'/action part of the drop down menu...at least I think that's the problem. Below is the code I have now, where I incorporated your comments to get the dropdown of choosing "DATASET1",2,3. But I couldn't get the second part to integrate correctly in the server section - aka nothing happens when you choose a different dataset. Here's the code now, with only the dropdown part in the 'ui' section :

library(shiny)
library( DT )

# Define UI for application that creates a datatables
ui <- fluidPage(fluidRow(selectInput("TheFile", "Select Cohort", 
                                     choices = c("DATASET1.csv", "DATASET2.csv", "DATASET3.csv"))),
                fluidRow(column(12, div(dataTableOutput("dataTable")))),
   
   # Application title
   titlePanel("Download Datatable")
   
   # Show a plot of the generated distribution
   , mainPanel(
         DT::dataTableOutput("fancyTable")
      ) # end of main panel
   
) # end of fluid page

# Define server logic required to create datatable
server <- function(input, output, session) {

myCSV <- reactive({
    read.csv(input$TheFile)
  })
  
  
  
   output$fancyTable <- DT::renderDataTable(
     datatable( data = read.csv("DATASET1.csv")
                , extensions = 'Buttons'
                , options = list( 
                  dom = "Blfrtip"
                  , buttons = 
                    list("copy", list(
                      extend = "collection"
                      , buttons = c("csv", "excel", "pdf")
                      , text = "Download"
                    ) ) # end of buttons customization
                   
                   # customize the length menu
                  , lengthMenu = list( c(10, 20, -1) # declare values
                                       , c(10, 20, "All") # declare titles
                  ) # end of lengthMenu customization
                  , pageLength = 10
                   
                   
                ) # end of options
               
     ) # end of datatables
   )
} # end of server

# Run the application 
shinyApp(ui = ui, server = server)

Try replacing the above part of your call to datatable() with

data = myCSV()

I wasn't doing the parenthesis () - so I was doing it like this
data = myCSV

the parenthesis made it work. thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.