how to modify input$submit value

Hello I want to modify input$submit value, the variable "submit" is created by: actionButton("submit", "submit"), I notice the original input$submit value is
[1] 0
attr(,"class")
[1] "shinyActionButtonValue" "integer"

Every time, I click "submit "button, the input$submit[[1]] value will increase by 1. I want to reset it to 0; I follow chatgpt answer. It does not work. How can I reset the value to its original value?

library(shiny)
library(shinyjs)

submit_value <- reactiveVal(0)

ui <- fluidPage(
actionButton("submit", "Submit"),
actionButton("reset", "reset"),
textOutput("result")
)

server <- function(input, output, session) {
observeEvent(submit_value , {
output$result <- renderText({
paste("Button clicked! input$submit[[1]] is now:", input$submit[[1]])
})
})

observeEvent(input$reset, { ##shinyjs::reset("submit")
isolate({
input$submit <- submit_value # Method 1: Direct reset using isolate()
})
} )
}
shinyApp(ui, server)

HI @yu.cai.tch. Like you, I thought shinyjs would offer a solution, but according to the documentation for the reset() function, action buttons are not supported. One way I've handled this before is by introducing a reactiveVal that serves as the button counter (input_button_value in example below). Every time the submit button is clicked, the reactiveVal increases by 1. When the reset button is clicked, the reactiveVal is set to 0. Then, any place in the app I would want to use the counter value of the submit button, I would refer to this reactiveVal.

I reworked the sample app you provided to illustrate this concept below.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  actionButton("submit", "Submit"),
  actionButton("reset", "reset"),
  textOutput("result"),
  br(),
  textOutput("result2"),
)

server <- function(input, output, session) {
  input_button_value <- reactiveVal(0)
  
  observeEvent(input$submit , {
    new = input_button_value() + 1
    input_button_value(new)
  }, ignoreNULL = T)
  
  observeEvent(input$reset, { 
    input_button_value(0)
  } )
  
  output$result <- renderText({
    paste("Button clicked! input$submit[[1]] is now:", input$submit[[1]])
  })
  
  output$result2 <- renderText({
    paste("Button clicked! input_button_value is now:", input_button_value())
  })
}
shinyApp(ui, server)

Thank you so much, Scott. The purpose that I want to reset input$submit to 0 is that I want to reset my UI. I do not know how to reset my UI. After I fill the parameters in UI, I want to reset it to the original state with empty values. Currently, I have to turn off it and relaunch it to achieve this purpose.

shiny actionButtons are designed to only ever count up, we can use shiny modules to make our own buttons that behave differently;

This can be seen as a modularisation of the concept Scott shared with you, I've used varitions on this in my own work.

library(shiny)
shiny::devmode()

resettableButtonUI <- function(inputId,
                               label,
                               icon=NULL,
                               width=NULL,
                               ...) {
  ns <- NS(inputId)
  tagList(
  actionButton(inputId = ns(inputId),
               label = label,
               icon = icon,
               width = width,
               ...
               )
  )
}

resettableButtonServer <- function(inputId,reset_trigger) {
  moduleServer(
    inputId,
    function(input, output, session) {
      stopifnot(is.reactive(reset_trigger))
      
      internal_count <- reactiveVal(0)
      
      observeEvent(reset_trigger(),{
        internal_count(input[[inputId]])
      })
      
      return(reactive(input[[inputId]] - internal_count()))
    }
  )
 
}

library(shiny)

ui <- fluidPage(
  resettableButtonUI("mybutton","Press"),
  actionButton("reset_button","Reset"),
  verbatimTextOutput("textout")
)

server <- function(input, output, session) {
  
  mybutval <- resettableButtonServer("mybutton",reset_trigger = reactive(input$reset_button))
  
  output$textout <- renderPrint({
    v <- mybutval()
    v
  })
}

shinyApp(ui, server)
1 Like

Hello nirgrahamuk, Thank you for your information. Great to know and take some time to learn these codes. It looks like that input$mybutton value can not change,. In the above code, through the difference "input[[inputId]] - internal_count()", it achieved that it looks that the value is reset. In fact, input$mybutton has never change. It is a clever way to bypass this issue.

I used DT library to fill in the input values in DT table input. I want to use a reset button to reset its value to empty after I complete the task. It looks that my thinking to reset input$mybutton is difficult. I also find to reset DT table is quite difficult.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.