Skip to content

Instantly share code, notes, and snippets.

@ptoche
Created January 16, 2014 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ptoche/8461329 to your computer and use it in GitHub Desktop.
Save ptoche/8461329 to your computer and use it in GitHub Desktop.
Demo on why observe() and isolate() can be preferable to reactive()
# server.R
shinyServer(
function(input, output, session){
# This approach triggers a delay when the box is filled before the actionButton is actioned
# busyReactive <- reactive({
# delay <- as.integer(input$delay)
# if(input$trigger > 0) {Sys.sleep(delay)}
# return(input$trigger)
# })
observe({
input$trigger
if(is.null(input$trigger) || input$trigger == 0){return()}
isolate(Sys.sleep(input$delay))
})
delayValues <- reactiveValues(total = 0)
observe({
input$trigger
if(is.null(input$trigger) || input$trigger == 0){return()} # needed for math below
delayValues$total <- isolate(delayValues$total) + isolate(input$delay)
})
output$back <- renderText({
if(is.null(input$trigger) || input$trigger == 0){return()}
paste("Shiny is back!", "The actionButton was activated", input$trigger, "times. You have waited a total of about", delayValues$total, "seconds.", sep = " ")
})
output$busy <- renderText({
paste0("Shiny is busy... ", "Expect to wait about ", input$delay, " seconds.")
})
}
)
# ui.R
shinyUI(
pageWithSidebar(
headerPanel(h3("Use of hasClass('shiny-busy') with conditionalPanel - Demo"))
,
sidebarPanel(
helpText(a("Reference: shiny-discuss/05C_X-XMwyk", href = "https://groups.google.com/forum/#!topic/shiny-discuss/05C_X-XMwyk", target="_blank"))
)
,
mainPanel(
h5("The actionButton triggers a time-consuming action:")
,
numericInput(inputId = "delay", label = "Select the delay (in seconds)", value = 2, min = 0), tags$br()
,
actionButton("trigger","actionButton")
,
conditionalPanel(condition = "$('html').hasClass('shiny-busy')"
, tags$h5(textOutput("busy"))
)
,
conditionalPanel(condition = "!$('html').hasClass('shiny-busy')"
, tags$h5(textOutput("back"))
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment