使用" withProgress"在闪亮

时间:2017-06-25 15:19:44

标签: r shiny shinydashboard

我试图理解"进度指标"在闪亮的工作,所以我创建了一个循环(虚构),大约需要7秒(1.8GHz)才能运行。 我想在用户点击Go!按钮后显示进度条

这是代码:

    ui <- fluidPage(
  headerPanel("Progress indicators"),
  sidebarPanel(
    numericInput("n", "N:", min = 0, max = 100, value = 50000),
    br(),
    actionButton("goButton", "Go!")

  ),
  mainPanel(
    verbatimTextOutput("nText")
  )
)

server <- function(input, output) {

  fictional <-  reactive({
  n=input$n
  p = rep(0,n)
  for(j in 1:n){
      data1=rnorm(1000,1,2)
      data2=runif(1000,1,2)
      p[j] =  min(data1,data2)
     }
    pw1 =  mean(p)
    return(pw1)
})
  ntext <- eventReactive(input$goButton, { fictional()})

  output$nText <- eventReactive(input$goButton, {

    withProgress(message = 'Progress indicators', {
    ntext()
     })
  })
}
shinyApp(ui, server)

我试图使用withProgress,但我不知道如何使用它来包装代码,因为当我点击Go!它向我显示进度条但停止。循环结束时消失

有什么建议吗?

提前谢谢!

1 个答案:

答案 0 :(得分:1)

请参阅?withProgress - 你必须告诉你的进展栏,例如

   ui <- fluidPage(
  headerPanel("Progress indicators"),
  sidebarPanel(
    numericInput("n", "N:", min = 0, max = 100, value = 50000),
    br(),
    actionButton("goButton", "Go!")

  ),
  mainPanel(
    verbatimTextOutput("nText")
  )
)

server <- function(input, output) {

  fictional <-  reactive({
  n=input$n
  p = rep(0,n)
  for(j in 1:n){
      if (j%%100==0) incProgress(100/n)
      data1=rnorm(1000,1,2)
      data2=runif(1000,1,2)
      p[j] =  min(data1,data2)
     }
    pw1 =  mean(p)
    return(pw1)
})
  ntext <- eventReactive(input$goButton, { fictional()})

  output$nText <- eventReactive(input$goButton, {

    withProgress(message = 'Progress indicators', {
    ntext()
     })
  })
}
shinyApp(ui, server)