Skip to content

Instantly share code, notes, and snippets.

@skohari
Created October 5, 2018 20:43
Show Gist options
  • Save skohari/7a432edce6bf45ee4553d279d7605b1d to your computer and use it in GitHub Desktop.
Save skohari/7a432edce6bf45ee4553d279d7605b1d to your computer and use it in GitHub Desktop.
reactiveValues; for values to be 'floating', and not `fixed`.
ui <- fluidPage(
actionButton("minus", "-1"),
actionButton("plus", "+1"),
br(),
textOutput("value")
)
# The comments below show the equivalent logic using reactiveValues()
server <- function(input, output, session) {
value <- reactiveVal(0) # rv <- reactiveValues(value = 0)
observeEvent(input$minus, {
newValue <- value() - 1 # newValue <- rv$value - 1
value(newValue) # rv$value <- newValue
})
observeEvent(input$plus, {
newValue <- value() + 1 # newValue <- rv$value + 1
value(newValue) # rv$value <- newValue
})
output$value <- renderText({
value() # rv$value
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment