I am trying to view 2 .csv files side by side in shiny app . But using attached logic i am unable to find the solution. Please help me out!

library(shiny)
library(dplyr)

## Only run examples in interactive R sessions
if (interactive()){
  
  ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        
        fileInput(
          "file1",
          "Y:/MyData1.csv",
          accept = c(
            "text/csv",
            "text/comma-separated-values,text/plain",
            ".csv"
          )
        ),
        
        fileInput(
          "file2",
          "Y:/MyData2.csv",
          accept = c(
            "text/csv",
            "text/comma-separated-values,text/plain",
            ".csv"
          )
        ),
        
        tags$hr(),
        
        checkboxInput("header", "Header", TRUE)
        
      ),
      
      mainPanel(tableOutput("contents"))
      
    ))
    
    server <- function(input, output) {
      
      observe({
        file1 = input$file1
        file2 = input$file2
        if (is.null(file1) || is.null(file2)) {
          return(NULL)
        }
        data1 = read.csv(file1$datapath)
        data2 = read.csv(file2$datapath)
      })
    }
    
    shinyApp(ui, server)
}

I am looking for a solution to browse 2 different CSV files in shiny app with 2 different panels one for file1 and one for file 2 ?

Please help me out .. thanks in advance

Rather than use observe(), it's probably better to use a reactive() expression for each file. observe() is for triggering side effects, which you don't want to do, you want to create/assign a value (i.e. the data) and then do something with it. later for that reactive() is a better choice.

If all you want to do is display the data, you can bypass the use of reactive() and go straight to reading in the file and rendering it as a table (getting it ready for display in the UI).

So your server logic should be amended to be something like that found in the documentation:

server<-(function(input, output) {
# Read in the first file and create a table output
output$data1 <- renderTable({
  inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath)
  })
}

This is reading in the data and then creating an output object for you to display in the UI. You can then show it in the ui by changing the code in mainPanel() to:

mainPanel(
    tableOutput("data1")
)

You can do something similar for the second file and add an extra tableOutput() in the mainPanel() to have the two data sets displayed side-by-side.

Hi Jim,
Very helpful and thanks you so much .. I will give a try with your solution and I will update you ... Once again thanks a lot

Hi Jim,
i managed to get the output using below logic . But file-1 output and file-2 output are view-able one after the other
example :
file1
1
2
3
file2
1
2
3
but i want to view the file 1 and file 2 side by side . Please is there any solution to fix this problem let me know ?
example:
file 1 file 2
1 1
2 2
3 3

Thank Q so much.

=================================================================================
if (interactive()) {

ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File1",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
fileInput("file2", "Choose CSV File2",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),

    checkboxInput("header", "Header", TRUE)
  ),
  mainPanel(
    tableOutput("data1"),
    tableOutput("data2")
    
  )
)

)

server <- function(input, output) {
output$data1 <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1

  if (is.null(inFile))
    return(NULL)
  
  read.csv(inFile$datapath)
})

output$data2 <- renderTable({
  # input$file1 will be NULL initially. After the user selects
  # and uploads a file, it will be a data frame with 'name',
  # 'size', 'type', and 'datapath' columns. The 'datapath'
  # column will contain the local filenames where the data can
  # be found.
  inFile <- input$file2
  
  if (is.null(inFile))
    return(NULL)
  
  read.csv(inFile$datapath)
})

}

shinyApp(ui, server)
}

Sounds like you need to adjust the UI side of your code. Using something like fluidRow() in conjunction with tableOutput() should do the trick. Of course, you'll need to use renderTable() or some similar server-side.

I would also recommend taking a look at the shiny application layout guide here:

https://shiny.rstudio.com/articles/layout-guide.html

2 Likes