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:
Requirements
Please note these steps require a Displayr license.
- 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.
Method
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 > 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., Age; Income; Gender). 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 one of the variables in the Data Sources, 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 Sources.
- 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=="Age") `What is your age?` else
if (Question.control.box=="Income") `What is your income?` else
if (Question.control.box=="Gender") `What is your gender?`
if (Question.control.box=="Age") Q1 else
if (Question.control.box=="Income") Q2 else
if (Question.control.box=="Gender") Q3 - 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 Sources 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.
See Also
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