Chapter 10. Dynamic UI

So far, we’ve seen a clean separation between the user interface and the server function: the user interface is defined statically when the app is launched so it can’t respond to anything that happens in the app. In this chapter, you’ll learn how to create dynamic user interfaces, changing the UI using code run in the server function.

There are three key techniques for creating dynamic user interfaces:

  • Using the update family of functions to modify parameters of input controls

  • Using tabsetPanel() to conditionally show and hide parts of the user interface

  • Using uiOutput() and renderUI() to generate selected parts of the user interface with code

These three tools give you considerable power to respond to the user by modifying inputs and outputs. I’ll demonstrate some of the more useful ways in which you can apply them, but ultimately you’re only constrained by your creativity. At the same time, these tools can make your app substantially more difficult to understand, so deploy them sparingly, and always strive to use the simplest technique that solves your problem. Let’s begin:

library(shiny)
library(dplyr, warn.conflicts = FALSE)

Updating Inputs

We’ll start with a simple technique that allows you to modify an input after it has been created: the update family of functions. Every input control—for example textInput()—is paired with an update function—for example updateTextInput()—that allows you to modify the control after it has been created.

Take ...

Get Mastering Shiny now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.