Chapter 19. Shiny Modules
In the last chapter we used functions to decompose parts of your Shiny app into independent pieces. Functions work well for code that is either completely on the server side or completely on the client side. For code that spans both (i.e., whether the server code relies on specific structure in the UI), you’ll need a new technique: modules.
At the simplest level, a module is a pair of UI and server functions. The magic of modules comes because these functions are constructed in a special way that creates a “namespace.” So far, when writing an app, the names (id
s) of the controls are global: all parts of your server function can see all parts of your UI. Modules give you the ability to create controls that can only be seen from within the module. This is called a namespace because it creates “spaces” of “names” that are isolated from the rest of the app.
Shiny modules have two big advantages. First, namespacing makes it easier to understand how your app works because you can write, analyze, and test individual components in isolation. Second, because modules are functions, they help you reuse code; anything you can do with a function, you can do with a module. Let’s begin by loading shiny:
library
(
shiny
)
Motivation
Before we dive into the details of creating modules, it’s useful to get a sense for how they change the “shape” of your app. I’m going to borrow an example from Eric Nantz, who talked about modules at rstudio::conf(2019). Eric was motivated ...
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.