JayCo
JayCo

Reputation: 824

Update selectInput on change of another selectInput

I need to change the value of one selectInput, with its own input id, whenever the value of another selectInput, with a different input id changes.

In javascript this would be onchage, but I can't figure out how to interface the two.

jQuery, or R code is OK.

EDIT: Got it to work with this code:

observe({ 
    choices <- input$product
    updateSelectInput(session, "productexperiance", 
                        choices=c("NA", paste(ProductList())), selected="NA")
  })

Still I don't understand why it works.

Upvotes: 3

Views: 3061

Answers (2)

Joe Cheng
Joe Cheng

Reputation: 8061

Use observe and updateSelectInput. Assume one selectInput called col and another called value:

observe({
    choices <- values[input$col]
    updateSelectInput(session, "value", choices=choices)
})

The first line of the observer was just standing in for whatever logic you want to use to decide what the choices are for the second selectInput. values was just a hypothetical vector or list that contains some data.

The important thing is that you access the value of the first selectInput inside the observer; this will cause the observer to re-execute whenever that value changes.

Upvotes: 4

JxAxMxIxN
JxAxMxIxN

Reputation: 1731

Using JQuery (you have to have the JQuery script loaded to use it)

$('#select1').change(function(){ 
    $('#select2').val('1');
});

You'll have to play with it a bit... (not tested)

The # denotes an element by it's ID - The actual ID can be anything (besides select1 or select2)

in my example when select1 is changed select2 will then also be changed.

the '1' in the val() should correspond to an actual value that is available through the other list.

Upvotes: 1

Related Questions