Run code just when actionbutton is pressed

I have a doubt about actionButton and submitButton in Shiny. I want to develop a simulator, but i need to ensure that the simulation will run ONLY if the button "simulate" is pressed.

The main issue is that, after the button is pressed one time, the output table starts to update its values when some input is changed even if the "simulate" button is not pressed. I need that the output update just right after the "simulate" button is clicked.

How can i prevent that? I tried to change the combination of the arguments "once", "ignoreINIT" and "ignoreNULL" in the observeEvent function, but without success.

I wrote a very simple app that shows the behavior that i want to solve, as it follows:

library(shiny)


ui <- fluidPage(
   
   # Application title
   titlePanel("Action Button Doubt app"),
   
   sidebarLayout(
      sidebarPanel(
         numericInput("n1","Number 1",value = NULL,min = 0,max = 50),
         numericInput("n2","Number 2",value = NULL,min = 0,max = 50),
         numericInput("n3","Number 3",value = NULL,min = 0,max = 50),
         textInput("t1","Text 1",value = NULL),
         actionButton("go","Simulate")
      ),
      
      
      mainPanel(
        DT::dataTableOutput("out")
      )
   )
)

server <- function(input, output) {
  
   observeEvent(input$go,{
     Value1 <- reactive(input$n1 + 10)
     Value2 <- reactive(input$n2 + 20)
     Value3 <- reactive(input$n3 + 30)
     output$out <- DT::renderDataTable(DT::datatable(data.frame(Value1(),Value2(),Value3(),input$t1)))
   })

}

shinyApp(ui = ui, server = server)


Can anyone help me with that? I found some topics with similar issues, tried to use the function "reset" from the shinyjs package, but still not helped me.

Thanks in advance

I didn't alter the UI at all, only the server code :

server <- function(input, output) {
  
  Value1 <- reactive(input$n1 + 10)
  Value2 <- reactive(input$n2 + 20)
  Value3 <- reactive(input$n3 + 30)
  
  restab <- eventReactive(input$go,{
    DT::datatable(data.frame(Value1(),Value2(),Value3(),input$t1))
  })
  output$out <- DT::renderDataTable(restab())
}

shinyApp(ui = ui, server = server)
4 Likes

Nice! Thank you @nirgrahamuk!

I was trying to use the eventReactive without a object! This totally solve my problem.

1 Like

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