R shiny bi-directional jet widgets

I am trying to figure out how to get 2 R Shiny widgets to update each other. For example, a slider widget that can update a text window widget and vice versa, where the end user can select one of the widgets.

This question is similar, but there is no answer, so I give what (hopefully) a simpler example. Getting reactive dependencies, as shiny :: reactive () did . If this question is answered elsewhere, I could not find such an answer.

I would like to know if I can move slider1 to move slider2 and slider2 to move slider1. Right now I can only do the first part (I can move slider1 to move slider2). If I can do this, I think I can make widget 1 a slider, and widget 2 a numeric one with the same code.

The example below has been modified from http://shiny.rstudio.com/gallery/update-input-demo.html and this is the minimum example I could do. This was the only application that I could find, getting closer to what I was looking for, although I understand that a completely different approach may be required ...

Server .R code

shinyServer( function(input, output, clientData, session) { #### one way interaction between slider 1 and 2 #### observe({![enter image description here][1] c_label <- input$control_label c_num <- input$control_num # <- input$inSlider # Slider input ============================================= updateSliderInput(session, "inSlider", label = paste("Slider2", c_label), value = c_num) updateSliderInput(session, "control_num", label = paste("Slider1", c_label), value = c_num) }) }) 

Code Ui.r

 shinyUI(fluidPage( titlePanel("One Way Reactive Slider"), fluidRow( column(3, wellPanel( h4("Slider Inputs"), sliderInput("control_num", "This controls values:", min = 1, max = 20, value = 15), sliderInput("inSlider", "Slider input:", min = 1, max = 20, value = 15) )) ) )) 

Below is a picture of the app while running. Moving slider 1 moves slider 2, but the opposite is not true (which is what I would like to do).

+5
source share
2 answers

The trick is to create a dynamic interface. Thus, you can update the slider drawing expression when changing in other elements of the user interface and rebuild the 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') )) ) )) 
+5
source

If you attach each input slider under responsive observation, you can achieve your goal. This not only works for two inputs, but also for multiple inputs. Let me explain with an example:

 observe({ # Create a reactive relationship with slider1 input$slider1 # Update the second slider - slider2 updateTextInput(session, "slider2", NULL, input$slider1) )} 

Similarly, for the second input, you will need to repeat the code:

 observe({ # Create a reactive relationship with slider2 input$slider2 # Update the second slider - slider1 updateTextInput(session, "slider1", NULL, input$slider2) )} 
0
source

Source: https://habr.com/ru/post/1210600/


All Articles