Search code examples
rshinyshinydashboardshiny-server

Compare CSV files from two dropdowns Rshiny


How can I create a shiny app with two dropdown menus with csv files from ./data folder then read those Csv and compare differences?

A user selects CSV from two dropdown menus then automatically generates differences

UI.R

library("shiny")

ui <- fluidPage(
  fluidPage(
    titlePanel("Automated Data Dictionary Comparison"),
    sidebarLayout(

      sidebarPanel(

        selectInput(inputId = 'Dic1',
                    label = 'Choose First Data Dictionary:',
                    choices = list.files(path = "./data",
                                         full.names = FALSE,
                                         recursive = FALSE)),
        selectInput(inputId = 'Dic2',
                    label = 'Choose Second Data Dictionary:',
                    choices = list.files(path = "./data",
                                         full.names = FALSE,
                                         recursive = FALSE))
      ),

      mainPanel(
        tableOutput('contents')
      )
    )
  )
)

SERVER.R

Library(shiny)
library(dplyr)

 server <-  function(input, output) {

   dataset <- reactive({
     infile <- input$Dic1

     if (is.null(infile)){
       return(NULL)
     }
     read.csv(infile[[1]])
   })

   output$contents <- renderDataTable({

     #x <- dataset()
     Diff <- render_diff(diff_data(data_ref=input$DIC1, data = input$DIC2),
     Diff
   })

 }

Solution

  • From what I can see here, what you are doing is that you are correctly creating your reactive dataset object dataset (for 1 of your input files though not both), but you are not using this later on, when you want to generate the differences table (which again needs to be a reactive component as it will be generated from 2 reactive ones - dataset1 and dataset2).

    Something like this should do the trick though (wrap it inside the server function):

    # Parse first file
    dataset1 <- reactive({
      infile <- input$Dic1
    
      if (is.null(infile)){
        return(NULL)
      }
      x <- read.csv(infile[[1]])
      x
    })
    # Parse second file
    dataset2 <- reactive({
      infile <- input$Dic2
    
      if (is.null(infile)){
        return(NULL)
      }
      x <- read.csv(infile[[1]])
      x
    })
    # Create comparison table (reactive as both of its elements are reactive)
    diff <- reactive({
      x <- render_diff(diff_data(data_ref=dataset1(), data=dataset2()))
      x
    })
    #Output
    output$contents <- renderDataTable({
      diff()
    })
    

    Check the above and let me know how it goes for you.