Created
January 16, 2014 19:10
-
-
Save ptoche/8461329 to your computer and use it in GitHub Desktop.
Demo on why observe() and isolate() can be preferable to reactive()
This file contains 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
# 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.") | |
}) | |
} | |
) |
This file contains 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
# 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