Instead of filtering outputs, you may want to show or hide entire outputs based on the selection in a Control. Keep in mind this method only works for R-based outputs and custom Calculations on the page. This article describes how to use a Combo box Control to let the user select either to show or hide the custom Calculation with comments on the Page .
Requirements
- A Control Box control (see How to Create a Combo Box Filter or How to Create a List Box Filter).
- An R-based output or custom Calculation to show or hide.
Please note this requires the Data Stories module or a Displayr license.
Method
In this example we create a custom Calculation on the page that contains a comment. It is shown/hidden based on the combo box.
- Create the comment output that we will be hiding/showing. Click the Calculation icon > Custom Code and create a box on the page for the output.
- Paste in your custom code to create the desired output in General > R CODE. The code below shows a comment in the box as in the gif above:
#Create comment to show inside the double quotes
comment = "Wow! This is great! What an insight-filled chart!"
#Create the text box using HTML for the formatting
#the paste function combines static HTML code with the comment created above
your.html = paste0('<style>div.mystyle {font-size: 24px;
font-family: Open Sans;
padding-left: 10px;
padding-top: 10px;
padding-bottom: 10px;
background-color: #42afe3;
color: white ;
opacity: 1;}
</style>
<body><div class = "mystyle">',
comment,
'</div></body>')
#Render the HTML code above using the Box function in rhtmlMetro
my.comment = rhtmlMetro::Box(text = your.html, text.as.html = TRUE) - To make the output dependent, we will use an if-else construct to return something different based on the value of the combo box. Paste the following code at the bottom of the R CODE:
#If Hide comments is selected in the combo box show a blank transparent output
#otherwise show the my.comment HTML formatted text
#change Combo.box to the GENERAL > name of your combo box (or
#click on it with the cursor inside the code)
#change "Hide comments" to the option selected to hide the output
#change my.comment to the output you want to show if it's not hidden
if (Combo.box == "Hide comments") "" else my.comment
Notes
- You can paste the code in Step 3 inside the General > R CODE of an R-based output (such as a visualization) as well to show or hide it based on a combo box. Just ensure my.comment is the specific name of the visualization.
- You can use similar code to switch the output shown to other outputs as well, see How to Use a Control Box as an Input for Calculations.
Next
How to Use a Control Box as an Input for Calculations
How to Use the Same Control on Multiple Pages
How to Connect Filters to Controls Using R Code
How to Dynamically Change a Question 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 Change Weighting using a Control Box