0

我已经编写了以下代码来上传文件并在单击“开始”按钮后进行一些计算。但是,即使我不再再次上传文件,以下代码也会每次执行文件加载。我不想再次加载文件。

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

1 回答 1

0

苏里亚,

  1. 首先看看我关于 Importing and access large data files in Shiny 的回答中的第 2 点。

    我们发现将数据加载到与您的和文件global.R位于同一目录的文件中很有帮助。ui.Rserver.R

    这可能效果不佳,因为您的应用需要用户选择文件,但它可能有助于精简您的一些代码。请参阅 R Shiny 的这个范围解释,了解 global 的工作原理。

  2. 在您的特定情况下,您的反应内部的隔离是一个问题吗?

    我仍然在掌握 Shiny 但看看这个答案,它使用了条件面板和setupComplete条件。并且@daattali 在下面的回答shinyjs可能会为您提供有关如何更好地放置您的反应/观察或提供解决方法的想法。

一切顺利。

于 2016-06-10T15:41:21.383 回答