This article describes how to use a Control Box to select between different weight variables.
- A data set with at least two single-response variables to chose between.
- At least two numeric variables to be used as weights.
- A Control Box control (see How to Create a Combo Box Filter or How to Create a List Box Filter).
- An output (e.g., tables, chart, visualization) to control.
- Go to the Anything menu, and select 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 to Single Selection.
- Change When item lists changes to Select First.
- Still, in the object inspector go to the Properties tab > General > Name and input the desired name (e.g. Weight.control.box) to use when referencing the Control Box in the R code.
- In the Anything menu select Data > Variables > New > Custom Code > R - Numeric. A new numeric variable called newvariable will appear in the Data Sets tree.
- OPTIONAL: Change the Label and Name of the variables by selecting the variable, and going to Properties > GENERAL, and inputting the desired text (e.g., Weight_variable).
- Update and input the below R Code:
if (Question.control.box=="Gender") weight_gender elseThe above code looks up the selected value in our Control box (e.g. "Gender"), and when matched returns the data from the referenced variable (e.g. weight_gender). You will need to update the above code and replace Question.control.box with the name of your control (see Step 5). You will also need to replace the mentions of 'Gender', 'Age', and 'Income' with the items from your Item list (Step 2) and finish each line referencing the numeric variable you wish to use as a weight.
if (Question.control.box=="Age") weight_age else
if (Question.control.box=="Income") weight_income
- Still, in the object inspector go to Properties > GENERAL and tick Usable as a weight.
- 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.
You can now use this variable to weight the results displayed in tables and charts.