Sometimes you may want to add interactive filtering directly to a page of your document. You can do this using Controls, such as a Combo Box (dropdown) control and use it as a filter. Once you understand How Control filtering works and the Requirements, you can choose to create your filtering using the Automatic or Manual method below.
This 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.
How Control filtering works
There are three components to setting up basic control filtering in your document, regardless of whether you use the Automatic or Manual method below. They are:
- A Control - can be any control listed in the Anything > Page Design > Control menu. The settings that can be configured for a control are discussed below.
- A Filter Variable - an R variable that compares the Control selections against a variable to create a filter. How to Connect Filters to Controls Using R Code details how to create these manually, but an example of one is below.
- Applying the Filter to Output(s) - the filter variable will be available in the Filter(s) dropdown on outputs. To have the combo box filter the output, select its filter variable from the Filter(s) dropdown list.
Below is a snapshot of these components created through the Automatic method in a document:
All three components are created simultaneously using the Automatic method, but the selections do not automatically update if data changes later (though you can fix this afterward).
Requirements
Please note these steps require a Displayr license.
- Objects on the page to filter (e.g., table, chart, visualization, etc.).
- Nominal or Binary - Multi variable set(s) to use as a filter for Method - Automatic, or if using the Method - Manual a Binary-Multi (Compact) variable set can also be used.
- Other Considerations to be aware of are listed below.
Method - Automatic
- Select the output(s) you wish to filter with the Combo Box.
- Go to the Anything menu and select Filter > Control > Combo Box (Drop-Down) Filters on an Output.
- Select the variable sets you wish to use as a filter. Each variable set will have its own combo box created, but only one filter variable will be created that is connected to the combo boxes being created.
- 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.
What happens:
- A Combo Box control for each variable set will appear on the Page. You can customize the appearance of the box and adjust other settings in the Control tab. The combo box will have the categories for selection hardcoded in the Item list, but if you expect to rename or change these categories later on, you should use the Items from field (see the Manual method below on how to do this and further customize the control).
- Displayr will create a single Combo Box Filter variable linked to the Combo Boxes that were created.
- Displayr will also automatically apply the Combo Box Filter variable to all the outputs selected in Step 1, though you can manually select it in the Filter(s) dropdown on other outputs in the document to filter them; see How to Apply a Filter.
Method - Manual
Step 1: Create and configure your Combo Box control
Step 2: Create the filter and link it to the Combo Box control
Step 3: Apply the filter to desired outputs
Step 1: Create and configure your Combo Box control
- Go to the Anything menu and select Page Design > Control > Combo Box (Drop-down).
- Use either Item list or Items from field to supply the list of categories to show as selections. These labels must match EXACTLY the variable sets that will be used to create the filter.
- Item list - this is a hardcoded list of selections separated by a semicolon ;.
- Items from - you can supply a table to show its row headers as selections or have it reference an R Calculation showing a vector of possible selections. You can also link the list to a variable set, but this is not recommended. These outputs will update when the data changes, and if any categories change, so will the options in the combo box.
- We'll use the Items from field in our example. In the Data Sources, select the Nominal or Binary-Multi variable you wish to use as a filter.
- Create a table by dragging and dropping the variable from the Data Sources onto the Page.
- 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).
- Specify a Selection mode:
- Multiple selection will allow the user to select more than one category in the Combo Box.
- Single selection will allow the user to select one category only.
- Confirm Must have selection:
- Yes will not allow the user to uncheck Select All to remove all the selections from a Multiple selection control.
- No will allow the user to uncheck Select All to remove all the selections, but unless the R filter variable (Step 2 below) is set up to handle this, it will filter out all respondents until something is selected.
- If Multiple selection is selected, confirm the Minimum selection and Maximum selection settings. These settings control the minium and maximum amount of items that are selectable in the combo box. When Must have selection is set to Yes, the minimum will always be set to 1. Leave Maximum selection blank to allow users to select as many items as possible.
- Confirm When item list changes, which determines what categories are selected if the Item list or Items from object changes (which can be done dynamically if desired):
- Remove all selections (only available if Must have selection is No).
- Keep current selection will keep the current item selected if it is on the new list.
- Select all (only available with Multiple selection) will select all the items in the new list.
- Select first (only available with Single selection) will select the first item in the new item list.
- If creating more than one combo box for filtering, you should also customize the Properties > General > Name of the combo box so it's easier to understand where it is used in your filter code in (Step 2 below).
- OPTIONAL: You can use the Font and Border fields to change the appearance of the combo box on the page.
- OPTIONAL: to change the text appearing on hover (if something is already selected in the Combo Box) input the desired text into Tooltip (e.g., Select the age categories to filter).
- OPTIONAL: to change the text appearing in the Combo Box, input the desired text into Placeholder (e.g., Select the age categories to filter). The Placeholder text is shown in a combo box when nothing is selected in the box.
Please note: It is not currently possible to modify the "All" text shown when all the categories are selected in a multiple-selection combo box.
Step 2: 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).
- Check Usable as a filter.
- Paste in one of the following code snippets in R CODE based on the structure of your filter variables, and replace the names to reference the items in your document (you can use the Properties > General > Name or click on your items to insert them into the code).
- Using a Nominal variable to filter: Where Age refers to the Name of the variable you wish to use to filter, and Combo.box refers to the Name of the Combo Box control.
#compare values in the variable to the value selected in your Control
For example:
#replace Variable with the Name of the variable with the categories
#replace YourControlNameHere with the name of your control (you can also click on it)
Variable %in% YourControlNameHere
Age %in% Combo.box
- Using a Binary-Multi or Binary-Multi (Compact) variable set for a multiple-response question:
#check if any of the variables selected by the control are selected in that case (=1)
Where Awareness is the variable set used to filter, and Combo.box.Aware refers to the Name of the Combo Box control.
#replace VariableSetName inside the backticks with your variable set Label
#replace YourControlNameHere with the name of your control (you can also click on it)
rowSums(`VariableSetName`[, YourControlNameHere, drop = FALSE]) > 0rowSums(`Awareness`[, Combo.box.Aware, drop = FALSE]) > 0
- Using a Nominal variable to filter: Where Age refers to the Name of the variable you wish to use to filter, and Combo.box refers to the Name of the Combo Box control.
- If you created more than one combo box, you can link together the criteria for each using & in the R code such as below; see How to Combine Multiple R Filter Control Variables into a Single Variable for more detail:
Age %in% Combo.box.Age & rowSums(`Awareness`[, Combo.box.Aware, drop = FALSE]) > 0
Step 3: Apply the filter to the desired outputs
- Select the item(s) you wish to filter on any page in your document.
- Go to object Inspector > Inputs > Filters & Weight > Filter(s), and choose the filter, see How to Apply a Filter for more detail.
In the end, you will end up with the three components: 1) A combo box(es), 2) A filter variable, and 3) Outputs with the filter variable applied.
Other Considerations
- This filtering is specific to the combo box on this page only (though you can apply the filter to outputs on any page). When you Duplicate the page, only the 1st (combo box) and 3rd (filtered output) components of the filtering will be duplicated, but NOT the 2nd (filter variable) - thus, the filtering will not work on the duplicated page. There are a few ways of handling this if you want to use combo box filtering on multiple pages:
- You can apply the filter variable created to outputs on other pages. Those filters will be based on this specific Combo box on this specific page. This is useful if you want a page of filters that will be used to apply to the whole document.
- In addition to #1 above, you can also show the Combo box on multiple pages while keeping the selected items consistent across those pages; see How to Use the Same Control on Multiple Pages for how to do this.
- To have exclusive combo box filtering for each page (which is not recommended as it may slow down your document if done repeatedly), you will need to Duplicate the Page, Duplicate the filter Variable, and edit the R code of the filter to refer to the new duplicated Combo Box Name, see How to Connect Filters to Controls Using R Code for more detail.
- Any item(s) selected in the Combo Boxes when you Publish a Document will be the default item selected in View Mode.
- You can use the combo box filter variable on Hidden outputs, and they will still be updated in View Mode based on the combo box selections.
- If you want to filter your output by only filtering in certain rows/columns, you can one of the methods in How to Filter Rows and Columns in Visualizations and Tables Without Code.
Next
How to Use the Same Control on Multiple Pages
How to Create a Combo Box (Drop-Down Control) With a Dynamic List
How to Dynamically Change a Question Based on a Control Box
How to Filter Rows and Columns in Visualizations and Tables Without Code
How to Switch Logos and Images Based on a Control Box Selection
How to Combine Multiple R Filter Control Variables into a Single Variable
How to Build Tables that Automatically Filter to the Latest Periods
How to Connect Filters to Controls Using R Code