I am struggling to figure out how to get 2 R Shiny widgets to update each other. For example, a slider widget that can update a text box widget, and visa versa, where the en
If you enclose each input slider under a reactive observe, you can achieve your goal. This not only works for two inputs, but for several inputs as well. Let me explain by an example:
observe({
# Create a reactive relationship with slider1
input$slider1
# Update the second slider - slider2
updateTextInput(session, "slider2", NULL, input$slider1)
)}
Similarly for second input, you'd have to repeat the code, by:
observe({
# Create a reactive relationship with slider2
input$slider2
# Update the second slider - slider1
updateTextInput(session, "slider1", NULL, input$slider2)
)}
The trick is to create a dynamic UI. In this way, you can update a slider-drawing expression on changes in the other UI elements and rebuild a slider widget using a different default value:
server.R
shinyServer(
function(input, output, clientData, session) {
output$slider1 <- renderUI({
slider2.value <- input$inSlider
default.slider1 <- if (is.null(slider2.value)) 15 else slider2.value
sliderInput("control_num",
"This controls values:",
min = 1, max = 20, value = default.slider1)
})
output$slider2 <- renderUI({
slider1.value <- input$control_num
default.slider2 <- if (is.null(slider1.value)) 15 else slider1.value
sliderInput("inSlider", "Slider input:",
min = 1, max = 20, value = default.slider2)
})
})
ui.R
shinyUI(fluidPage(
titlePanel("One Way Reactive Slider"),
fluidRow(
column(3,
wellPanel(
h4("Slider Inputs"),
uiOutput('slider1'),
uiOutput('slider2')
))
)
))