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 need to connect it to a Filter variable in order to filter your outputs. 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.
Requirements
- A Control (i.e. dropdown, list box, text box, etc).
- A variable to compare against the option selected in the combo box:
- For a single variable (e.g. Age):
- The categories must match the options in the Control exactly.
- For multiple response data:
- Each response has a separate variable for each alternative in the Control.
- The variable labels match the Control options exactly.
- The variable set is of structure Binary - Multi.
- For a single variable (e.g. Age):
Please note these steps require a Displayr license.
Method
- Create a new variable to filter your outputs based on the combo box. Hover over a variable in the Data Sources tree and select + > Custom Code > R - Numeric.
- From the General tab in the object inspector, input the desired Label (e.g. Age filter).
- Check Usable as a filter under Data > Properties.
- For single response variables and input, modify the below R Code:
#compare values in the variable to the value selected in your Control
For multiple-response variables and input, modify the below R Code:
#replace Variable with the name of the variable with categories to compare
#replace YourControlNameHere with the name of your control (you can also click on it)
Variable %in% YourControlNameHere#check if any of the variables selected by the control are selected in that case (=1)
You can find the name of the control by selecting it and going to the object inspector, General > General > Name.
##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]) > 0 - [OPTIONAL] 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 Sources 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(s) you wish to filter, go to the object inspector > Data > Filters & Weight > Filter(s), and select the variable you created from the list.
Other Considerations
- This filtering is specific to these Controls only. Once you get your filtering setup as you like it, you may want to show and apply the same Controls on multiple pages:
- You can apply the filter variable created to outputs on other pages, but those filters will be based on the specific Controls on the specific page. This is useful if you want a page of filters that will be used to apply to the whole document.
- To show the same Control on multiple pages while keeping the selected items consistent across those pages, see How to Use the Same Control on Multiple Pages.
- Keep in mind any item selected in the Control when you Publish (Share) a Document will be the default item selected in View Mode.
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 Select Which Rows or Columns to Show in a Visualization Based on a Control Box Selection
How to Switch Logos and Images Based on a Control Box Selection