Control boxes are a popular way for users to change things on a Displayr page. This post will show you how to use a control box to dynamically apply weights and filters to tables and visualizations.
The two main types of control boxes are the combo and the list box. Typically they are used for changing how the data is filtered, as discussed in this post. But you can also use a control box to change the actual question in a table (or chart, visualization, etc.). You can also use control boxes to change the weighting you want to apply.
For example, the image below shows a question (Preferred Cola) that I've chosen to split by income brackets, using the selection in the control box.
If I change the control box option to Age, it becomes:
You can do this with an R variable. The R variable dynamically updates when the selection in the control box changes. The purpose of this post is to show, via example, how you can do this. Currently, this technique only works with single-variable questions (either categorical or numeric).
Setup your control box with your options
Use Insert > Control and then choose either a Combo or List box. Over in the Object Inspector, list your questions in CONTROL > Item List (which can be labeled however you like). In this example, I entered 4 possible options for a combo box:
I set the Selection mode to be "Single selection," and When item list changes to be "Select first."
Be sure to take note of the control box’s name under PROPERTIES > General > Name, because we’re about to use this in the R variable.
Create an R variable with conditional statements that link to the questions
Then create an R variable with Insert > New R > Numeric variable. This will make a new numeric variable under Data Sets, creatively called “newvariable” by default. Displayr will reveal in the Object Inspector a blank box where you can put in the R CODE:
As per the picture above, you enter simple conditional statements with R. Basically, it references the control box (called
Combo.box in this example) and then each of the 4 options. The four variable names --
d4 -- pertain to each of the single-variable questions to use in the table. The code consists of very straightforward "IF and ELSE IF" statements.
Be sure to change the variable Structure to be nominal or ordinal (if you intend for the question to be categorical). This is done under INPUTS > Structure in the Object Inspector for the R variable (in the picture above at the very bottom under the code).
And that’s it! From there you can use your R variable in a table, directly in a visualization, or in another analysis. It will change dynamically as you alter the selection in the control box.
Changing the weighting dynamically with an R variable
You can apply the same technique to dynamically change the weighting. You essentially reference different weighting variables in the R code based on your selection in the control. For example:
if (Combo.box == "USA") weight_us else if (Combo.box == "France") weight_fr else if (Combo.box == "UK") weight_uk
Then make sure the R variable has the Usable as weight box checked in the Object Inspector. You can then apply that to a table (or chart or whatever) as your weighting variable.
Try for yourself
The above example is captured in this Displayr document. The R variables are the first two variables in the Data Set.