Skip to content

Commit

Permalink
add module code-along and exercise files
Browse files Browse the repository at this point in the history
  • Loading branch information
rpodcast committed Sep 15, 2023
1 parent dd6dbe6 commit e3c3561
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hash": "c88328e0e03c4bc3dae3add79032a6de",
"result": {
"markdown": "---\ntitle: Modules for LEGO attribute selections\n---\n\n\nAs 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.\n\n## Requirements\n\nCreste three inputs for the user to subset the LEGO metadata by the following key variables:\n\n* Theme(s) associated with sets. Default should be all sets, but let the user customize selections with a search box.\n* Year range for when set was created. Give the user a visual cue of how many sets are present in each year.\n* Pre-defined ranges for how many parts are present in each set. Ranges are the following:\n + Small (1-50 parts)\n + Medium (51-200 parts)\n + Large (201 or more parts)\n + All sizes\n\n## Data\n\nThe 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).\n\n### `sets`\n\n\n::: {.cell}\n\n```{.r .cell-code}\ndplyr::glimpse(sets)\n```\n:::\n\n::: {.cell}\n::: {.cell-output .cell-output-stdout}\n\n```\nRows: 368\nColumns: 6\n$ set_num <chr> \"001-1\", \"002-1\", \"1030-1\", \"1038-1\", \"1039-1\", \"1237-1\", \"1…\n$ name <chr> \"Gears\", \"4.5V Samsonite Gears Motor Set\", \"TECHNIC I: Simpl…\n$ year <dbl> 1965, 1965, 1985, 1985, 1986, 2001, 1999, 1999, 1999, 1999, …\n$ theme_id <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …\n$ num_parts <dbl> 43, 3, 210, 120, 39, 56, 30, 29, 28, 26, 28, 103, 98, 64, 28…\n$ img_url <chr> \"https://cdn.rebrickable.com/media/sets/001-1.jpg\", \"https:/…\n```\n\n\n:::\n:::\n\n\n### `themes`\n\n\n::: {.cell}\n\n```{.r .cell-code}\ndplyr::glimpse(themes)\n```\n:::\n\n::: {.cell}\n::: {.cell-output .cell-output-stdout}\n\n```\nRows: 30\nColumns: 3\n$ theme_id <dbl> 191, 708, 693, 398, 650, 234, 56, 604, 136, 736, 692, 676, 2…\n$ name <chr> \"Dark Forest\", \"The LEGO Batman Movie\", \"Monkie Kid\", \"FIRST…\n$ parent_id <dbl> 186, 697, NA, NA, 632, 233, 52, NA, 126, 501, 535, NA, 227, …\n```\n\n\n:::\n:::\n\n\n\n## Plan\n\n1. Create a new module R script (and possible a supporting R script) by using `golem::add_module()` in the `dev/02_dev.R` script.\n1. Pick a Shiny input type that matches our requirements.\n1. Ensure the server-side portion of the module returns the result of the selection, with additional post-processing if necessary.\n1. Plug in the UI and server-side portions of the module in the main `app_ui.R` and `app_server.R` files, respectively.",
"supporting": [
"codealong-1_files"
],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
59 changes: 59 additions & 0 deletions materials/d1-02-structure/codealong-1.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: 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`

```{r}
#| echo: true
#| eval: false
dplyr::glimpse(sets)
```

```{r}
#| echo: false
#| eval: true
sets <- readRDS("../../data_examples/df_sets.rds")
dplyr::glimpse(sets)
```

### `themes`

```{r}
#| echo: true
#| eval: false
dplyr::glimpse(themes)
```

```{r}
#| echo: false
#| eval: true
themes <- readRDS("../../data_examples/df_themes.rds")
dplyr::glimpse(themes)
```


## 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.
1. Pick a Shiny input type that matches our requirements.
1. Ensure the server-side portion of the module returns the result of the selection, with additional post-processing if necessary.
1. Plug in the UI and server-side portions of the module in the main `app_ui.R` and `app_server.R` files, respectively.
50 changes: 50 additions & 0 deletions materials/d1-02-structure/ex-1.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "Value Metrics Module"
---

## Access Instructions

The project used for this particular exercise is hosted on [Posit Cloud](https://posit.cloud) in this [space](https://posit.cloud/spaces/400774/join?access_code=DDgV_peF5WCCCpB5JHjQtMN2aHByWoNF0k5p8Wp7). The project for this exercise is called **modules-exercise1**.

## Exercise Instructions

Your task is to create a new Shiny module that displays three important metrics to the user:

* Total number of sets
* Total number of parts among the sets
* Total number of mini-figures

Note that these quantities are dependent on the user selections from the inputs contained in the left sidebar (theme, year, and parts range).

Keeping with the overall user interface style, you are recommended to use the [`value_box`](https://rstudio.github.io/bslib/reference/value_box.html) function from the [`{bslib}`](https://rstudio.github.io/bslib/) package. The metrics can be derived using the function below. In the application code, you will find reactive data frames called `sets_rv` and `part_meta_rv` which can be used in the parameters of the function below.

```r
#' Derive key LEGO data set metrics
#'
#' @param sets_rv data frame containing sets information
#' @param part_meta_rv data frame containing parts metadata information
#'
#' @import dplyr
derive_widget_metrics <- function(sets_rv, part_meta_rv) {
# number of sets
n_sets <- length(unique(sets_rv$set_num))

# number of parts
n_parts <- sum(sets_rv$num_parts, na.rm = TRUE)

# number of minifigs
n_minifigs <- part_meta_rv |>
summarize(total_minifigs = sum(minifig_ind)) |>
collect() |>
pull(total_minifigs)

return(
list(
n_sets = n_sets,
n_parts = n_parts,
n_minifigs = n_minifigs
)
)
}
```

20 changes: 20 additions & 0 deletions units/d1-02-structure.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ listing:
sort-ui: false
filter-ui: false
image-placeholder: assets/img/placeholder.png
- id: code-along
contents:
- ../materials/d1-02-structure/codealong-*.qmd
type: table
fields: [subtitle, title]
field-display-names:
subtitle: "Code-Along"
sort: [filename]
sort-ui: false
filter-ui: false
image-placeholder: assets/img/placeholder.png
tbl-colwidths: [5,20,75]
---

Expand All @@ -29,4 +40,13 @@ These slides are under construction and will be finalized prior to the workshop
```{=html}
<iframe class="slide-deck" src="../materials/d1-02-structure/" height="420" width="747" style="border: 1px solid #2e3846;"></iframe>
```
## Code-Along Examples

::: {#code-along}
:::

## Exercises

::: {#exercises}
:::

0 comments on commit e3c3561

Please sign in to comment.