1. Customized inputs for charts
In this lesson we'll talk about ways to incorporate Shiny into your dashboard that go beyond the simple reactive dataframe pattern.
2. Chart-specific effects
For reactive effects that are specific to a chart, you can keep the inputs in the sidebar and use them in specific chart code. Inputs can be accessed using the input list as usual, and can be added inside any reactive output function such as renderPlot or renderLeaflet. In this example, the maximum trip duration slider affects all three charts via the reactive dataframe pattern, but the duration bin slider affects only the Trip Durations chart. The input value is used directly within the chart code.
3. Moving inputs into charts
You may prefer to have inputs relating to a specific chart co-located with that chart. This is possible, but adds substantial complication to your code. In this example, the duration bin slider has moved from the sidebar to the duration chart box. Let's look at what this requires.
4. Moving inputs into charts
Putting Shiny inputs and outputs in the same container means dealing with layouts. Some of the convenience flexdashboard provides in handling layouts is lost. Now we need to use Shiny fill layouts, meaning either fillCol or fillRow. The height is specified and the two values in the vector for the flex argument indicate the non-flexible height for the first component - in this case the input panel - and the fully flexible height for the second component - the plot.
5. Moving inputs into charts
Next we need to specify an input panel, which contains the inputs we might otherwise have put on the sidebar, like a sliderInput.
6. Moving inputs into charts
The second component in the fill layout is the output, in this case the plotOutput. Specifying the height as 100% means that the plot can expand to fill the available space under the input panel.
7. Moving inputs into charts
Finally, we need to render the output as in a standard Shiny app. Here renderPlot would contain the same code we would have used if the chart were reacting to input collected in sidebar inputs as in the reactive dataframe pattern. As you can see, there is substantially more complication in putting Shiny inputs with charts in your dashboard, and you should consider whether it's worth the trouble for each particular situation.
8. A shortcut
One shortcut worth mentioning if you want to have Shiny inputs in a sidebar affect some charts and not others is to have a multi-page dashboard in which charts affected by the same set of user inputs are collected on a page together. Then the sidebar can be for that page and the user will see the appropriate inputs for the charts in view. It's up to you, however, to make sure you make the charts react appropriately in line with what the user sees on the page. Conversely, if you have sidebar inputs you want to display across all the pages in your dashboard, you can do this by creating a page with the sidebar class and putting all your inputs there, followed by the rest of your pages.
9. Let's practice!
Let's try this!