This article describes how to use a Control Box to change the question (variable) displayed in a table, chart, or visualization. The post contains the steps required to create a Page like this:
- A data set with at least two single-response variables to choose between.
- 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.
Note: each time a Control Box selection changes Displayr will need to recalculate the variable. When working with large data sets this can take time and slow down the Document. In this case, you may want to consider dynamically changing the table rather than the Question. See Method 2 here.
- Go to the Anything menu, and select Page Design > Control > Combo Box (Drop-Down) or List Box.
- With the control selected go to the object inspector > Control > Item List and input the question 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. Question.control.box) to use when referencing the Control Box in the R code.
- Hover over the Data Set tree, select + > Custom Code > R - Numeric, or to the Anything menu and 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., Dynamic_variable).
- Update and input the below R Code:
if (Question.control.box=="Gender") `Q2. Gender` elseThe above code is referencing the variable Label and looks up the selected value in our Control box (e.g. "Gender"), and when matched returns the data from the referenced variable (e.g. 'Q2. Gender'). If you are referencing the variable Name you do not need the apostrophes, see the example below:
if (Question.control.box=="Age") `Q3. Age` else
if (Question.control.box=="Income") `Q32. Income`
if (Question.control.box=="Gender") Q2 elseYou 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 a question you wish to display.
if (Question.control.box=="Age") Q3 else
if (Question.control.box=="Income") Q32
- In Properties > GENERAL > Structure, change the variable structure to Nominal or Ordinal, depending on the type of variables used in the R Code.
- 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 use the table you created and link it to visualization or another type of analysis. Alternatively, you can use the variable itself in different types of outputs, such as charts or visualizations.
How to Create a Combo Box Filter
How to Connect Filters to Controls Using R Code
How to Dynamically Change Binary or Numeric Variable Sets in an Output 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
How to Control Which Calculations Filtered in View Mode
How to Change Weighting using a Control Box
How to Use a Control Box as an Input for Calculations
How to Use the Same Control on Multiple Pages
Article is closed for comments.