You can always use the Items From field to on a combo box to link the combo box to a variable or output that can update with new options. However, sometimes you may want to setup filtering that has options specific to other filters selected. This could be regions specific to a country selected or products specific to brands. This article describes how to create a Combo Box drop-down (you can also do this with a List box) where the list of options is dynamic, updating based on the selection in another Combo Box. As an example, this post contains the steps required to dynamically update the Country Control Box list based on the selection in the Continent Control Box.
Requirements
- A lookup table containing all the combinations of the values appearing in the Combo Boxes:
- A variable to use to filter based on the more granular options (Country above).
- [Optional] If the more granular options are listed multiple times, you will also need a variable in the Data Set to match to the larger combo box (i.e. if two brands have a product called the same thing, you will need a variable for product and brand in your Data Set).
- The labels used in the lookup table above, must exactly match the category labels in the variables.
Method
- Start by creating a lookup table consisting of the list of values you wish to appear in the Control Boxes. To create a table go to Table > Paste or Enter Table.
- In the object inspector, click Data > DATA SOURCE > Paste or type data.
- In the first column add the values to appear in the first Control Box with the more high-level options, and in the second column the values to appear in the second Control Box for the dependent options that correspond to each in the first. Ensure you add Column labels (e.g. Continent, Country). Select OK.
- Select the table and go to the object inspector > GENERAL > General > Name and define the table's name. In this example, I named the table country.lookup.
- Now we need to create an output that will only show the list of unique options for our first control box found in the first column of our lookup table. Go to Calculation > Custom Code and draw a box on the Page.
- In Data > R CODE, paste in the below R Code:
#return unique options in the first column of the lookup table to give to the combo box
The code in this example looks like this:
#highlight table_name$First_Column_name and click on the first header in your lookup
#table to insert the correct reference
#replace box1vals with a more specific name
box1vals = unique(table_name$First_Column_name)continents = unique(country.lookup$Continent)
- Click Calculate.
- Now we will insert our first combo box to use for the first list of options (Continents in our example). Click Anything > Page Design > Control > Combo Box (Drop-down).
- Select the Combo Box and go to the object inspector > General > General > Name and give it a unique name for what it filters. In this example, I named it cContinent.
- Hook up the combo box to the Calculation we created with the unique options in column 1. Go to Control > Items from and select the Calculation you created in Step 4 (in this example continents).
- Delete the default text in Control > Item list.
- Now we need to create an output that will show the list of unique options for our second control box (from our second column in the lookup table) based on the item(s) selected in our first combo box. Go to Calculation > Custom Code and draw a box on the Page.
- In Data > R CODE, paste in the below R Code:
#create output to show options for second control box based on item selected in first
In our example the code would look like this:
#replace box2vals below with a more specific name of options
#table_name is the name of your lookup table
#Second_Column_name and First_Column_name are the headers for those columns in the table
#ComboBoxName is the name of your first combo box in step 9
box2vals = table_name$Second_Column_name[table_name$First_Column_name %in% ComboBoxName]countries = country.lookup$Country[country.lookup$Continent %in% cContinent]
- Now create the second combo box to use for filtering (to filter Country in our example). Click Anything > Page Design > Control > Combo Box (Drop-down).
- Select the Combo Box and go to the object inspector > General > General > Name and define the control's name. In this example, I named it cCountry.
- Then click into Control > Items from and click on the Calculation output showing the dynamic options from the second column of the lookup table (in Step 12, in this example countries).
- Delete the default text in Control > Item list.
- [Optional]: Set Selection mode to Multiple Selection.
- Follow the steps in How to Connect Filters to Controls Using R Code to learn how to create the Filter variables for these combo boxes that you can use to filter your outputs.
Next
How to Use the Same Control on Multiple Pages
How to Connect Filters to Controls Using R Code
How to Dynamically Change a Question Based on a Control Box
How to Switch Logos and Images Based on a Control Box Selection