title: How to prevent output from running twice when inputs are inter-dependent?
body: <p>I'm developping an R Shiny-based application.
  I want to keep my input consistent with available data, thus I update the selected values in selectInput.
  When I change selected value in input 1, then the value of input 2 is updated, then the data is updated (just once). OK
  BUT if I change selected value in input 2, then the data is updated, then the value of input 1 is updated, then the data is updated AGAIN.
  Check out the "check latest_value" that is printed twice.</p>
  
  <p>Initially I used renderUI rather than updateSelectInput, but at initialisation, the data is computed twice. </p>
  
  <pre><code>library(shiny)
  library(DT)
  library(dplyr)
  my_data=data.frame(CO2)
  # Running a Shiny app object
  app &lt;- shinyApp(
    ui = bootstrapPage(
      selectInput('type','Choix du type',choices = unique(my_data$Type)),
      uiOutput('plant_ui'),
      DTOutput('plot')
    ),
    server = function(input, output) {
  
      data=reactive({
        # req(input$type)
        my_data_temp=my_data
        if(length(input$type)&gt;0){
          my_data_temp=my_data_temp%&gt;%filter(Type%in%input$type)
        }
        if(length(input$plant)&gt;0){
          my_data_temp=my_data_temp%&gt;%filter(Plant%in%input$plant)
        }
  
        my_data_temp
      })
  
      latest_plant_value=reactive({
        if(is.null(input$plant))data()$Plant[1]
        else input$plant
      })
  
  
      output$plant_ui=renderUI({
        sub_data=data()
        selectInput(inputId = 'plant',"filtre par plant",choices = unique(sub_data$Plant),
                    selected=latest_plant_value())
      })
  
      output$plot &lt;- renderDT({ 
        print("check latest_value")
        datatable(data()) })
    }
  )
  runApp(app)
  </code></pre>
  
  <p>That's why I decided to use updateSelectInput based on this <a href="https://stackoverflow.com/questions/30630668/alternate-control-of-a-sliderinput-between-a-derived-value-and-user-selected-val">Alternate control of a sliderInput between a derived value and user selected value</a> but the sequential structure of the code makes the data to be computed twice when I change input 2 value.</p>
  
  <pre><code>library(shiny)
  library(DT)
  library(dplyr)
  my_data=data.frame(CO2)
  # Running a Shiny app object
  app &lt;- shinyApp(
    ui = bootstrapPage(
      selectInput('type','Choix du type',choices = unique(my_data$Type),selected=my_data$Type[1]),
      selectInput('plant','Choix du type',choices = unique(my_data$Plant),selected=my_data$Plant[1]),
      DTOutput('plot')
    ),
    server = function(input, output,session) {
  
      data=reactive({
        # req(input$type)
        my_data_temp=my_data
        if(length(input$type)&gt;0){
          my_data_temp=my_data_temp%&gt;%filter(Type%in%input$type)
        }
        if(length(input$plant)&gt;0){
          my_data_temp=my_data_temp%&gt;%filter(Plant%in%input$plant)
        }
  
        my_data_temp
      })
  
      observeEvent(input$type,{
        print("update type changed")
        updateSelectInput(session, "plant",
                          selected =  unique(my_data%&gt;%filter(Type%in%input$type)%&gt;%.$Plant)[1])
      })
      observeEvent(input$plant,{
        print("update plant changed")
        updateSelectInput(session, "type",
                          selected =  unique(my_data%&gt;%filter(Plant%in%input$plant)%&gt;%.$Type)[1])
      })
  
     output$plot &lt;- renderDT({ 
       print("check latest_value")
  
       datatable(data()) })
    }
  )
  runApp(app)
  </code></pre>
  
  <p>Fixes like this one don't work in that case because I'm not trying to achieve the same thing <a href="https://stackoverflow.com/questions/42436998/three-interdependent-selectinput-in-r-shiny-application">three interdependent selectInput in R/Shiny application</a></p>
  
  <p>I want the default selected value of each input to be consistent so that the filter returns at least 1 value. This of any input I change.</p>