2015-09-27 81 views
2

我的閃亮應用必須執行一些稍慢的服務器端計算,所以我希望用戶能夠跟蹤他們在等待時發生的事情。這裏是我的應用程序的結構的一個小例子:R shiny - observeEvent - 使命令按時間順序執行

https://gist.github.com/0bb9efb98b0a5e431a8f

runGist("0bb9efb98b0a5e431a8f") 

我想發生的事情是:

  1. 點擊提交
  2. 的應用程序移動到「輸出'標籤面板
  3. 它按照它們在observeEvent
  4. 中列出的順序顯示消息和輸出

實際發生的是:

  1. 點擊提交
  2. 一切都執行服務器端一次
  3. 的UI在年底

更新,是否有可能得到什麼我想在這裏?

回答

2

我無法用你的方法想出一個解決方案。 Shiny似乎要等到server = function(input, output)中的所有內容都計算完畢,並且在output$...的所有組件都可用時才顯示結果。我不知道是否有辦法解決這個問題。

然而有一個解決方案來實現,你可以嘗試:用你的代碼Progress indicators

實現:

library(shiny) 

shinyApp(
    ui = navbarPage(title="test", id="mainNavbarPage", 

        tabPanel("Input", value="tabinput", 
          numericInput('n', 'Number of obs', 100), 
          actionButton(inputId="submit_button", label="Submit") 
       ), 

        tabPanel("Output", value="taboutput", 
          plotOutput('plot') 
       ) 
), 
    server = function(input, output, session) { 


    observeEvent(input$submit_button, { 
     # Move to results page 
     updateNavbarPage(session, "mainNavbarPage", selected="taboutput") 

     withProgress(message = "Computing results", detail = "fetching data", value = 0, { 

     Sys.sleep(3) 

     incProgress(0.25, detail = "computing results") 

     # Perform lots of calculations that may take some time 
     Sys.sleep(4) 

     incProgress(0.25, detail = "part two") 

     Sys.sleep(2) 
     incProgress(0.25, detail = "generating plot") 

     Sys.sleep(2) 
     }) 

     output$plot <- renderPlot({hist(runif(input$n)) }) 

    }) 
}) 
+0

這是一個有用的替代,非常感謝你 – explodecomputer

+0

@explodecomputer歡迎您! –