如何在进度条中包含反应元素(图形)

时间:2014-11-18 01:52:46

标签: r shiny

我有.txt文件和.xlsx文件,将上传到闪亮。 .txt上传很好,内置进度条显示完美的进度。 但是在.xlsx文件上传的情况下,我在内部反应元素中进行合并操作,因此它与实际进度不一致。 (进度条始终在任务之前完成)

我可以使用pbapply,但是我必须再次通过命令行监视它们,这不是我想要的。

我非常有兴趣将内置进度条与反应元素内的整个上传过程联系起来。我怎么能这样做。

这是我的计划。

ui.R

shinyUI(fluidPage(
  titlePanel("Uploading Files"),
sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose first file to upload',
                accept = c(
                  'text/csv',
                )
      ),))sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose second file to upload',
                accept = c(

                  '.xlsx'
                )
      )
)))

server.R

shinyServer(function(input, output) {
a <- reactive({
   fileinput1 <- input$file1
   if (is.null(fileinput1))
   return(NULL)
   read.table(fileinput1$datapath, header = TRUE, col.names =  c("Experiment","Mesocosm","Hour","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","PAR","Temperature","Salinity","CO2atm","u10","DICflux","CO2ppm","CO2mol","pH"))
   #a$Chla <- a$CHLphy + a$CHLcocco  #Add new columns as per observation data
   #a$PON <- a$Nphy + a$Nhet + a$Ndet + a$Ncocco 
 })

 #Upload Observation Data 
b <- reactive({
  #xlfile <- list.files(pattern = "*.xlsx")
  fileinput2 <- input$file2
      if (is.null(fileinput2))
        return(NULL)
      xlfile <- fileinput2$datapath
  wb <- loadWorkbook(xlfile)
  sheet_ct <- wb$getNumberOfSheets()
  for( i in 1:sheet_ct) {    #read the sheets into 3 separate dataframes (mydf_1, mydf_2, mydf3)
    print(i)
    variable_name <- sprintf('mydf_%s',i)
    assign(variable_name, read.xlsx(xlfile, sheetIndex=i))
  }
  colnames(mydf_1) <- names(mydf_3)
  colnames(mydf_2) <- names(mydf_3)
  full_data <- rbind(mydf_1[-1,],mydf_2[-1,],mydf_3[-1,]) #making one dataframe here
  b <- lapply(full_data,function(x) as.numeric(x))
})
})

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题,但理论上您可以在浏览器顶部创建进度条:

 observe({
     fileinput2 <- input$file2
     if (is.null(fileinput2))
       return(NULL)
    withProgress(message = 'Downloading file', value = 0, {
    #xlfile <- list.files(pattern = "*.xlsx")


xlfile <- fileinput2$datapath
wb <- loadWorkbook(xlfile)
sheet_ct <- wb$getNumberOfSheets() # this will be used in progressbar

for( i in 1:sheet_ct) {    #read the sheets into 3 separate dataframes (mydf_1, mydf_2, mydf3)
  print(i)
  variable_name <- sprintf('mydf_%s',i)
  assign(variable_name, read.xlsx(xlfile, sheetIndex=i))
  incProgress(1/sheet_ct, detail = paste("Sheet:", sheet_ct,"Dowloaded"))

}
colnames(mydf_1) <- names(mydf_3)
colnames(mydf_2) <- names(mydf_3)
full_data <- rbind(mydf_1[-1,],mydf_2[-1,],mydf_3[-1,]) #making one dataframe here
b <- lapply(full_data,function(x) as.numeric(x))
})

}) })

更多信息:http://shiny.rstudio.com/articles/progress.html