Get startedGet started for free

Explore a dataset with Shiny

1. Explore a dataset with Shiny

In the last chapter, you made a shiny app that creates a customized plot using the gapminder dataset.

2. Explore a dataset with Shiny

After seeing this app, your supervisor became impressed and now he wants to get familiar with the dataset you used. But he doesn't just want to have the raw data file, he wants an interactive environment where he can view the data, filter it in different ways, and download it. This chapter will guide you in creating such an application—a Shiny app for exploring the Gapminder dataset. This type of Shiny app is actually a fairly common use of Shiny, because any time you get your hands on a new dataset, one of the first thing to do is explore it, and Shiny can help with that.

3. Visualize data as a table

A simple way to start exploring a dataset is simply viewing it. For any dataset that is made up of rows and columns, like gapminder, a table is a natural way to visualize it.

4. Tables in shiny

In shiny, tables are outputs. Recall that creating an output involves two steps: First you add a placeholder for the output in the UI using an output function, which is the tableOutput function in the case of a table. Then you call the corresponding render function in the server, which is renderTable in this case. This code will add a table to the shiny app that will display the gapminder data.

5. Filtering table data

One simple way you might want to explore the gapminder data is by looking at only one specific country at a time. This can be achieved by adding an input, or more specifically a select input with the choices being the list of countries. The input will allow the user to select a country, and then it can be used inside the renderTable function to filter for just that one country. Thanks to reactivity, whenever a different country is selected, the table will get updated.

6. Select input choices

It's important to remember that the choices argument of the selectInput function takes a list of strings, and that you have full control over what that list is. For example, suppose for some reason you wanted to only allow selecting one of the first ten countries. To do this, you can pass only the first ten items from the countries list to the choices argument. You can also add values to the list: for example, if you wanted to add the word "any" to the list of countries, you can use the c function, which is the standard way to create vectors in R, to add the word "any" to the dropdown.

7. Let's practice!

Now let's try some examples