2016-11-22 108 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}) 
      } 


     }) 

}