隔离文件加载

时间:2016-06-08 06:50:57

标签: r dependencies shiny shinydashboard isolate-scope

我已经编写了以下代码来上传文件并在点击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")
    )
  )
))

1 个答案:

答案 0 :(得分:0)

苏里亚,

  1. 首先,请看看my answer around Importing and accessing large data files in Shiny中的第2点。

    我们发现在global.R文件中加载数据非常有用,该文件与ui.Rserver.R文件位于同一目录中。

    这可能无法正常运行,因为您的应用需要用户选择文件,但它可能有助于减少部分代码。请参阅scoping explanation了解R Shiny的全球运作方式。

  2. 在您的具体情况下,您的反应中的隔离是一个问题吗?

    我仍然掌握着Shiny,但是使用look at this answer来使用条件面板和setupComplete条件。 @daattali在下面用shinyjs回答可以为您提供有关如何更好地发布被动反应/观察或提供解决方法的建议。

  3. 一切顺利。