Search code examples
rshinydashboard

Input 2 tables in R shiny dashboard


I want to have a provision to input 2 tables namely train and test for further analysis in R shiny dashboard.


Solution

  • Here you go. Assuming your files are in a format that you can read with read.table(), this example will be a working start for you.

    You can use fileInput() to input files, this can be any format from .csv, .txt or .RData or any other file type you need.

    You use fileInput("Table1",...) in the UI. And then in the server() function of your app you can access the input files by input$Table1. Specifically, the file is uploaded and saved at a temporary location that you can access with input$Table1$datapath.

    With this file location you should be able to do whatever you want to do with the data that is now uploaded to your shiny App. In the example below I only read the data using read.table() and then show the data in two datatables in the UI.

    ## app.R ##
    library(shiny)
    library(shinydashboard)
    
    # A minimal dashboard page
    #   Sidebar for file inputs
    #   Main body to show 2 datatables
    ui <- dashboardPage(
      dashboardHeader(title = "Two tables for further analysis"),
      dashboardSidebar(
        h2("fileInput() to upload files"),
        fileInput("Table1", "Input file for train data"),
        fileInput("Table2", "Input file for test data")
      ),
      # A body for two datatables to show the data input
      dashboardBody(
        fluidPage(
          fluidRow(
            column(width=6,
                   DT::dataTableOutput("Train")),
            column(width=6,
                   DT::dataTableOutput("Test"))
      )
    )
    ))
    
    server <- function(input, output) {
      output$Train <- DT::renderDataTable({
        # NULL if no input given
        if (is.null(input$Table1)) return(NULL)
    
        # Else... read table (input$Table1 is a list of componenent - the 'datapath' field is the temp file location)
        read.table(input$Table1$datapath) # Assuming you have a file to read with read.table()
      })
    
      # Using output$Test - we can use datatableOutput("Test") to show the datatable in the UI
      output$Test <- DT::renderDataTable({
        # NULL if no input given
        # 
        if (is.null(input$Table2)) return(NULL)
        # Else... read table (input$Table1 is a list of componenent - the 'datapath' field is the temp file location)
        read.table(input$Table2$datapath) # Assuming you have a file to read with read.table()
      })
    
      # You can of course read the data for your own purpose
      #   Not for a datatable, but to do follow-up analysis
      #   
      #   Further, you can also upload .RData files for example - or any other file.
    }
    
    # Run the shiny app
    shinyApp(ui, server)
    

    Please be a little more specific next time and give an example of what you have already tried. I have no clue what type of data format you are trying to work with... Hope this helps!