Skip to content

Commit

Permalink
docs: 📝 split exercises so they aren't so big (#229)
Browse files Browse the repository at this point in the history
## Description

This PR splits up some exercises that were multi-part, which
participants found confusing last time.

Closes #139

<!-- Please delete as appropriate: -->
No review needed.

## Checklist

- [x] Ran spell-check
- [x] Formatted Markdown
- [x] Rendered website locally
  • Loading branch information
lwjohnst86 authored Dec 5, 2024
2 parents 7f0df68 + 3440d2d commit 1145212
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 154 deletions.
6 changes: 3 additions & 3 deletions .vscode/json.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
"scope": "quarto,markdown",
"prefix": "exercise",
"body": [
"## :technologist: Exercise: ~${0:Title}",
"## :technologist: Exercise: ${0:Title}",
"",
"**Time: ~${1:Minutes}**",
"> Time: ~${1:Minutes} minutes.",
"",
"${2:Text}",
],
Expand All @@ -61,7 +61,7 @@
"## :book: Reading task: ~${0:Minutes}",
"",
"${1:Text body}",
"",
"",
"{{< include ../includes/_sticky_up.qmd >}}",
":::",
],
Expand Down
77 changes: 40 additions & 37 deletions appendix/model-variability.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ run.
metabolites_with_bootstrap_results <- lipidomics |>
split_by_metabolite() |>
map(generate_model_variation) |>
map(tidy_bootstrap_output) |>
map(tidy_bootstrap_output) |>
list_rbind()
metabolites_with_bootstrap_results
```
Expand All @@ -298,21 +298,12 @@ metabolites_with_bootstrap_results
metabolites_with_bootstrap_results
```

## :technologist: Exercise: Convert to function and add as a target in the pipeline
## :technologist: Exercise: Convert variation calculation into a function

> Time: \~15 minutes.
Continue the workflow we've applied throughout the course:

1. Move the code into a function structure (use the scaffold below as a
guide).
2. Include one argument in the `function()` called `data`.
3. Replace `lipidomics` in the code with `data`.
4. Add the Roxygen documentation with {{< var keybind.roxgyen >}}.
5. Cut and paste the function over into the `R/functions.R` file.
6. Style using {{< var keybind.styler >}}.
7. Commit the changes to the Git history with {{< var keybind.git >}}.
> Time: \~8 minutes.
Continue the workflow we've applied throughout the course, convert the
code to calculate the variation of the model estimates into a function.
Use this code as a guide for the function.

``` r
Expand All @@ -323,6 +314,15 @@ calculate_variation <- function(___) {
}
```

1. Move the code we made right before this exercise into a function
structure (use the scaffold below as a guide).
2. Include one argument in the `function()` called `data`.
3. Replace `lipidomics` in the code with `data`.
4. Add the Roxygen documentation with {{< var keybind.roxgyen >}}.
5. Cut and paste the function over into the `R/functions.R` file.
6. Style using {{< var keybind.styler >}}.
7. Commit the changes to the Git history with {{< var keybind.git >}}.

```{r solution-new-function-calculate-variation}
#| eval: false
#| code-fold: true
Expand All @@ -337,12 +337,27 @@ calculate_variation <- function(data) {
data |>
split_by_metabolite() |>
purrr::map(generate_model_variation) |>
purrr::map(tidy_bootstrap_output) |>
purrr::map(tidy_bootstrap_output) |>
purrr::list_rbind()
}
```

Next, add the function to `_targets.R`.
## :technologist: Exercise: Add the new function as a target in the pipeline

> Time: \~8 minutes.
Continuing from the previous exercise, add the `calculate_variation()`
function to `_targets.R`. Use this code as a scaffold:

``` r
list(
...,
tar_target(
name = ___,
command = ___
)
)
```

1. Create another `tar_target()` item in the `list()` at the bottom of
the file.
Expand All @@ -354,14 +369,15 @@ Next, add the function to `_targets.R`.
{{< var keybind.targets-make >}}.
4. Commit the changes to the Git history with {{< var keybind.git >}}.

Use this code as a scaffold:

``` r
```{r solution-calculate-variation-to-pipeline}
#| eval: false
#| code-fold: true
#| code-summary: "**Click for the solution**. Only click if you are struggling or are out of time."
list(
...,
# ...,
tar_target(
name = ___,
command = ___
name = df_model_variation,
command = calculate_variation(lipidomics)
)
)
```
Expand All @@ -370,14 +386,14 @@ list(
#| eval: false
#| echo: false
#| purl: true
variation_targets_code <- '
variation_targets_code <- "
),
tar_target(
name = df_model_variation,
command = calculate_variation(lipidomics)
)
)
'
"
# print_lines("_targets.R")
revise_by_line_num(
path = "_targets.R",
Expand All @@ -391,19 +407,6 @@ targets::tar_make()
git_ci("_targets.R", "Update targets with model variation")
```

```{r solution-calculate-variation-to-pipeline}
#| eval: false
#| code-fold: true
#| code-summary: "**Click for the solution**. Only click if you are struggling or are out of time."
list(
# ...,
tar_target(
name = df_model_variation,
command = calculate_variation(lipidomics)
)
)
```

## Visualizing the variability of results

::: {.callout-note collapse="true"}
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ style:
#!/usr/bin/Rscript
styler::style_dir(here::here())

