rshinydynamic-ui

R Shiny - How to update a SINGLE input with Multiple input methods


I want to use TWO methods to update a SINGLE input.
Let say I want to use NumericInput and SliderInput.

When I update my NumericInput to 5, the SliderInput should update to 5.
Whatever I said above, I've seen many examples.
But how do I do it the other way around? (update each other)
Now If I update the SliderInput to 6, how to get my NumericInput updated to 6?

Heres what I've tried but didn't work:

ui <- fluidPage(
numericInput("year", "", min = 1979, max = 2017, value = 1979),
sliderInput("year", "Select Year", min = 1979, max = 2017, value = 1979)
)

#===================================================

server <- function(input, output, session) {
  observe(input$year,  {
    updateSliderInput(session, "year", value = year)
  })

  observe(input$year,  {
    updateNumericInput(session, "year", value = year)
  })

Solution

  • Do you mean something like this?

    ui <- fluidPage(
      numericInput("year", "", min = 1979, max = 2017, value = 1979),
      sliderInput("year2", "Select Year", min = 1979, max = 2017, value = 1979)
    )
    
    #===================================================
    
    server <- function(input, output, session) {
      observeEvent(input$year,  {
        updateSliderInput(session, "year2", value = input$year)
      })
    
      observeEvent(input$year2,  {
        updateNumericInput(session, "year", value = input$year2)
      })
    }
    
    
    
    shinyApp(ui, server)