Introduction
This article describes how to combine the code within two or more R filter variables that are connected to control boxes into a single filter variable.
Requirements
- Two or more control box filters setup for either method in How to Create a Combo Box Filter. This includes:
- Two or more control boxes (created via the Anything icon > Page Design > Control).
- Two or more R filter variables that reference these control boxes.
Please note these steps require a Displayr license.
Method
In short, this is as easy as copying the original code from both variables and combining them with the & (AND) symbol.
1. In the Data Sources tree, hover over a variable and click Plus (+) > Custom Code > R-Numeric.
2. Give it a Label such as Combined Filter.
3. Check the Usable as Filter checkbox.
4. In the Data Sources tree, select the first filter that you want to combine and copy the code under General > R CODE into the General > R CODE of your new Combined Filter.
5. In the Data Sources tree, select the second filter that you want to combine and copy the code under General > R CODE.
6. In the General > R CODE of your new Combined Filter, type &
(for AND) and then paste in the filtering code form the second filter variable.
Example #1: Below is two R code snippets for an Age filter and Gender filter respectively:
Age %in% combo.box.age
Gender %in% combo.box.gender
The R CODE for the Combined Filter is:
Age %in% combo.box.age & Gender %in% combo.box.gender
Here, the logic is looking at whether the selection in combo.box.age matches the category in the Age variable AND the selection in combo.box.gender matches the category in the Gender variable.
Example #2: This process will work on any type of filter, using both a multiple and single-response filter - such as Brand Awareness and Income is below:
rowSums(`Brand Awareness`[, combo.box.brand, drop = FALSE]) > 0 & Income %in% combo.box.income
Here, the logic is looking at whether the selection in combo.box.brand has been selected in the Brand Awareness variable set AND the selection in combo.box.income matches the category in the Income variable.
Notes
Keep in mind, the final result of any combined filter must be a TRUE/FALSE or 1/0. By using the &
operator, the result will only return TRUE if both conditions are TRUE. If you'd like to add in an OR condition, you can use the |
symbol.
See Also
How to Work with R in Displayr
How to Connect Filters to Controls using R Code
How to Create a Combo Box Filter
How to Create a List Box Filter
How to Create a Combo Box With a Dynamic List