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.
Requirements
- 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.
Method
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 the Anything icon > 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 General > 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 Sources pane and click + > Custom Code > R - Numeric. A new numeric variable called newvariable will appear in the Data Sources tree.
- In General > 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 General > R CODE and modify as needed:
####Based on the option selected in the control box change the values to the
####corresponding weight
#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 Sources 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 Data > FILTER(S) & WEIGHT > Weight.
Next
How to Use the Same Control on Multiple Pages
How to Switch Between Tables via a Control Box
How to Use a Control Box as an Input for Calculations
How to Dynamically Change a Question Based on a Control Box
How to Hide or Show an Output based on a Control
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