R Tutorial: Case Studies: Building Web Applications with Shiny in R | Inputs and outputs

Learn to build interactive Shiny apps by connecting inputs like textInput and numericInput to outputs like tables and plots. A numeric input controls how many rows of the iris dataset display in a table. Three rules: wrap code in a render function, assign to output$id, and read inputs via input$id.
Chapters
So far you've reviewed how to create a shiny app and add text to it. You might be thinking that this isn't very useful as a web application. It's more of a text document. In order to be useful, shiny apps need inputs and outputs. Inputs are what gives users a way to interact with a shiny app. An input is anything that the user can interact with using the mouse or keyboard to modify its value. For example, this is a text input where users can enter text, and this is a numeric input which lets users select a number. In fact, shiny provides many different input functions, some of which are shown here. Inputs are created by calling an input function inside the ui. Here is the code that creates the two inputs that you saw earlier. You may notice that the code for the two inputs has some similarities. Most input functions have the word input in their name and have the same first two input ID and label. The input ID has to be unique because later on you'll use this ID to retrieve the value of the input. The label argument is the descriptive text that appears above the input. Some inputs can also have other arguments specific to that input type, which we'll see later on. Outputs can be any object that R creates and that we want to show in our shiny app, such as a plot, a table, or even just text. Creating an output, for example a plot is a two step process. First, you need to tell shiny where to place the plot by adding an output placeholder in the ui. Similar to input functions, output placeholder functions also have an output ID parameter that must be unique. The second step is to actually write the R code for the plot. This is done in the server. So far, we only wrote code in the ui. Creating inputs and output placeholders is entirely done in the user interface, but building outputs is done in the server code. Recall that the server of a shiny app is a function with two parameters, input and output. You can think of both of them as lists. As the name suggests, input is a list you will read values from, and output is a list you will write values to too. Specifically the outputs themselves. Let's see how to build an output. Suppose your app has a UI with a numeric input with ID num and a table output with ID mytable. You want the table to show the first num rows of the iris data set. This is the server code for building that table. There are three rules for building any shiny output, including this table. First, the code for the output object needs to be inside a render function. Each type of output has its own render function. In this example, since the output is a table, the rendertable function is used. Second, the result of the render function needs to be saved to the output list using the output ID from the ui. Here, the ID of the table in the UI is mytable, so we save to output $sign my table. Third, if you want to access any input values, use input $input ID. In our example, input $num is used in the output code, so every time the user changes the num input, the table will update automatically. Now let's see if you can use inputs and outputs on your own.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







