如何在不等待功能完成的情况下渲染文本?

时间:2015-10-06 14:50:25

标签: r user-interface shiny

我是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')
        )
    )
))
在for循环结束之前,不会呈现

output$console。我在互联网上搜索解决方案,但没有找到。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分: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