3

I want to execute the if statement only once when the actionButton is pressed once. So, I want to reset the actionButton because the if statement is executed continuously. T.T

Here is my code.

      output$action_btn <- renderUI({
        actionButton("act_btn", class="btn-primary", "active")
      })

      output$test <- renderPlot({
        if(input$act_btn!= 0){
         (execution code..)
        }
      })

      input$act_btn <- 0  #This is Error.

When I press the actionButton, It works well.

The problem is input$act_btn <- 0. I want to reset the value of act_btn.

Is there a method to solve this problem?

S.Kang
  • 581
  • 2
  • 10
  • 28

1 Answers1

3

A little late, but I just went through this process recently and found this blog post from Antoine Guillot very helpful.

In essence he creates a proxy variable in the JS client side, that always gets updated on button click. Then in your r code, you observe the proxy variable and can continue the logic as if you had reset the button status to 0.

tags$script("$(document).on('click', '#Main_table button', function () {
 Shiny.onInputChange('lastClickId',this.id);
 Shiny.onInputChange('lastClick', Math.random())
});")

In this code, any click event in the shiny output #Main_table containing a button will trigger the onClick event. Then using shiny js commands, a new input variable is created which now contains the id of the button clicked.

You will notice

Shiny.onInputChange('lastClick', Math.random())

This is the "proxy" that you use to observeEvent on, and take action with.

Section f. here http://enhancedatascience.com/2017/03/01/three-r-shiny-tricks-to-make-your-shiny-app-shines-33-buttons-to-delete-edit-and-compare-datatable-rows/

Hope it helps someone else as much as it did me.

PCNZ
  • 31
  • 5