1. Advanced features to improve your plot
Right now you have a working Shiny app with six different inputs, where each input modifies the plot in some way. For the rest of this chapter, instead of adding more inputs, we'll discuss other ways in which the plot can be improved.
2. Colour input
You've already added a colour selection using radio buttons, but you might agree that it's a bit strange to have to list manually all the available colours. In fact, colour selection is such a common task that there's a package called colourpicker that was written specifically for this purpose. The colourpicker package has a colourInput function that creates a colour input. Even though colour inputs are not part of the shiny package, they behave just the same as any other input.
A colour input is a great tool for selecting colours because it allows the user to select any possible colour rather than limit them to a list. The colour input also previews the selected colour as its background. Colour inputs have many more arguments, so as always, it's a good idea to look at their documentation to see what else they can do.
3. Outputs can have arguments
So far we explored many different input functions, and we saw that they all have unique arguments to customize them. While there aren't as many output functions, some of the output functions also have some arguments that will let you customize them.
For example, if you look at the documentation for the `plotOutput()` function, you'll see that it has many arguments besides the output ID. We won't cover all of them, but it is useful to know that outputs can also be modified with arguments.
4. Plot output arguments
Two arguments that you may want to use with plot outputs are width and height. If the default size of a plot in your app is too small or too large, you can have more control over it by specifying the exact number of pixels the plot should occupy.
5. Interactive plots with plotly
Since Shiny is an interactive way to explore your data, people sometimes want to make the plots themselves interactive as well. There are many different packages in R that provide interactive visualizations.
One of the popular packages that provide interactive plots is called `plotly`. `plotly` has a convenient function, ggplotly, that converts any `ggplot2` plot into an interactive plot. This can be useful because if you already know ggplot2 then it requires a minimal amount of code to transform your plot to an interactive one.
For example, suppose we have this ggplot code that creates a plot. Wrapping the ggplot code in a call to the ggplotly function will create an interactive version of the plot. There are many interactive features in the resulting plot. For example,
6. Interactive plots with plotly
the user can zoom in on
7. Interactive plots with plotly
specific regions, or
8. Interactive plots with plotly
move their mouse over a data point to see the x and y values of that point.
9. Plotly in Shiny
When using plotly in shiny, there is one more thing that you need to know. A plot that gets generated with plotly is considered to be a different type of output than a regular plot. That means that you can't put a plotly object inside a renderPlot function, because renderPlot expects a regular plot. Instead, you need to use renderPlotly, and similarly in the UI, the placeholder function changes from plotOutput to plotlyOutput.
10. Let's practice!
Now let's put all this into practice.