Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use "withProgress" in shiny

I'm trying to understand how "Progress indicators" works in shiny, so I created a loop (fictional) that takes about 7seconds (1.8GHz) to run. I would like to show a progress bar after user clicks a button Go!

This is the code:

    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)

I was trying to use withProgress but I don't know how to use it to wrap the codes because when I click in Go! it show me the progress bar but stops. Disappears when the loop ends

Any suggestions?

Thank you in advance!

like image 663
albert Avatar asked Oct 16 '25 16:10

albert


1 Answers

See ?withProgress- you have to tell your progess bar the progress, e.g.

   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)
like image 128
lukeA Avatar answered Oct 18 '25 11:10

lukeA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!