This article describes how to create a Combo Box control and use it as a filter. The article contains the steps to take you from a Page containing a grid of bars chart....
...to a Page containing a Combo Box that can be used to filter the chart.
- A Displayr document containing a data set.
- A Displayr data object to filter (e.g., table, chart, visualization, etc.).
- A single-response variable to use as a filter (automatic method only).
- A Binary-Multi variable to use as a filter (manual method only).
Method - automatic
- Select the output(s) you wish to filter with the Combo Box.
- Go to the Anything menu and select Filtering > Combo Box (Drop-Down) Filters on an Output.
- Select the single-response variable(s) you wish to use as a filter.
- Select Yes to allow the user to select more than one category in the Combo Box control, or select No to allow the user to select one category only.
A Combo Box control will appear on the Page. Displayr will create a new Combo Box Filter variable linked to the Combo Box selection. Displayr will also automatically apply the filter variable to all the outputs selected in Step 1.
Method - manual
To create a Combo Box control:
- In the Data Sets tree select the single-response or Binary-Multi variables you wish to use as a filter.
- Create a table by drag and dropping the variable from the Data Sets tree onto the Page.
- Go to the Anything menu and select Page Design > Control > Combo Box (Drop-down).
- Select the Combo Box control, and go to the object inspector > Control > Item list > remove the default text.
- In the Items from the dropdown menu select the table created in step 2 (e.g., Age).
- In the Selection mode dropdown menu select Multiple selection to allow the user to select more than one category in the Combo Box, or select Single selection to allow the user to select one category only.
- OPTIONAL: to change the text appearing in the Combo Box, input the desired text into Placeholder (e.g., Select the age categories to filter).
To create the filter and link it to the Combo Box control:
- Go to the Anything menu and select Data > Variables > New > Custom Code > R - Numeric.
- Set the filter Label (e.g., Age filter).
- Set the R Code as per below, where Age refers to the name of the variable you wish to use to filter, and Combo.box refers to the label of the Combo Box control.
Age %in% Combo.boxIf you are using a different variable or a Combo Box control with a different name you will need to amend the R code accordingly.
- If you are filtering with multiple response data, the R code is, replacing variable.set.name with the name of your variable:
rowSums(variable.set.name[, Combo.box, drop = FALSE]) > 0
In order for the above code to work, each alternative used in the Combo Box control needs to be saved in a separate variable, using aligned labels and the variable set as Binary-Multi.
- To apply the filter, select the item that you wish to filter, go to object Inspector > Inputs > FILTERS & WEIGHT > Filter(s), and choose the filter.