Purpose: how to create Shiny interactive
selectInput controls in R
Links to topics on this page
Shiny is an R package used to build interactive applications in web pages, R Markdown publications, and dashboards.
selectInput() controls enable a user to make a selection(s) based on dropdown lists. The output(s) from the selection can then be used in various ways to feed into other interactive elements in the web application or publication, e.g. charts, plots and dynamic text outputs.
Outputs automatically update when the inputs change.
You can use the Shiny package in different ways. In RStudio create:
- an R Markdown document with a Shiny output (see below); or,
- a new R Markdown document from scratch and load the library into the document:
- a new Shiny web app.
In these examples we’re going to show you how to use Shiny in an R Markdown document with a Shiny output, in RStudio.
Create a new R Markdown Shiny publication.
In RStudio select File | New | R Markdown, and in the dialog box, select Shiny from the left hand pane. Name your document, add an Author name (or use the field as a subtitle) if desired, and click OK.
The template will load with some default content which includes an example of
You see in the screenshot from the Shiny RMD template below that:
selectInputcontrol has the name (ID) of
- the output (user choice) from this is then used in the
- the dropdown output is called using the format
input$ID, where [ID] is the selectInput name (ID).
The example also shows how another type of reactive input control, a
sliderInput, is used to allow the user to define settings for the plot output. The ID for this control is
Sample code in the RStudio R Markdown Shiny template
>> On the next page we show the syntax for the selectInput() function