Reputation: 555
I have a shiny app which outputs the sum of clicks for two action buttons {+1, -1}. Can someone tell me how I can make the minimum value of the counter stop at zero. For example, if the first two clicks from a new session are Remove1 and Remove 1, the output will be 0, rather than -2?
library(shiny)
ui <- fluidPage(
actionButton("add", "Add 1"),
actionButton("remove", "Remove 1"),
textOutput("counter")
)
server <- function(input, output, session) {
output$counter <- renderText(
input$add + input$remove * -1
)
}
shinyApp(ui, server)
Upvotes: 1
Views: 58
Reputation: 19544
It seems you can not change the value of an actionButton
.
Instead you can use reactiveVal()
like this:
server <- function(input, output, session) {
total <- reactiveVal( 0 )
observeEvent( input$add, total( total() + 1 ))
observeEvent( input$remove, total( max( 0, total() - 1 )))
output$counter <- renderText( total() )
}
Upvotes: 1