2016-06-08 69 views
0

我已經編寫了下面的代碼來上傳文件並在點擊Go按鈕後進行一些計算。但是,下面的代碼每次都會執行文件加載,即使我沒有再次上傳文件。我不想再次加載文件。隔離文件的加載

**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") 
    ) 
) 
)) 

回答

0

蘇里亞,

  1. 第一旁白採取my answer around Importing and accessing large data files in Shiny看看點2。

    我們發現將文件加載到位於與您的ui.Rserver.R文件相同的目錄中的global.R文件中很有用。

    這可能無法正常工作,因爲您的應用程序需要用戶選擇文件,但它可能有助於減少一些代碼。對於R Shiny,請參閱scoping explanation瞭解全球如何運作。

  2. 在你的具體情況下,你的反應問題是隔離的嗎?

    我仍在與Shiny握手,但採取look at this answer它使用條件面板和setupComplete條件。並且@daattali回答如下shinyjs可能爲您提供關於如何更好地放置您的反應/觀察或提供解決方法的想法。

一切順利。

+0

謝謝..我已經解決了這個問題 – Surya

相關問題