Sometimes it is useful to give users the ability to select which outputs they want to view on a page of a dashboard. This post illustrates how to do that, using a simple example where a user controls whether or not they view comments. The same basic idea can be used for showing and hiding charts and other analyses.
In the example below, the combo box says Hide Comments. If you click on it and change it to Show Comments a comment appears.
How it works
There are three stages to doing this:
- Adding a control to the page
- Creating the content you wish to show.
- Hooking the content up to the control.
1. Adding a control to the page
In the example above, I've used a combo box: Insert > Control > Combo Box (this option is on the very far right of the Insert tab in the ribbon at the top of Displayr). In the object inspector on the right-side of the screen, enter the options that the user can choose from in Control > Item list. In this example, I used Hide Comments; Show Comments.
2. Creating the content you wish to show
This is the hard bit. We need to create a table or something that is written in R code. So, if we want to create a textbox, for example, we need to do it in R code. The example below is created using Insert > R Ouptut and then adding in the code.
The text is shown in line 1. The rest of the code does the formatting, using HTML.
You will probably want to hide this content (Home > Hide)
3. Hooking the content up to the control
- Insert > R Output
- Paste in: if (Combo.box == "Hide Comments") "" else my.comment
If necessary, change Combo.box and my.comment to whatever names you've given these objects.
Now, just use the control and you will see that the comments appear and are then hidden, depending on what you have selected.
Adding lots of conditional outputs
If we need to have lots of outputs that appear based on controls, it could have some performance implications. There are a few things you can do. One is to combine steps 2 and 3 together, as shown below. Another option is to have one R output that contains lots of different things. For example, all the text boxes could be created in a single R Output if you have the HTML skill (this is a complicated thing to do; only investigate it if you have a performance issue due to too many R Outputs on the page).