2016-11-22 109 views
0

参照下面的代码,现在所有的verbatimTextOutput框只有在所有列返回results时才会更新。 我希望UI在每列返回results时显示输出,以指示应用程序确实正在取得进展。 我该怎么做?更新循环内闪亮的输出

INPUT_COL = c("V1","V2","V3") 

shinyUI(fluidPage(
    sidebarLayout(
     sidebarPanel(
      uiOutput("controls") 
     ), 


     mainPanel(
      lapply(INPUT_COL, function(self){ 
      verbatimTextOutput(outputId=paste0("vb.",self)) 
      }) 
     ) 
    ) 
)) 


shinyServer(function(input, output,session) { 
    output$controls<- renderUI({ 
     lapply(INPUT_COL, function(self) { 
      output[[self]] <- renderUI(...) 
      uiOutput(self) 
     }) 
    }) 

    observe({ 
      params <- sapply(INPUT_COL, function(self) input[[self]]) 

      lapply(INPUT_COL, function(self) { 
      output[[paste0("vb.",self)]] <- renderPrint(
       { tryCatch(
       { 
        results<- getResults(params) #some long process 
        print(results) 
       }, 
       warning=function(war) return(war), 
       error=function(err) return(err) 
       ) 
       }) 
      }) 


     }) 

} 

回答

0

想通了通过使用计数器

status <-reactiveValues(i=0) 

shinyServer(function(input, output,session) { 
    output$controls<- renderUI({ 
     lapply(INPUT_COL, function(self) { 
      output[[self]] <- renderUI(...) 
      uiOutput(self) 
     }) 
    }) 

    observe({ 
      params <- sapply(INPUT_COL, function(self) input[[self]]) 
      isolate({ 
      if (length(params) > 0){ 
       status$i <- status$i+1 
       self <- INPUT_COL[status$i] 
       output[[paste0("vb.",self)]] <- renderPrint(
       { tryCatch(
        { 
         results<- getResults(params) #some long process 
         print(results) 
        }, 
        warning=function(war) return(war), 
        error=function(err) return(err) 
       ) 
       }) 
      } 
      }) 

      if (isolate(status$i) < length(params)){ 
      invalidateLater(0, session) #reset the observer, so it'll repeat 
      } 
      else{ 
      isolate({status$i <- 0}) 
      } 


     }) 

}