Combo Boxes (controls) can be used in Displayr to allow users to select data and other outputs of interest. Sometimes it is useful to create controls where the list of options is dynamic, updating based on either another control or some aspect of the data (e.g., whether it is filtered or not). In the example that I illustrate in this post, the list of countries is conditional on which continent has been selected.

Worked example

In this example I start with a table, called country.lookup, which contains countries by continent. This was created by selecting Insert > Paste Table, pasting in a table, and changing its name in Properties > GENERAL > Name to country.lookup.

Creating a control from an R Output

An R Output is an arbitrary calculation in Displayr. A control is created from an R Output as follows:

  1. Create an R Output that contains a character vector of the options for the control. In this example, this is done by
    • Clicking Insert > R Output
    • Typing continents = unique(country.lookup$Continent), which extracts all the unique continents from the Continent column of the table.
    • Pressing  CALCULATE  
  2. Insert > Control (More) and set:
    • Properties > GENERAL > Name to cContinent
    • Control > Items from R Output to continents
    • Delete entries in Control > Item list
  3. Click on the control and select any country (the rest of this post assumes you selected Asia)

Creating a control that is linked to the first control

Now we will create a second control, the contents of which are linked to the user's selections in the first control. This basic approach can be extended to much more complicated examples, allowing users to drill into large databases.

  • Click Insert > R Output and insert the following code, which selects all the countries int the selected continent:

    countries = country.lookup$Country[country.lookup$Continent %in% cContinent]

  • Insert > Control (More) and set:
    • Properties > GENERAL > Name to cCountry
    • Control > Items from R Output to countries
    • Delete entries in Control > Item list
    • (Optionally) Set Selection mode to Multiple Selection
  • Click on the control

If everything has gone to plan, your screen will now look like this (if you do this without having published using Export, you will get some little circles on either side of the control).

Check out more handy tips on Using Displayr!

Click here for an interactive tutorial on interactive controls