Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Created March 13, 2014 01:09
Show Gist options
  • Save jcheng5/9520060 to your computer and use it in GitHub Desktop.
Save jcheng5/9520060 to your computer and use it in GitHub Desktop.
Progress example License: MIT
library(shinyIncubator)
shinyServer(function(input, output, session) {
output$plot <- renderPlot({
if (input$go == 0)
return()
# Wrap the entire expensive operation with withProgress
withProgress(session, {
setProgress(message = "Calculating, please wait",
detail = "This may take a few moments...")
Sys.sleep(1)
setProgress(detail = "Still working...")
Sys.sleep(1)
anotherExpensiveOperation()
Sys.sleep(1)
setProgress(detail = "Almost there...")
Sys.sleep(1)
plot(rnorm(100), rnorm(100))
})
})
anotherExpensiveOperation <- function() {
# It's OK for withProgress calls to nest; they will have
# a stacked appearance in the UI.
#
# Use min/max and setProgress(value=x) for progress bar
withProgress(session, min = 0, max = 10, {
setProgress(message = "Here's a sub-task")
for (i in 1:10) {
setProgress(value = i)
if (i == 7)
setProgress(detail = "Sorry, this is taking a while")
Sys.sleep(0.3)
}
})
}
})
library(shinyIncubator)
shinyUI(fluidPage(
# progressInit() must be called somewhere in the UI in order
# for the progress UI to actually appear
progressInit(),
h1("Progress demo"),
sidebarLayout(
sidebarPanel(
"This is a demonstration of the progress indicator ",
"from the",
a(href="https://github.com/rstudio/shiny-incubator",
"shiny-incubator"),
"package.",
hr(),
actionButton("go", "Run")
),
mainPanel(
plotOutput("plot")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment