R Shiny selectInput dependent on another input

Symbol of coding on a computer screen

Method 2: use an observe() function with updateSelectInput()

In this method, an observe() function is added with updateSelectInput() which updates the choices for the second selectInput when the first input changes.

We create the first and second (dependent) selectInput() controls with initial (default) values selected.

Steps to create a dependent selectInput using observe() with updateSelectInput()
  1. Add both selectInput() selection controls, with initial values selected;
  2. Create an observe() function, with updateSelectInput() which updates when the first selectInput control changes.

Below is the R code for the observe() function. There are two slight variations, using observe()

observe({ updateSelectInput(session,
 inputId = "choose_item",
 choices = unique(choices_table
 [choices_table$category == input$choose_category,"items"]))

… or using observeEvent()

  [choices_table$category == input$choose_category,"items"]))}
Symbol of coding on a computer screenMethod 2: observe() and updateSelectInput – example code

Paste the code below into RStudio and save the file as an R Markdown file, .Rmd. This example also shows how to add layout formatting using inputPanel().

For details of the variable names, see above.

title: "Shiny selectInput based on another selectInput"
runtime: shiny
output: html_document

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, error=TRUE)


```{r create_table_of_choices, eval=TRUE}

# table of choices for the selectInput controls
# note there's a duplicate item 'Pear'

  x <- c("Fruit","Animal","Animal","Fruit","Fruit","Animal","Animal","Fruit")
  y <- c("Pear","Dog","Cat","Apple","Banana","Monkey","Giraffe","Pear")

#bind x and y into a dataframe with two columns, using cbind() (column bind)
  choices_table <- as.data.frame(cbind(x,y))
  colnames(choices_table) <- c("category","items")

# look at the table of choices
#   choices_table
#   choices_table$category


```{r observe_updateSelectInput event, eval=TRUE, error=TRUE}

# create both selectInput selection control
# in this example we also show how to add the controls
#  into a formatted input panel

# start of inputPanel
            label="Select category",

            label="Select item",
# end of inputPanel


output$tb_chosen <- renderTable(subset(choices_table,choices_table$category==input$choose_category & choices_table$items==input$choose_item), rownames=TRUE)

# observe function which updates the second selectInput when the 
#  first selectInput is changed
 observe({ updateSelectInput(session,
                            inputId="choose_item", choices=unique(choices_table[choices_table$category == input$choose_category,"items"])


>> On the next page, we show the second variation of Method 2 using observeEvent() with updateSelectInput()

Pages: 1 2 3 4