Skip to content

Instantly share code, notes, and snippets.

@ptoche
Last active January 3, 2016 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptoche/8431284 to your computer and use it in GitHub Desktop.
Save ptoche/8431284 to your computer and use it in GitHub Desktop.
Demo on actionButtons, reactiveValues and Sys.sleep()
# server.R
shinyServer(
function(input, output, session){
# Part I: create an observer on the state of actionButton "calculate"
busy <- reactiveValues(state = 0)
observe({
if(is.null(input$calculate) || input$calculate == 0){return()}
busy$state <- 1 - isolate(busy$state) # toggles between 0 and 1
})
# Part II: create an observer on the reactiveValues based on the state of actionButton "calculate"
# Alternatively, nest the conditionals inside output$observeReactiveValues with a return() for each branch
observe({
input$calculate
# if input$calculate is undefined, e.g. wrapped in a conditional on server side:
if (is.null(input$calculate)) {
output$observeReactiveValues <- renderUI({
helpText(HTML("The current state is <code>is.null(input$calculate)</code>"))
})
}
# if input$calculate is defined:
if (input$calculate == 0){
output$observeReactiveValues <- renderUI({
helpText(HTML("The current state is <code>input$calculate == 0</code>"))
})
}
# if input$calculate is defined:
if (input$calculate != 0 && busy$state == 1){
output$observeReactiveValues <- renderUI({
helpText(HTML("The current state is <code>input$calculate != 0 && busy$state == 1</code>"))
})
}
# if input$calculate is defined:
if (input$calculate != 0 && busy$state == 0){
output$observeReactiveValues <- renderUI({
helpText(HTML("The current state is <code>input$calculate != 0 && busy$state == 0</code>"))
})
}
})
# Part III: create an observer on the state of actionButton "sleep"
observe({
input$sleep
# if input$sleep is undefined:
if (is.null(input$sleep)) {
output$observeActionButton <- renderUI({
helpText(HTML("The current state is <code>is.null(input$sleep)</code>"))
})
}
# if input$sleep is defined:
if (input$sleep == 0){
output$observeActionButton <- renderUI({
helpText(HTML("The current state is <code>input$sleep == 0</code>"))
})
}
# if input$sleep is defined:
if (input$sleep != 0){
Sys.sleep(5)
output$observeActionButton <- renderUI({
list(
helpText(HTML("The current state is <code>input$sleep != 0</code>."))
,
helpText(HTML("You have just finished with <code>Sys.sleep(5)</code>. The actionButton only triggers once. Refresh your browser to reinitialize <code>input$sleep</code>."))
)
})
}
})
})
# ui.R
shinyUI(
pageWithSidebar(
headerPanel("Use of observe with actionButtons, reactiveValues, and Sys.sleep() - Demo")
,
sidebarPanel(
actionButton(inputId = "calculate", label = "actionButton bound to reactiveValues")
, tags$br(), tags$hr(), tags$br(),
actionButton(inputId = "sleep", label = "actionButton bound to Sys.sleep(5)")
,
tags$hr()
,
helpText("Basic Demo by Patrick Toche, 15 January 2014. In good faith but with no guarantees of correctness and/or elegance.")
)
,
mainPanel(
h4("observe with reactiveValues conditioned on FIRST actionButton:")
,
uiOutput("observeReactiveValues")
,
tags$hr()
,
h4("conditionalPanel that displays only after SECOND actionButton is actioned")
,
conditionalPanel(condition = "input.sleep > 0"
, wellPanel(
uiOutput("observeActionButton")
)
)
,
tags$hr()
,
h4("Comment:")
,
helpText(HTML("The <code>actionButton</code> is incremented by 1 each time it is actioned, so to keep track of several actions I created a reactiveValue that switches between 0 and 1 each time the actionButton is actioned"))
)
)
)
@ptoche
Copy link
Author

ptoche commented Jan 15, 2014

some people don't like my indentations, well they're ugly but they work for me ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment