How to update one selectInput w.r.t to other. These inputs store values of two columns from a single data frame.

I have two selectInputs for column 1 and column 2 resp. of a data frame

ui.R

fluidPage(){
fluidRow(

selectInput("IP1","input1", choices = table$Column1)

selectInput("IP2","input2", choices = table$Column2)


),

fluidRow(

dataTableOutput("table1")
)

}

server.R file is

observe({
updateSelectInput(session,"IP2",selected = table$Column2[table$Column1==input$IP1]  #  How to update the input w.r.t second input
updateSelectInput(session,"IP1",selected = table$Column1[table$Column2==input$IP2]  # This one loops the value b/w previous one and new one.

tableOP<- subset(table,Column1== input$IP1)

output$table1<-renderDataTable(tableOP)
})

On Googling I found that functions isolate() or observeEvent() can be used. But I don't know where and how. I tried using them but didn't get the results.

Please, help me learn this method.

Thanking you for your support.:hugs:

@jcheng has a gist that uses modules to update select inputs dependent on other selections. You could try to adapt to your use case.

Thank you for the link and support.
I tried using the methodology to the best of my knowledge.
But, my need is slightly different.
In the link the selectInputs are filtered to values available on first choice.
But my need is that the selectInputs do not filter out values but only, select the value which is related to the first selection. ( The first selection can be from any of the input).

Like

selectInput("IP1","Input 1",choices = c("Option A","Option B","OptionC")
selectInput("IP2","Input 2",choices =c("Option 1","Option 2","Option 3")
# if I select "Option A" from IP1
#Option1 of IP2  is automatically selected "NOT REMOVE REMAINING OPTIONS"
#OR, if I select "Option 1" from IP 2 "Option A" is automatically selected in IP1 

# This selection must be reproducible i.e. I can do selection any number of times.
```
Thanking You for your help.

Ah ok, got you. In that case it's a bit simpler.

You just have to put each updateSelectInput in it's own observeEvent that is triggered by the other input.

Also, to avoid both observers triggering at the same time on launch (which would send the updates into an infinite loop of updating each other if the initial selected values didn't correspond with one another) you should add an ignoreInit = TRUE to one of the observers.

Example below.

library(shiny)

df <- data.frame(
  col1 = c("Option A","Option B","OptionC"),
  col2 = c("Option 1","Option 2","Option 3"),
  stringsAsFactors = FALSE
)

ui <- fluidPage(
  selectInput("IP1","Input 1", choices = df$col1),
  selectInput("IP2","Input 2", choices = df$col2)
)

server <- function(input, output, session) {
  
  observeEvent(input$IP1, {
    updateSelectInput(session, "IP2", 
                      selected = df[df$col1 == input$IP1, "col2", drop = TRUE])
  })
  
  observeEvent(input$IP2, ignoreInit = TRUE, {
    updateSelectInput(session, "IP1", 
                      selected = df[df$col2 == input$IP2, "col1", drop = TRUE])
  })
}

shinyApp(ui, server)
1 Like

Just a comment about this approach, if you restrict the options this way, what is the point in having two inputs? would not be simpler to just have both combined? because you can't get other combinations anyways.

selectInput("IP","Input",choices = c("Option A-1","Option B-2","Option C-3")
1 Like

Actually, the two options are important selection factors for getting required table.
Some people in my work, are familiar with Options (A,B,C), rest are with Options (1,2,3). In general I could have subset the table with Options (A,B,C) OR (1,2,3) but, for sake of simplicity for all people I thought of providing both inputs.

Also, in actual dataset Options (A,B,C) have more than one combinations in second input i.e. Options(1,2,3) like, (A-1,A-2,A-3). So, a second level of drill down is applicable here. Like someone can choose Option A and Option 2 to get required table output.

Thank you for your time and concern.:slightly_smiling_face:

Thank You for the help, I was not familiar with observeEvent() with ignoreInit argument. Thank You for teaching me that.

I got the desired output and the "killing loop" did not occur.

Just, if I can bother you one last time,

On applying the above methodology the selectInputs are not reproducible. I mean that once I have done the selection I have to restart the app to make another selection.

I can make a resetInput button using observe() but, still it would mean same as resetting the app.

I am using selectInput(…,multiple ==TRUE) # in both the inputs.

By any means can I just use Backspace to remove the selections.

Which generally work when the app is simple. But, due to slight complexity this backspace is not working.
Because the drop down choices are getting removed and only selected choice is visible.

Anyway, Thank You and Grateful for your support.:hugs::slightly_smiling_face:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.