Shiny selectInput interactive controls in R

Symbol of coding on a computer screen

Purpose: how to create Shiny interactive selectInput controls in R

A Shiny input dropdown created by a selectInput control
The R code to create a simple selectInput control

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.

New R Markdown Shiny template in RStudio

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
Example of selectInput in the default RStudio R Markdown Shiny template

>> On the next page we show the syntax for the selectInput() function

Pages: 1 2 3