Purpose: how to create Shiny interactive selectInput
controls in R
Links to topics on this page
- Using Shiny
- Create a new R Markdown shiny publication
- Shiny selectInput syntax
- Add selectInput control to a layout
- Add choices to the selection list
- How to use selectInput outputs
- Example selectInput code – input and output
- Related topics and resources
Using Shiny
Shiny is an R package used to build interactive applications in web pages, R Markdown publications, and dashboards.
Shiny’s 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:
library(shiny)
; or, - 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 selectInput
.
You see in the screenshot from the Shiny RMD template below that:
- a
selectInput
control has the name (ID) of"n_breaks"
, and - the output (user choice) from this is then used in the
renderPlot()
settings below; - 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 "bw_adjust"
.
Sample code in the RStudio R Markdown Shiny template
>> On the next page we show the syntax for the selectInput() function