在SHILINY中使用&WITH&PROCESS&QOOT;amp、SHILINY、WITH、QOOT

2023-09-03 14:01:48 作者:爱意在夜里泛滥

我想了解一下"进度指示器"在Slight中是如何工作的,所以我创建了一个循环(虚构的),运行时间约为7秒(1.8 GHz)。 我想在用户单击按钮开始后显示进度条!

代码如下:

    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)
B B

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

有什么建议吗?

提前感谢!

推荐答案

查看?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)