This article describes how to include a Total sample category within an R filter variable that is connected to a single selection control box.
Requirements
- A control box (created via Anything (+) > Page Design > Control) with Control > Selection mode set to Single selection.
- An R filter variable that references this control box.
See the "Method - manual" section of How to Create a Combo Box Filter for instructions on how to set up the required elements.
Method
In this example, we have a single R filter variable connected to a control box (Household.Status.combo.box) which is based on the Status variable. The logic is as follows:
Status %in% Household.Status.combo.box
To add a Total sample category to the above, you can follow these steps:
1. Select the combo box control on your page.
2. Add Total sample to your control box via Control > Item list in the object inspector.
This will place Total sample at the top of your control item list.
3. Select your filter R variable in the Data Sets tree and go to the R CODE section in the object inspector.
4. The general way for setting up a Total sample option for mutually exclusive data is to do so via an if/else statement in your code:
if (Household.Status.combo.box == "Total sample") {
rep(TRUE, length(id))
} else {
Status %in% Household.Status.combo.box
}
- Here, we need to reference the Total sample selection item by wrapping our existing condition within an if/else statement.
- To do this, we can use the rep function and reference the length of a variable which matches the total number of records in the file, i.e. id.
- This will then generate a TRUE or 1 for every record in the data set if Total sample is selected, otherwise it will return the individual filter category.
5. Once you've updated the code above to reference your specific combo box and variables, click Calculate.
Next
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 (Drop-Down Control) With a Dynamic List