Using Displayr
| 22 August 2017 | by Tim Bock

Adding a Combo Box to a Displayr Dashboard

Using Displayr

combo box can be added to a Displayr document by selecting Insert > Control (More), which causes a combo box to appear in the middle of the screen. The control allows the user to make one or more choices from a list of options. You can then use these choices as inputs into calculations.

Settings

When you select the control, settings appear in the Control tab of the Object Inspector. Settings exist for controlling selected items, formatting, and tooltips.

In terms of functionality, the key settings are Item list and Selection mode. Item list should contain a semi-colon-delimited list of options from which the user can choose. For example: dog; cat; tigerSelection mode governs whether a user can choose one or multiple items from the combo box.

The Properties tab of the Object Inspector has settings for the control’s Name and LAYOUT settings. By default its name is Combo.box.

Using the combo box as an input into calculations

You can refer to combo boxes in R calculations. If you create an R Output (Insert > R Output (Analysis)) using the following code:

 
if(Combo.box == "dog") "A canine was selected" else "A feline was selected"

then this prints “A canine was selected when selecting dog and “A feline was selected” otherwise. Prior to making a selection, the code returns Error: argument is of length zero. To deal with non-selection, we can use the following R code in the R Output:

 
if (length(Combo.box) == 0) "Nothing has been selected" else {
    if(Combo.box == "dog") "A canine was selected" else "A feline was selected"
}

Note that once something has been selected from a combo box where Selection mode is set to Single Selection, the user cannot de-select. So, if you were using the control to filter (e.g., Male; Female) and wanted to have a total option, you need to add it to the list (e.g., Male; Female; Total).

Whatever option(s) you selected when creating the document stay selected when a user accesses the document in View Mode. For example, if you select cat and then publish the dashboard, it will appear with cat selected.

Multiple selection

When you set the Selection mode is set to Multiple Selection, the user is presented with multiple check boxes. If you make this change to the example above, though, and select all three animals, you may be in for a bit of a surprise, with the result being “A canine was selected“. This is because the way that the combo box control works is that any selections are returned as a vector. Thus, when nothing is selected, the length of the vector is 0. When selecting one item the length is 1. In this example, with three things selected, the length becomes 3. As the vector is greater than 0 (it’s 3, in fact), then the first selected item in the list (which is “dog”) will be interpreted as the one to use by the code, resulting in “A canine was selected“.

Restrictions in View Mode

Some restrictions exist as to how combo boxes work in View Mode:

  • When the user changes the combo box, any R Output that refers to this combo box will automatically update. However, any other R Outputs that refer to the R Output that refers to the combo box will not update. Thus, you need to put all your calculations in the R Output that refers to the combo box.
  • You cannot refer to combo boxes in R Variables.
  • R Outputs cannot refer to combo boxes on other pages.

 

To play around with this example, click here. My post titled How to Create an Online Choice Simulator contains a more ambitious example.

Author: Tim Bock

Tim Bock is the founder of Displayr. Tim is a data scientist, who has consulted, published academic papers, and won awards, for problems/techniques as diverse as neural networks, mixture models, data fusion, market segmentation, IPO pricing, small sample research, and data visualization. He has conducted data science projects for numerous companies, including Pfizer, Coca Cola, ACNielsen, KFC, Weight Watchers, Unilever, and Nestle. He is also the founder of Q www.qresearchsoftware.com, a data science product designed for survey research, which is used by all the world’s seven largest market research consultancies. He studied econometrics, maths, and marketing, and has a University Medal and PhD from the University of New South Wales (Australia’s leading research university), where he was an adjunct member of staff for 15 years.

No comment. Share your thoughts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Human? *

Keep updated with the latest in data science.