This article describes how to connect filter variables to an existing Control using R Code. This approach is suitable if you have already created a Control and wish to connect it to a Filter variable. If you are creating a Filter control from scratch see: How to Create a Combo Box Filter or How to Create a List Box Filter).
- A Control Box.
- A variable to reference in the filter (eg Age).
- For multiple response data:
- Each response has a separate variable for each alternative in the combo box.
- The labels are exactly aligned.
- The variable set is of structure Binary - Multi.
- Drag and drop the variable you wish to use to filter (eg Age) from the Data Sets tree onto the page to create a table.
- Go to the Anything menu, and select Page Design > Control > Combo Box (Drop-down) or List Box.
- Select the control and click on the Control tab in the object Inspector.
- Remove the default text in the Item List and select the table you created in Step 1 from the Items from the dropdown menu.
- OPTIONAL: Set Selection mode to Multiple Selection.
- In the Anything menu select Data > Variables > New > Custom Code > R - Numeric.
- Input the desired Label (eg Age filter).
- Check Usable as a filter.
- For single response variables update and input the below R Code:
Age %in% Combo.boxFor multiple-response variables update and input the below R Code:
rowSums(variable.set.name[, Combo.box, drop = FALSE]) > 0You will need to replace the mention of Age / variable.set.name with the name of the variable, you want to use to filter. You will also need to replace the mention of Combo.box with the name of the control you are using. You can find the name of the control by selecting it and going to the object inspector, Properties tab > General > Name.
- If you have your code connected to a single-response variable with overlapping NET categories, you will need to set this up instead as a set of binary variables. You can easily do this by selecting your variable in the Data Sets tree and going to Anything > Filter > Filters From Selected Data in the toolbar. You will then need to adjust the filter formula to reference these new variables using the multiple-response format above.
- Select the output you wish to filter, go to the object inspector > Inputs > FILTERS & WEIGHT > Filter(s), and select the variable you created from the list.