Get startedGet started for free

Adding simple inputs to modify a plot

1. Adding simple inputs to modify a plot

Now that you're familiar with the gapminder dataset, it's time to use it in a shiny app.

2. Gapminder plot app

This is the shiny app that will act as your starting point. As you can see, there's not much to it:

3. Gapminder plot app

an empty sidebar on the left,

4. Gapminder plot app

and a plot of GDP per capita versus life expectancy on the right.

5. Gapminder plot app

This plot uses gapminder as its data source. As a side note, we'll be using the ggplot2 package to build the plot, but don't worry if you don't know how to use ggplot2, all the relevant code will be provided to you.

6. Gapminder plot app

In this chapter, you'll add several inputs to this app, and use these inputs to modify the plot in real time. After the first set of exercises, your app will look like this. It's still the same plot, but there are now three inputs that act as parameters to the plot:

7. Gapminder plot app

there's a title field using a text input,

8. Gapminder plot app

a numeric selector to choose the size of the points,

9. Gapminder plot app

and a checkbox for whether or not to add a line of best fit.

10. Gapminder plot app

Let's discuss each of these inputs in more detail.

11. Text inputs

Here's an example of a shiny app with a text input asking the user for their favorite R package. Text inputs are great to use whenever you want to retrieve some text from the user. Recall from the previous chapter that the first argument of every input is inputId and the second argument is the label. The third argument here, value, supplies the text field with the initial value it should hold when the app initializes. Remember that you can use input dollar sign input id to access the current value of the input in the server. In the case of a text input, that variable will return a string.

12. Numeric inputs

Another very common input that we'll use is the numeric input, which is used when you want the user to select a number. Notice that in this code, the names of the first two parameters are not stated explicitly; that's ok since all inputs expect the same first two arguments. The first argument, "years", is the ID of the input, and the second argument is the label. This input also has a couple extra parameters - the minimum and maximum allowed values. These extra arguments are optional. When a numeric input is accessed in the server, the return value is an integer or a numeric, depending on whether the number is whole or fractional.

13. Checkbox inputs

Another useful input is the checkbox. It presents the user with a box they can either check or uncheck. Checkbox inputs are useful when you want to get a yes/no or true/false value from the user. As you might expect, when accessing the value of a checkbox input from the server, a logical TRUE or FALSE is returned.

14. Let's practice!

Now that you've seen some basic inputs, it's time to practice using them.