Modules for LEGO attribute selections

As you make the transition to building production Shiny applications, Shiny modules are a very important tool to improve code organization and overall flow of your application. We will work together to build Shiny modules that let the user filter the underlying LEGO metadata based on a key variables in the overall data.

Requirements

Creste three inputs for the user to subset the LEGO metadata by the following key variables:

  • Theme(s) associated with sets. Default should be all sets, but let the user customize selections with a search box.
  • Year range for when set was created. Give the user a visual cue of how many sets are present in each year.
  • Pre-defined ranges for how many parts are present in each set. Ranges are the following:
    • Small (1-50 parts)
    • Medium (51-200 parts)
    • Large (201 or more parts)
    • All sizes

Data

The following data sets included in the application contain the variables needed for the requirements (note that these snippets are a reduced sample of each source data set).

sets

dplyr::glimpse(sets)
Rows: 368
Columns: 6
$ set_num   <chr> "001-1", "002-1", "1030-1", "1038-1", "1039-1", "1237-1", "1…
$ name      <chr> "Gears", "4.5V Samsonite Gears Motor Set", "TECHNIC I: Simpl…
$ year      <dbl> 1965, 1965, 1985, 1985, 1986, 2001, 1999, 1999, 1999, 1999, …
$ theme_id  <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
$ num_parts <dbl> 43, 3, 210, 120, 39, 56, 30, 29, 28, 26, 28, 103, 98, 64, 28…
$ img_url   <chr> "https://cdn.rebrickable.com/media/sets/001-1.jpg", "https:/…

themes

dplyr::glimpse(themes)
Rows: 30
Columns: 3
$ theme_id  <dbl> 191, 708, 693, 398, 650, 234, 56, 604, 136, 736, 692, 676, 2…
$ name      <chr> "Dark Forest", "The LEGO Batman Movie", "Monkie Kid", "FIRST…
$ parent_id <dbl> 186, 697, NA, NA, 632, 233, 52, NA, 126, 501, 535, NA, 227, …

Plan

  1. Create a new module R script (and possible a supporting R script) by using golem::add_module() in the dev/02_dev.R script.
  2. Pick a Shiny input type that matches our requirements.
  3. Ensure the server-side portion of the module returns the result of the selection, with additional post-processing if necessary.
  4. Plug in the UI and server-side portions of the module in the main app_ui.R and app_server.R files, respectively.

Solution (Theme Picker)

Create the module file with the following snippet:

golem::add_module(name = "theme_picker", with_test = FALSE)

While we could choose a selectInput that displays the name of the theme as the choices and allow multiple to be selected, we want to incorporate a search box with snappy performance. The virtualSelectInput from the {shinyWidgets} package is an excellent solution.

Since we have the themes data set available, we can also leverage the supporting function shinyWidgets::prepare_choices() to get the possible choices ready for the input. The module will simply return the selection as a reactive variable.

# UI portion
mod_theme_picker_ui <- function(id, label = NULL) {
  ns <- NS(id)

  themes <- dplyr::arrange(themes, name)

  choices_list <- shinyWidgets::prepare_choices(
    themes,
    label = name,
    value = theme_id
  )

  tagList(
    virtualSelectInput(
      ns("virt_theme_id"),
      label = label,
      choices = choices_list,
      selected = themes$theme_id,
      multiple = TRUE,
      search = TRUE
    )
  )
}

# server portion
mod_theme_picker_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    return(reactive(input$virt_theme_id))
  })
}

At the bottom of the module script, {golem} automatically includes commented code for referencing the module UI and server blocks in your main Shiny application. We can copy them into the app_ui.R and app_server.R files.

# app_ui.R
mod_theme_picker_ui("theme_picker_1")

# app_server.R
input_theme_ids <- mod_theme_picker_server("theme_picker_1")