Get startedGet started for free

Inputs and outputs

1. Inputs and outputs

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.

2. Inputs

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,

3. Inputs

and this is a numeric input, which lets users select a number.

4. Inputs

In fact, Shiny provides many different input functions, some of which, are shown here.

5. Building inputs

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 arguments: inputId and label. The inputId 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.

6. Outputs

Outputs can be any object that R creates and that we want to display in our 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 outputId parameter that must be unique. The second step is to actually write the R code for the plot. This is done in the server.

7. 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, specifically, the outputs themselves.

8. Building outputs

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 "my_table". You want the table to show the first "num" rows of the iris dataset. This is the server code for building that table. There are 3 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 "my_table", so we save to output dollar sign my_table. Third, if you want to access any input values, use input dollar-sign input_id. In our example, input dollar-sign num is used in the output code, so every time the user changes the num input, the table will update automatically.

9. Let's practice!

Now let's see if you can use inputs and outputs on your own.