# Build pkgdown website
# Build website
build-site:
quarto render
60 changes: 32 additions & 28 deletions sessions/pipelines.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ git_ci("DESCRIPTION", "Add tidyverse and targets to deps")
c(
"\n## Basic statistics\n\n```{r setup}\nlibrary(tidyverse)",
"source(here::here('R/functions.R'))",
"lipidomics <- read_csv(here::here('data/lipidomics.csv'))\n```\n\n"
"lipidomics <- read_csv(here::here('data/lipidomics.csv'))\n```\n\n"
) |>
paste0(collapse = "\n") |>
write_to_file("doc/learning.qmd")
Expand Down Expand Up @@ -410,7 +410,7 @@ Let's write out the code!
lipidomics |>
group_by(metabolite) |>
summarise(across(value, list(mean = mean, sd = sd))) |>
mutate(across(where(is.numeric), ~round(.x, digits = 1)))
mutate(across(where(is.numeric), ~ round(.x, digits = 1)))
```

After that, style the file using {{< var keybind.styler >}} on the file.
Expand Down Expand Up @@ -485,7 +485,7 @@ descriptive_stats <- function(data) {
data |>
dplyr::group_by(metabolite) |>
dplyr::summarise(dplyr::across(value, list(mean = mean, sd = sd))) |>
dplyr::mutate(dplyr::across(tidyselect::where(is.numeric), ~round(.x, digits = 1)))
dplyr::mutate(dplyr::across(tidyselect::where(is.numeric), ~ round(.x, digits = 1)))
}
```

Expand Down Expand Up @@ -537,19 +537,19 @@ Lets do that now.
#| eval: false
#| filename: "targets.R"
list(
tar_target(
name = file,
command = "data/lipidomics.csv",
format = "file"
),
tar_target(
name = lipidomics,
command = readr::read_csv(file, show_col_types = FALSE)
),
tar_target(
name = df_stats_by_metabolite,
command = descriptive_stats(lipidomics)
)
tar_target(
name = file,
command = "data/lipidomics.csv",
format = "file"
),
tar_target(
name = lipidomics,
command = readr::read_csv(file, show_col_types = FALSE)
),
tar_target(
name = df_stats_by_metabolite,
command = descriptive_stats(lipidomics)
)
)
```

Expand Down Expand Up @@ -618,8 +618,10 @@ Before continuing, let's commit the changes (including the files in the
#| eval: false
#| echo: false
#| purl: true
git_ci(c("_targets", "_targets.R", "DESCRIPTION"),
"Tell targets about packages, add _targets")
git_ci(
c("_targets", "_targets.R", "DESCRIPTION"),
"Tell targets about packages, add _targets"
)
```

## Creating figure outputs
Expand Down Expand Up @@ -710,7 +712,7 @@ tasks:
#' @return A ggplot2 graph.
#'
plot_distributions <- function(data) {
data |>
data |>
ggplot2::ggplot(ggplot2::aes(x = value)) +
ggplot2::geom_histogram() +
ggplot2::facet_wrap(ggplot2::vars(metabolite), scales = "free")
Expand Down Expand Up @@ -746,13 +748,13 @@ works, than **commit the changes to the Git history** with
#| eval: false
#| echo: false
#| purl: true
update_targets_plots <- '
update_targets_plots <- "
),
tar_target(
name = fig_metabolite_distribution,
command = plot_distributions(lipidomics)
)
'
"
# print_lines("_targets.R")
# -20 to remove the previous `)`
revise_by_line_num(
Expand Down Expand Up @@ -851,7 +853,8 @@ tar_read(fig_metabolites_distribution)
#| eval: false
#| echo: false
#| purl: true
basic_stats_md_text <- c("```{r setup}",
basic_stats_md_text <- c(
"```{r setup}",
"library(tidyverse)",
"library(targets)",
"lipidomics <- tar_read(lipidomics)",
Expand All @@ -865,7 +868,8 @@ basic_stats_md_text <- c("```{r setup}",
"",
"```{r}",
"tar_read(fig_metabolites_distribution)",
"```")
"```"
)
print_file("doc/learning.qmd")
revise_by_line_num(
"doc/learning.qmd",
Expand All @@ -887,7 +891,7 @@ let's add it to ignore file by using:

```{r}
#| filename: Console
use_git_ignore("doc/_targets.yaml")
use_git_ignore("doc/_targets.yaml")
```

Before continuing, let's commit these changes to the Git history with
Expand All @@ -905,7 +909,7 @@ can use it to format the final table text to be `mean value (SD value)`:

```{r stats-to-table}
#| filename: "doc/learning.qmd"
targets::tar_read(df_stats_by_metabolite) |>
targets::tar_read(df_stats_by_metabolite) |>
mutate(MeanSD = glue::glue("{value_mean} ({value_sd})")) |>
select(Metabolite = metabolite, `Mean SD` = MeanSD) |>
knitr::kable(caption = "Descriptive statistics of the metabolites.")
Expand All @@ -916,7 +920,7 @@ targets::tar_read(df_stats_by_metabolite) |>
#| echo: false
#| purl: true
pretty_basic_stats_code <- '
targets::tar_read(df_stats_by_metabolite) |>
targets::tar_read(df_stats_by_metabolite) |>
mutate(MeanSD = glue::glue("{value_mean} ({value_sd})")) |>
select(Metabolite = metabolite, `Mean SD` = MeanSD) |>
knitr::kable(caption = "Descriptive statistics of the metabolites.")
Expand All @@ -933,8 +937,8 @@ git_ci("doc/learning.qmd", "Basic stats as a pretty table.")
```{r execute-only-table-basic-stats}
#| eval: true
#| echo: false
lipidomics |>
descriptive_stats() |>
lipidomics |>
descriptive_stats() |>
mutate(MeanSD = glue::glue("{value_mean} ({value_sd})")) |>
select(Metabolite = metabolite, `Mean (SD)` = MeanSD) |>
knitr::kable(caption = "The mean and standard deviation of metabolites in the lipidomics dataset.")
Expand Down
Loading

0 comments on commit 1145212

Please sign in to comment.