Typically, controls (such as combo boxes) are used to dynamically filter raw data that goes into a visualization to make it interactive, see How to Create a Combo Box Filter. Additionally, controls can also change the actual variable that is being shown in the visualization. This article describes how to use a combo 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
- A data set with at least two categorical (Nominal) variables to choose between. If you want to swap between variable sets with multiple variables see How to Dynamically Change Binary or Numeric Variable Sets in an Output Based on a Control Box.
- 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 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 General > 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 > Nominal. 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, going to General > General, and inputting the desired text (e.g., Dynamic_Variable).
- Update and input the below R Code:
The above code references the variable Label and looks up the selected value in our Control box (e.g. "Age"), and when matched returns the data from the referenced variable (e.g. 'What is your age?'). If you are referencing the variable Name you do not need the apostrophes, see the example below: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?`
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 'Age', 'Income', and 'Gender', 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") Q1 else
if (Question.control.box=="Income") Q2 else
if (Question.control.box=="Gender") Q3 - 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.
Next
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