David Z
David Z

Reputation: 7041

How to show a progressBar in a single function in shiny?

Here is an example. The progress bar just jumps from 0% to 100% due a single function getres(). How to indicate the progress smoothly?

library("shiny")
library("shinyWidgets")
library("DESeq2")
library("airway")
data(airway)

getres <- function(eset){
  dds<-DESeqDataSet(eset, design = ~cell + dex)
  keep <- rowSums(counts(dds)) >= 10
  dds <- dds[keep,]
  dds <- DESeq(dds)
  res <- results(dds)
  return(res) 
}


ui <- fluidPage(
  tags$h1("Progress bar in Sweet Alert"),
  useSweetAlert(), # /!\ needed with 'progressSweetAlert'
  actionButton(
    inputId = "go",
    label = "Launch long calculation !"
  )
)

server <- function(input, output, session) {

  observeEvent(input$go, {
    progressSweetAlert(
      session = session, id = "myprogress",
      title = "Work in progress",
      display_pct = TRUE, value = 0
    )
    for (i in seq_len(1)) {
      Sys.sleep(0.1)
      updateProgressBar(
        session = session,
        id = "myprogress",
        res<-getres(airway),
        value = i
      )
    }
    closeSweetAlert(session = session)
    sendSweetAlert(
      session = session,
      title =" Calculation completed !",
      type = "success"
    )
  })

}

shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 1085

Answers (1)

David
David

Reputation: 2647

I was unable to run your example as airway and DESeq2 are not available for R 3.6+. BUT there is an interesting package that I have been meaning to try out called waiter.

Within waiter there is waitress which will "let you display loading bars on the entire screen or specific elements only."

There is a great demo app where you play with the different functions.

Here is an example from the docs!

library(shiny)
library(waiter)

ui <- navbarPage(
  "Waitress on nav",
  tabPanel(
    "home",
    use_waitress(),
    plotOutput("plot")
    )
)

server <- function(input, output){

  # now waitress ranges from 0 to 100
  waitress <- Waitress$new("nav", theme = "overlay", min = 0, max = 10)

  output$plot <- renderPlot({

    for(i in 1:10){
      waitress$inc(1) # increase by 10%
      Sys.sleep(.5)
    }

    hist(runif(100))
    waitress$close() # hide when done
    })

}

shinyApp(ui, server)

Hope this helps or gives you other ideas!

Upvotes: 3

Related Questions