Skip to content

Commit

Permalink
docs(sessions): 📝 split exercises up into smaller sets
Browse files Browse the repository at this point in the history
Closes #139
  • Loading branch information
lwjohnst86 committed Dec 5, 2024
1 parent c323b1c commit f5ff5cf
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 121 deletions.
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
Loading

0 comments on commit f5ff5cf

Please sign in to comment.