1. Types of output
We will now add outputs, and define interactions.
2. Output and render functions
In a shinyApp, there will be outputs which can come in the form of text, charts, and so on. These are like the dishes that the restaurant serves. For each output, a pair of codes need to be called.
The first is a function in the UI, which allows us to position the output. Such an output function typically requires an identifying label, and other output-specific arguments.
The second code required is defined within the server function, which will take the following form. Arguments of a render function will determine the form and appearance of our output.
3. Example: Sleep study
Let's try some examples. Suppose we collected data from the sleep study, which we import and call it sleep.
This dataset is made up of many variables. We will use average hours per day sleeping in many of the examples, and utilize many of the categorical variables, such as Year, Period, and type of days.
4. textOutput
A simple example is that of a textOutput. Let us place this textOutput in the UI.
5. renderText
At this point, we won't see the textOutput rendered yet. To render textOutput, we will need to call the renderText function in the server. Note the dollar sign notation, which is used to assign a value to the textlabel output.
6. textOutput rendered
This shows how basic text outputs can be obtained in a shinyApp.
7. textOutput with interactions
We can also incorporate user input. At this point, the outputs do not change based on any input provided. To allow interactions between user input and output, we need to call the inputs using the dollar sign notation.
8. textOutput with interactions
Let us consider interactions with a text input.
See that the text input, which we labelled as textlabel, is called in the server function in this code block. This is defined within renderText, which then allows for interactions with the textOutput.
9. textOutput with interactions
Upon rendering the app, updating the input will update the text output, as desired.
10. plotOutput
We will frequently show charts in a shinyApp or shinydashboard, which is achieved by placing plotOutput in the UI.
At this point, we will not see the plot, but rest assured that it has been placed in the app.
11. renderPlot
To render the plot, we will need to make an assignment within the server function using renderPlot. In this example, a histogram is plotted.
12. plotOutput rendered
Rendering the app using shinyApp then produces the histogram.
13. plotOutput with selectInput
Here is an example where we plot either a histogram or box plot based on user input. See that we have labelled the input as selectinput, and the output as plotlabel.
14. plotOutput with selectInput
In the server function, the if-else statements display a histogram if histogram is selected and a boxplot otherwise.
15. plotOutput with selectInput
In the rendered shinyApp, selecting histogram produces a histogram and so on.
16. plotOutput with sliderInput
Here is another example where we use a sliderInput to display the boxplot based on the selected range of values.
17. plotOutput with sliderInput
The rendered app produces a boxplot that reacts to user input on the slider.
18. Icons in shiny
Let's now briefly discuss icons in shiny. These are icons that can be included in a shinyApp to allow for a cleaner look. We will see more examples of these icons later.
19. Icons in shiny: An example
To obtain icons, we will need to use the icon function.
Let's revisit an earlier example. Being involved in a sleep study, it is apt to use a bed icon someplace. Placing the icon in a list with the displayed text renders them side by side.
20. Rendered app with icon
This icon will appear in the positioned location, which was in the sliderInput description.
21. Let's practice!
Now it's your turn to practice putting together some shinyApps!