Control boxes can be used to dynamically change a question to be displayed as an output. They can also be used to change the weighting, to control which output to show or hide, or to switch between images.
This post describes how a Control can be used as an input for custom Calculations, as per the below:
Requirements
Please note this requires the Data Stories module or a Displayr license.
- A Control Box control configured to your desired options (see How to Create a Combo Box Filter, How to Create a List Box Filter, How to Insert a Date Control, and How to Create a Text Box Filter for details). In this example we created a single selection Combo Box with the Item List options Canine, Feline and When item list changes > Select first so there's always an option selected for the code:
- Knowledge of How to Work with Conditional R Formulas.
Method
- With your control box selected, in the General > GENERAL > Name give it a specific name for use in your R code (e.g. Calculation.control.box).
- Go to Calculation > Custom Code and draw a box for the output on the Page.
- Paste in and update the code below in R CODE:
#change the output of the Calculation based on a control box
#replace Calculation.control.box with the name of your control box from step 1
#replace "Dog" with one of the options you'd like to setup first
#replace "A canine was......" with whatever you'd like to return for option1
#add in more else if(...) options as needed
#if Dog is selected in the control box
if(Calculation.control.box == "Dog"){
#print the following text
"A canine was selected"
} else {
#print the following text if dog is not selected
"A feline was selected"
} - [Optional]: If nothing is selected in the control or you want to be able to leave the control blank, you might get the error message:
To correct this, you can use the below R code, which first checks if a selection was made in our Control box, and if not, returns a different calculation (e.g. a string value in our case).
#change the output of the Calculation based on a control box
#replace Calculation.control.box with the name of your control box from step 1
#replace "Dog" with one of the options you'd like to setup first
#replace "A canine was......" with whatever you'd like to return for option1
#add in more else if(...) options as needed
#if nothing is selected in the control its length ==0 show a message if so
if (length(Calculation.control.box) == 0) {
#print the following if nothing was selected
"Nothing has been selected"
}else if(Calculation.control.box == "Dog"){ #if Dog is selected in the control box
#print the following text
"A canine was selected"
} else { #if something is selected and it isn't Dog print the following
#print the following text if dog is not selected
"A feline was selected"
}
Next
How to Connect Filters to Controls Using R Code
How to Hide or Show an Output based on a Control
How to Dynamically Change a Question Based on a Control Box
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 the Same Control on Multiple Pages