0

I have written the below code to Upload the files and do some calculation after I click on the Go Button. However following code executes loading of files every time even I do not upload the file again. I do not want to load the file again.

**Server.R**

options(shiny.maxRequestSize = 400*1024^2)

shinyServer(function(input, output,session) {

      datasetInput <- reactive({


      progress <- shiny::Progress$new()
      # Make sure it closes when we exit this reactive, even if there's an error
      on.exit(progress$close())

      progress$set(message = "Processing is going on..Kindly wait")

     isolate({
       S <- input$file1
      T <- input$file2

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

      Startrpt<-read.csv(S$datapath, header=TRUE)

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

      Takeoffrpt<-read.csv(T$datapath, header=TRUE)

      Takeoffrpt$TkDates = as.POSIXct(Takeoffrpt$Date,format='%m/%d/%Y %H:%M')
      Startrpt$StDates = as.POSIXct(Startrpt$Date,format='%m/%d/%Y %H:%M')

      # Getting only Dates
      Takeoffrpt$TkDate1 = as.Date(Takeoffrpt$TkDates)
    Startrpt$StDate1=as.Date(Startrpt$StDates)

    # Getting only Time
    Takeoffrpt$TkTime = format(Takeoffrpt$TkDates,'%H:%M')
    Startrpt$StTime=format(Startrpt$StDates,'%H:%M')

    Takeoffrpt$TkMins<-as.numeric(substring(Takeoffrpt$TkTime,1,2))*60+as.numeric(substring(Takeoffrpt$TkTime,4,5))
    Startrpt$StMins<-as.numeric(substring(Startrpt$StTime,1,2))*60+as.numeric(substring(Startrpt$StTime,4,5))

     })
    query<-paste("select tk.*,st.*,tk.Date as Tkdate, st.Date as Stdate, (tk.TkMins-st.StMins) as difference from Takeoffrpt as tk inner join Startrpt as st on tk.ESN=st.ESN and tk.TkDate1=st.StDate1 AND (tk.TkMins-st.StMins) <= ", input$Range," and (tk.TkMins-st.StMins) >= 0",sep="")
    P<-sqldf(query,drv="SQLite")
    P<-subset(P, select=-c(Date,TkDates,TkDate1,TkTime,TkMins,StDates,StDate1,StTime,StMins,difference))
    write.csv(file="output.csv",P)


    #data1<-sqldf("select * from Startrpt",drv="SQLite")
    })

    output$view <- renderDataTable({
      if (input$goButton == 0)
        return()
      isolate(datasetInput())
    }, options = list(lengthMenu = c(5, 10, 15), pageLength = 5))

 })


**ui.R**

    rm(list=ls())  

    packages <- c("sqldf", "shiny")  
    if (length(setdiff(packages, rownames(installed.packages()))) > 0) {  
      install.packages(setdiff(packages, rownames(installed.packages())))    
    }  

    library(shiny)  
    library(sqldf)  

    shinyUI(fluidPage( 
      titlePanel("Uploading Files"), 
      sidebarLayout( 
        sidebarPanel( 
          fileInput('file1', 'Choose your Start Report CSV File', 
                    accept=c('text/csv',  
                                     'text/comma-separated-values,text/plain',  
                                     '.csv')), 

          fileInput('file2', 'Choose your take off Report CSV File', 
                    accept=c('text/csv', 
                             'text/comma-separated-values,text/plain', 
                             '.csv')),


      # Sidebar with a slider input for number of observations



      sliderInput("Range", 
                    "Accepted Minutes difference", 
                    min = 1,
                    max = 60, 
                    value = 1),
      actionButton("goButton", "Go!")
      #submitButton("Apply")

  ),
    mainPanel(
        dataTableOutput("view")
    )
  )
))
Surya
  • 1
  • 3

1 Answers1

0

Surya,

  1. first an aside take a look at point 2 in my answer around Importing and accessing large data files in Shiny.

    We found it helpful to load data in a global.R file that sits in same directory as your ui.R and server.R files.

    This may not work well as your app is needing the user to select a file, but it may help thin out some of your code. See this scoping explanation for R Shiny on how global works.

  2. In your specific case, is the isolate inside your reactive a problem?

    I am still getting to grips with Shiny but take a look at this answer which makes use of a conditional panel and setupComplete condition. And @daattali answer below that with shinyjs may give you ideas on how to better place your reactives/observe, or provide a workaround.

All the best.

Community
  • 1
  • 1
micstr
  • 5,080
  • 8
  • 48
  • 76