Created
March 13, 2014 01:09
-
-
Save jcheng5/9520060 to your computer and use it in GitHub Desktop.
Progress example
License: MIT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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