3 Inputs

All Shiny inputs have two arguments called inputId and label. The values created by the input will be stored in input$inputId, and the text displayed above the input is given by label.

Beyond these two, other common arguments include

  • choices or min and max, which define the options available
  • value or selected, which are used to provide starting values

The base shiny package includes a number of inputs that return character or numeric vectors, making them useful for tasks like selecting variables to plot and filtering observations to those within a certain numeric range.

Examples of most of the inputs in shiny are in the app below, and they can also be viewed by clicking here. Note that the code to reproduce each input can be found right below it, followed by a line that displays the current value of the input.

More inputs can be found in other packages such as shinyWidgets. For this and more, see the list of packages on awesome-rshiny.

Let’s look again at the code for selectInput() from example1:

selectInput(
  inputId = "xvar",
  label = "Pick a variable for the x-axis:",
  choices = c("wt", "hp"),
  selected = "wt"
)

selectInput() returns a character vector from a drop-down menu with the following specifications:

  • inputId = "xvar": server can access the selected value at input$xvar
  • label = "Pick a variable for the x-axis:": the text “Pick a variable for the x-axis:” is displayed above the input
  • choices = c("wt", "hp"): two choices are in the menu, “wt” and “hp”
  • selected = "wt": “wt” is selected by default
    • Note that this argument is unnecessary because “wt” would have been selected by default, per the documentation, “If not specified then defaults to the first value for single-select lists.”

To make the app more user-friendly, we can use a named list for the choices so that the user does not have to interact with actual variable names. To make a named list, use the format "name" = "value". To display “Weight” and “Horsepower” as choices instead of “wt” and “hp”, we could modify the code to this:

selectInput(
  inputId = "xvar",
  label = "Pick a variable for the x-axis:",
  choices = c("Weight" = "wt", "Horsepower" = "hp"),
  selected = "wt"
)

This would have the effect that the user sees “Weight” and “Horsepower” while the app passes along their choice as “wt” or “hp”, respectively, to server.

3.1 Exercises

Check Your Understanding:

Copy and paste the following code into a new R script and save it. Click “Run App”. This app returns the mean of a selected variable. Modify the app’s ui in these ways, and run it again after each modification to confirm the changes worked:

  1. The app will return the mean of either mtcars$wt or mtcars$mpg. Add another variable to the list of choices. See the available choices with colnames(mtcars).

  2. Add a label for your newly added variable. You can find descriptive names of the mtcars variables at help(mtcars).

  3. Change the input control (selectInput()) to show radio buttons rather than a drop-down menu.

  4. Change the text displayed above the input control.

library(shiny)

ui <- 
  fluidPage(
    selectInput(inputId = "var",
                label = "Pick a variable",
                choices = c("Weight" = "wt", "MPG" = "mpg"),
                ),
    
    textOutput(outputId = "myText")
  )

server <-
  function(input, output) {
    output$myText <- 
      renderText(
        paste("The average of", 
              input$var, 
              "is", 
              mean(mtcars[, input$var]))
    )
  }

shinyApp(ui = ui, server = server)

Create Your Own App:

  1. Choose a dataset to use for your Shiny app. Choices include:

    • airquality
    • freeny
    • quakes
    • starwars (from the dplyr package)
    • storms (from the dplyr package)
    • You are also free to use an external dataset, such as the sample of the 2000 ACS data used in Data Visualization in R with ggplot2, though you should take a small subsample (<1000) rows for your app to run quickly. Whatever you use, be sure it is not too large (so that the app can run at a reasonable speed) and that it has at least five columns (to provide opportunities for different inputs).
  2. Create at least two different types of inputs.

  3. Run your app to verify the inputs work.