Get startedGet started for free

The reactive dataframe pattern

1. The reactive dataframe pattern

Making your dashboard interactive using Shiny means creating a way to receive user inputs and then making the outputs - the dashboard components - react appropriately. In this lesson we're going to look at an easy way to do this with one common pattern. This applies to cases where the user inputs are used to filter the datasets used to create the dashboard.

2. Creating a sidebar

A nice way to separate user inputs from the rest of the dashboard is in a sidebar. To create a sidebar, simply add a column, generally a narrow one, with the sidebar attribute.

3. Creating a sidebar

You can see here how the sidebar attribute on the first column gets styled differently and sets apart user inputs.

4. Adding user inputs

User inputs are collected using Shiny widgets - the same ones you would use in any Shiny app. Unlike a standard Shiny app, the widgets don't need to appear within a page layout or UI section. In this example you can see how the sliderInput widget is in an R chunk that goes in a flexdashboard column like any other dashboard component.

5. Making our dataframe reactive

As usual, the value collected from the user with the Shiny widget can be accessed as a member of the input list. In this case input$duration-slider is the duration value chosen by the user. Now comes the exciting part: if what we want to do with this value is use it to modify the dataset the dashboard depends on, it becomes very simple. The reactive block shown here contains code that filters the dataset based on the slider value. The "reactive" function means that this runs anytime there is a change to the input values. The result is that show-trips-df is updated to reflect the user inputs.

6. Using the reactive dataframe

Once we have created a version of the dataframe that reacts to user inputs, we can replace the use of the dataframe throughout our dashboard with this reactive alternative. To do this, we replace our previous dataframe - in this case trips-df (on the left) - with the reactive show-trips-df (on the right). Note that unlike a regular dataframe, the reactive version is followed by parentheses. This is because the reactive dataframe is not a dataframe object, it's actually a function that returns the dataframe specified by the code within reactive.

7. Making dashboard components reactive

There's one more step to make our dashboard react to user input. In every output that now relies on the reactive version of our dataframe, we need to wrap the output in the appropriate reactive output render function. This example shows the code that creates the leaflet map of bikeshare trip origins in our dashboard. In this case we need to add renderLeaflet around this code chunk. Now this map will change in response to users selections of trip durations! Notice that we don't need to use the corresponding output function, like we would in a normal shiny app. This is one way that an interactive R Markdown document is different from a standard shiny app.

8. Steps to the reactive dataframe pattern

In summary, there are five steps to making your dashboard reactive using Shiny with the reactive dataframe pattern. Create a sidebar column, add user inputs, make your dataframe reactive, replace the static dataframe with the reactive one in your dashboard code, and wrap each output with the relevant render function.

9. Let's practice!

Now let's try it!