Home > Enterprise >  How to make input$action starts from zero every time I click on Action Buttom in Shiny?
How to make input$action starts from zero every time I click on Action Buttom in Shiny?

Time:01-06

As this link says about Action Button, the input$action return the number of clicks On the button. enter image description here

CodePudding user response:

If we really need to "reset" the button, we can re-render it. Here is an example app:

library(shiny)
library(dplyr)

ui <- fluidPage(
  uiOutput("action"),
  actionButton("reset", "Reset"),
  verbatimTextOutput("values")
)

server <- function(input, output, session) {
  output$action <- renderUI({
    actionButton("action_but", "Action")
  })

  vals <- reactiveVal(tibble(clicks = integer(0)))
  counter <- reactiveVal(0)

  observe({
    counter(input$action_but)
  })

  observeEvent(input$reset,
    {
      vals(add_row(vals(), clicks = as.integer(input$action_but)))

      output$action <- renderUI({
        actionButton("action_but", "Action")
      })

      counter(input$action_but)
    },
    ignoreInit = TRUE
  )

  output$values <- renderPrint({
    list(counter(), vals())
  })
}

shinyApp(ui, server)

enter image description here

  •  Tags:  
  • Related