1. More ways to view data: plot and download
Other than viewing a dataset as a table, there are also other ways to view or access the data in your Shiny app.
2. Plot data
We've all heard that a picture is worth a thousand words. It's true, looking at a picture, or a plot, can often be more informative than looking at a text representation of the same data. For this reason, when exploring a dataset, plotting the data is a common first step.
Similarly to tables, plots are also outputs.
This means that adding a plot requires two things: first, a placeholer for the plot is added to the UI using the plotOutput function. This tells shiny *where* the plot will be.
Then the plot needs to be created in the server, inside a renderPlot function that gets assigned to the output list. Inside the renderPlot function you can write any code that creates a plot.
3. Download data
A different way of accessing data in a Shiny app is by offering it as a downloadable file. Shiny has a download button that, when clicked, creates a file and downloads it to the user's computer.
We can use this download button to download any kind of file type. It can be used, for example, to download an image, a text file, a CSV file, or any other file you create.
4. CSV files
Let's take a moment to introduce CSV files in case you aren't familiar with them. CSV, which stands for comma separated values, is a simple file format that is commonly used to store small or medium sized datasets.
Here you can see what a CSV file of gapminder looks like: each line in the file represents a row of the data, and each column is separated by a comma.
To create a CSV file, you use the write-dot-csv function, with the first argument being the dataset to store, and the second argument is the filename
5. Download data in Shiny
Now that we know how to create a CSV file, let's see how we can use Shiny to download one.
The download button in shiny can be thought of as a special kind of output. It requires two functions, one in the UI and one in the server, just like any other output.
First, the downloadButton function is called in the UI, with outputId and label as parameters. You can think of downloadButton as equivalent to the output placeholder functions.
Then, in the server, you use the downloadHandler function, which you can think of as the equivalent of the output render functions. The download handler is assigned into output dollar-sign output-id, just like render functions.
6. Download handler
If you look at the downloadHandler function you'll notice it's different from typical render functions because it has two mandatory arguments.
The first argument of downloadHandler is filename, which is the name you want to assign to the file that will be downloaded by the user.
The second argument is content. content is itself a function with one argument. Inside this content function you need to create the file that you want the user to download. This file that you create needs to be saved to the location specified by the argument.
For example, as you can see in the sample code, inside the content function we can use the write-dot-csv function to create a CSV file of the gapminder data. Notice that the argument of the content function is used in the write-dot-csv function, because the CSV file needs to be written to that file path. When the user in the app clicks on the download button, this CSV file will be downloaded, and it'll be given the file name data-dot-csv.
7. Let's practice!
Let's try this in our app now.