2015-10-06 42 views
0

我是Shiny編碼新手,我正在編寫代碼來實現圖像處理和計算。但是,由於輸出文本僅在函數完成執行時才顯示,所以我遇到了問題。如何渲染文本輸出而不等待函數完成?

以下是代碼我有一部分:

server.R

shinyServer(function(input, output) { 
    for(i in 1:100){ 
     processImage(i); 
     output$console <- renderText({ 
      paste(i," images completed"); 
     }) 
    } 

    processImage(i) <- function(){ 
     # code goes here 
    } 
} 

ui.R

shinyUI(fluidPage(
    titlePanel(
     h4("Image Processing") 
    ), 
    sidebarLayout(
     sidebarPanel(
      # some inputs here 
     ), 
     mainPanel(
      textOutput('console') 
     ) 
    ) 
)) 

output$console直到for循環結束後不會被渲染。我已經在互聯網上搜索解決方案,但沒有找到。誰能幫我這個?

+0

不確定閃亮的代碼,但在常規的是有一個'flush.console'功能。 –

+0

由於'output $ console'與控制檯不一樣,所以flush.console不適用於我。嘗試了我所知道的每一種可能的方式,但仍然無法知道如何在函數仍在運行時呈現文本。 – Woody

+0

對您有幫助嗎? http://shiny.rstudio.com/gallery/onflush-example.html –

回答

1

你可以用withProgress這樣做。 編輯:您需要安裝shinyIncubator

rm(list = ls()) 
library(shiny) 
library(shinyIncubator) 

server <- function(input, output, session) { 
    observe({ 
    if(input$aButton==0) return(NULL) 
    withProgress(session, min=1, max=15, expr={ 
     for(i in 1:10) { 
     setProgress(message = 'Processing...',detail = paste0('Image number ',i)) 
     Sys.sleep(0.5) 
     } 
    }) 
    }) 
} 

ui <- pageWithSidebar(
    headerPanel("Testing"), 
    sidebarPanel(actionButton("aButton", "Let's go!"), width=2), 

    mainPanel(progressInit()) 
) 

shinyApp(ui = ui, server = server) 

enter image description here

+0

我收到一個錯誤:'錯誤:無法找到函數「progressInit」' – Woody

+0

需要安裝'shinyIncubator'包,對不起! –

+0

感謝,除了使用進度條渲染'output $ console'之外的進度,可以使用'output $ console'渲染進度嗎?正如我想要的是在mainPanel中顯示進度。 – Woody