rshiny

How can I prevent shiny code to run twice?


I have a shiny app where two inputs are dependent on each other and I can see in my print("Running") that the code is executed twice when I change "A" to +1. I do understand why it happens, first I update A and the code is run and afterwards B is updated and the code is run again. Is it possible to have it only run once?

library(shiny)

ui <- fluidPage(
  numericInput("a", "A", 0, step = 1),
  numericInput("b", "B", 1, step = 1),
  textOutput("sum")
)

server <- function(input, output, session) {
  
  observeEvent(input$a, {
    
    updateNumericInput(session = session, "b", value = input$a + 1)
    
    
  })
  
  sum_a_b <- reactive({
    print("Running")
    input$a + input$b
    
  })
  
  output$sum <- renderText(sum_a_b())
  
}

shinyApp(ui, server)

Solution

  • Yes, use: isolate(input$a) + input$b