3 Inputs

Inputs are ways that users can communicate information to the Shiny app. Explore some different input types in the embedded app below before you read about how to set up each type.

Figure 3.1: Input Demo App. You can also access this app with shinyintro::app("input_demo") or view it in a separate tab with the showcase interface.

3.1 Input functions

3.1.1 textInput

textInput() creates a one-line box for short text input. The first argument, inputId (the argument name is usually omitted), needs to be a unique string that you cannot use for another input or output in this app.

demo_text <- 
  textInput("demo_text", 
            label = "Name", 
            value = "", 
            width = "100%",
            placeholder = "Your Name")

Experiment with the values of label, value, width, and placeholder to see what they do.

3.1.2 textAreaInput

textAreaInput() creates a multi-line box for longer text input.

demo_textarea <- 
  textAreaInput("demo_textarea", 
                label = "Biography", 
                value = "",
                width = "100%",
                rows = 5, 
                placeholder = "Tell us something interesting about you.")

What is the inputId of the widget above?

3.1.3 selectInput

selectInput() creates a drop-down menu. Set the first choice to "" to default to NA. If your choices are a named list or vector, the names are what is shown and the values are what is recorded. If the choices aren't named, the displayed and recorded values are the same.

demo_select <- 
  selectInput("demo_select", 
              label = "Do you like Shiny?", 
              choices = list("", 
                             "Yes, I do" = "y", 
                             "No, I don't" = "n"),
              selected = NULL,
              width = "100%")

If you set multiple to TRUE, you can also make a select where users can choose multiple options.

genders <- list( # no blank needed
  "Non-binary" = "nb",
  "Male" = "m",
  "Female" = "f",
  "Agender" = "a",
  "Gender Fluid" = "gf"
)

demo_select_multi <- 
  selectInput("demo_select2", 
              label = "Gender (select all that apply)", 
              choices = genders,
              selected = NULL,
              multiple = TRUE, 
              selectize = FALSE,
              size = 5)

3.1.4 checkboxGroupInput

However, this interface almost always looks better with checkboxGroupInput().

demo_cbgi <-
  checkboxGroupInput("demo_cbgi",
                     label = "Gender (select all that apply)",
                     choices = genders)

How can you get the checkboxes to display horizontally instead of vertically?

3.1.5 checkboxInput

You can also make a single checkbox with checkboxInput(). The value is TRUE when checked and FALSE when not.

demo_cb <- checkboxInput("demo_cb",
                         label = "I love R",
                         value = TRUE)

sliderInput() allows you to choose numbers between a min and max value.

demo_slider <- sliderInput("demo_slider",
                           label = "Age",
                           min = 0,
                           max = 100,
                           value = 0,
                           step = 1,
                           width = "100%")

What happens if you change value or step? Try changing value to c(10, 20).

3.1.6 radioButtons

If you want users to only be able to choose one option and there are a small number of short options, radioButton() is a good interface.

demo_radio <- radioButtons("demo_radio",
                           label = "Choose one",
                           choices = c("Cats", "Dogs"),
                           selected = character(0),
                           inline = TRUE)

Radio buttons default to selecting the first item unless you set selected to a choice value or character(0) to start with no selection.

3.1.7 dateInput

I find the date interface a little clunky, but that might just be because I have to click the back button on the year interface 44 time to find my birthdate. However, it also allows you to type in a date following the format that you can set.

demo_date <- dateInput("demo_date",
                       label = "What is your birth date?",
                       min = "1900-01-01",
                       max = Sys.Date(),
                       format = "yyyy-mm-dd",
                       startview = "year")

IMHO, the default of "yyyy-mm-dd" is the best because it sorts into chronological order. Don't let me catch you storing dates like "m/d/yyyy".

What would you set format to in order to display dates like "Sunday July 4, 2021"?

3.1.8 fileInput

Users can upload one or more files with fileInput(). The argument accept lets you limit this to certain file types, but some browsers can bypass this requirement, so it's not fool-proof.

demo_file <- fileInput("demo_file",
                       label = "Upload a data table",
                       multiple = FALSE,
                       accept = c(".csv", ".tsv"),
                       buttonLabel = "Upload")

What would you set accept to to accept any image file?

3.2 Setting inputs programatically

Sometimes you need to change the value of an input with code, such as when resetting a questionnaire or in response to an answer on another item. The following code resets all of the inputs above.

updateTextInput(session, "demo_text", value = "")
updateTextAreaInput(session, "demo_textarea", value = "")
updateSelectInput(session, "demo_select", selected = "")
updateCheckboxGroupInput(session, "demo_cbgi", selected = character(0))
updateCheckboxInput(session, "demo_cb", value = TRUE)
updateRadioButtons(session, "demo_radio", selected = character(0))
updateSliderInput(session, "demo_slider", value = 0)
updateDateInput(session, "demo_date", value = NULL)

Note that select inputs, checkbox groups, and radio buttons use the argument selected and not value(). If you want to set all the values in a checkbox group or radio button group to unchecked, set selected = character(0).

3.4 Exercises

Pets

Create an interface that gets people to rate the following pets on a 9-point scale. You can use any option labels or input type you like.

  • Dogs 🐕
  • Cats 🐈
  • Birds 🦜
  • Fish 🐠
  • Mice 🐁
  • Hedgehogs 🦔
  • Snakes 🐍

3.5 Your App

In the app you're developing, add a tab for a questionnaire that you're interested in and set up the appropriate inputs.

Add a "reset" button to your questionnaire tab and write the server() code to reset all of its inputs.