Control boxes are used to make dashboards interactive. They are most commonly used to select filters for outputs, but can also be used to change variable sets used in outputs, show/hide outputs, and do other things. This article describes how to use a Control Box to apply different weightings to outputs.
- At least two Numeric variables to use as the different weights.
- A Control Box control using Selection mode > Single Selection.
- An output (e.g., tables, chart, visualization) to control.
In this example we will create a new weight variable using R that will take on the values of the specific weight variable that is selected in the Control Box. Then apply this variable in the Weight dropdown on applicable outputs.
- First create your Control Box with the Item list corresponding to the different weights you want to switch between. For our example, click Anything > Page Design > Control > Combo Box (Drop-down).
- With the control selected go to the object inspector > Control > Item List and input the weight labels you wish to appear in the list (e.g., Gender; Age; Income). All items in the list should be separated by a semi-colon (;).
- Change Selection mode > Single Selection.
- [Optional]: Change When item lists changes > Select first.
- Still, in the object inspector go to the Properties > General > Name and input the desired name (e.g. Weight.control.box) to use when referencing the Control Box in the R code.
- Now we will create a new R variable that will dynamically change between the individual weights based on the combo box. Hover over a variable in the Data Sets pane and click + > Custom Code > R - Numeric. A new numeric variable called newvariable will appear in the Data Sets tree.
- In Properties > GENERAL, change the Label and Name of the newvariable to something more specific (e.g., Weight_variable).
- Check Usable as a weight.
- Paste in the code below in Properties > R CODE and modify as needed:
####Based on the option selected in the control box change the values to the
#Change Weight.control.box to the name of your control box used to change the weight
#Change Gender Age and Income to the items in your control box
#change weight_gender weight_age weight_income to the names of the corresponding
#weight variables in your data set
if (Weight.control.box=="Gender") weight_gender else
if (Weight.control.box=="Age") weight_age else
if (Weight.control.box=="Income") weight_income
- To check the variable is working correctly, select it from the Data Set tree and drag it onto the page to create a table. Change the selection in the Control box to confirm it's working correctly.
- On any outputs you'd like to weight, select your weight variable (e.g. Weight_variable) in Inputs > FILTER(S) & WEIGHT > Weight.