1. Types of input
Now that we have seen what a shinyApp is made of, let us look at the types of inputs that we can insert into it.
2. Input functions
In the user interface, users can provide inputs like instructions to the waiter at the restaurant.
In a shinyApp, these instructions are communicated through the server. This is done by adding input functions to the UI.
In a typical input function,
the first argument is an identifying label that is not visible to the user.
The second argument is a displayed label to be rendered in the shiny app.
The other arguments are specific to the type of input.
3. An example: sleep study
Let's look at an example using input functions.
Suppose we carried out a sleep study and collected plenty of data, and will now like to design a shinyApp that allows users to give inputs.
What possible types of inputs can such a shinyApp user give? Let's answer this question here.
4. selectInput
A common input is selectInput, which, as mentioned earlier, is placed in the ui. This creates a drop-down menu with many possible options. The third argument is a vector where each element is a possible option.
Here, we have four possible options.
For now, let's keep the server empty. The server will not fetch anything at this point.
Let's render the shiny app by running the shinyApp function.
5. selectInput
With a selectInput, the user can select one out of four options, as seen here.
6. selectInput with multiple selections
We can also add a fourth option called "multiple" which allows users to choose multiple values.
7. selectInput with multiple selections
This will then produce an app where the user can select multiple options.
8. textInput
Another example is textInput.
There are two other important arguments in textInput.
One is the value, which is the value that the textbox is pre-filled with. The value argument is typically kept empty.
The other is the placeholder, which can contain either further instructions or a sample of what we want users to enter.
We can glue ui and server together using shinyApp.
9. textInput
This is the rendered app. See that the textbox was pre-filled with Birdie, which we set earlier. When empty, the placeholder Don appears, which can be replaced with a different name.
10. checkboxInput
Another example is checkboxInput. This provides a single checkbox in the app.
The third argument, value, which is either TRUE or FALSE, and will allow us to toggle the initial state of the checkbox.
There is a variation of checkboxInput, called checkboxGroupInput which produces a group of checkboxes.
11. checkboxInput
This is what a single checkbox looks like once it's rendered.
12. sliderInput
Finally, sliderInput. This produces a slider that allows the user to do one of two things.
The user can select a single numerical value from a range of numbers, or two from a range of numbers.
For users to select a single value, we need to set the min and max arguments.
The initial value is determined by value.
We can also set the step size using step.
13. sliderInput
This allows users to select just one number from the slider. See that the left and right limits of the slider define the range.
14. sliderInput with ranges
To obtain a two-sided slider where two numbers can be selected, we need only set the value argument to a vector of length-2. In this example, we have set the value to a vector containing 8 and 10.
15. sliderInput with ranges
In doing so, the user can move the slider's left and right limits.
16. Let's practice!
We have seen some input functions in this video, but there are many more. In the following exercises, we will practice working with input functions.