From c323b1c14c75e3e0c85e17e4c04e734f0cfce5c8 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 5 Dec 2024 21:50:59 +0100 Subject: [PATCH 1/4] chore: :technologist: fix some typos in the snippet for exercises --- .vscode/json.code-snippets | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.vscode/json.code-snippets b/.vscode/json.code-snippets index d92e388..d58068d 100644 --- a/.vscode/json.code-snippets +++ b/.vscode/json.code-snippets @@ -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}", ], @@ -61,7 +61,7 @@ "## :book: Reading task: ~${0:Minutes}", "", "${1:Text body}", - "", + "", "{{< include ../includes/_sticky_up.qmd >}}", ":::", ], From f5ff5cfdf5d56613edba31e8e03a6bda0662a647 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 5 Dec 2024 21:51:33 +0100 Subject: [PATCH 2/4] docs(sessions): :memo: split exercises up into smaller sets Closes #139 --- appendix/model-variability.qmd | 77 +++++----- sessions/stats-analyses-multiple.qmd | 212 ++++++++++++++++----------- 2 files changed, 168 insertions(+), 121 deletions(-) diff --git a/appendix/model-variability.qmd b/appendix/model-variability.qmd index 12de0f5..fd30313 100644 --- a/appendix/model-variability.qmd +++ b/appendix/model-variability.qmd @@ -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 ``` @@ -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 @@ -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 @@ -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. @@ -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) ) ) ``` @@ -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", @@ -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"} diff --git a/sessions/stats-analyses-multiple.qmd b/sessions/stats-analyses-multiple.qmd index b232a0e..8bacef7 100644 --- a/sessions/stats-analyses-multiple.qmd +++ b/sessions/stats-analyses-multiple.qmd @@ -267,14 +267,65 @@ model_estimates <- lipidomics |> model_estimates ``` -Wow! We're basically at our first `{targets}` output! Before continuing, -there is one aesthetic thing we can add: The original variable names, -rather than the snake case version. Since the original variable still -exists in our `lipidomics` dataset, we can join it to the -`model_estimates` object with `right_join()`, along with a few other -minor changes. First, we'll `select()` only the `metabolite` and then -create a duplicate column of `metabolite` called `term` (to match the -`model_estimates`) using `mutate()`. +Wow! We're basically at our first `{targets}` output! Before continuing, do an +exercise to convert the code into a function. + +## :technologist: Exercise: Create a function to calculate the model estimates + +> Time: \~10 minutes. + +Convert the code into a function that calculates the model estimates. +Use this scaffold to help complete the tasks below for the +`calculate_estimates()` function: + +``` r +calculate_estimates <- function(data) { + ___ |> + # Code from right before the exercise that creates the results + ___ +} +``` + +1. Name the new function `calculate_estimates`. +2. Within the `function()`, add one argument called `data`. +3. Paste the code we created from above into the function, replacing `lipidomics` + with `data`. +4. Add `dplyr::`, `purrr::`, and `stringr::` before their respective functions. +5. Add the Roxygen documentation using {{< var keybind.roxygen >}}. +6. Use {{< var keybind.styler >}} to style the file to fix up the code. +7. Cut and paste the function over into the `R/functions.R` file. +8. Commit the changes you've made so far with {{< var keybind.git >}}. + +```{r solution-new-function-calculate-estimates} +#| eval: false +#| code-fold: true +#| code-summary: "**Click for the solution**. Only click if you are struggling or are out of time." +#' Calculate the estimates for the model for each metabolite. +#' +#' @param data The lipidomics dataset. +#' +#' @return A data frame. +#' +calculate_estimates <- function(data) { + data |> + split_by_metabolite() |> + purrr::map(generate_model_results) |> + purrr::list_rbind() |> + dplyr::filter(stringr::str_detect(term, "metabolite_")) +} +``` + +## Improve the readability of the model output + +We now have a function that calculates the model estimates, but it +outputs the variable names in snake case. While this is a good practice +for programming, it is not the most aesthetic for reporting. Let's add +back the original variable names, rather than the snake case version. Since the +original variable still exists in our `lipidomics` dataset, we can join it to +the `model_estimates` object with `right_join()`, along with a few other minor +changes. First, we'll `select()` only the `metabolite` and then create a +duplicate column of `metabolite` called `term` (to match the `model_estimates`) +using `mutate()`. ```{r duplicate-original-vars} #| filename: "doc/learning.qmd" @@ -284,7 +335,8 @@ lipidomics |> ``` Right after that we will use our custom `column_values_to_snake_case()` -function on the `term` column. +function on the `term` column so it matches the `term` variable in the +model estimates data frame. ```{r dup-column-to-snakecase} #| filename: "doc/learning.qmd" @@ -332,98 +384,87 @@ lipidomics |> right_join(model_estimates, by = "term") ``` -Awesome :smile: Now can you guess what we are going to do next? That's -right, making a function of both the model creation code and this code -to add the original variable names. Then we can add our first -`{targets}` output! +Awesome :smile: Now let's bring this code into the `calculate_estimates()` +function we created. Then afterwards we can add our first `{targets}` output! -## :technologist: Exercise: Creating functions for model results and adding as a target in the pipeline +## :technologist: Exercise: Incorporate additional code into an existing function - +> Time: \~12 minutes. -> Time: \~25 minutes. - -::: {.callout-warning appearance="default"} -There are two parts to this exercise and two solution chunks. -::: - -**Part 1**: Convert the code that calculates the model estimates as well -as the code that adds the original metabolite names into functions. -Start with the code for the metabolite names, using the scaffold below -as a starting point. - -1. Name the new function `add_original_metabolite_names`. -2. Within the `function()`, add two arguments, where the first is - called `model_results` and the second is called `data`. -3. Paste the code we created into the function, replacing `lipidomics` - with `data` and `model_estimates` with `model_results`. -4. Add `dplyr::` and `stringr::` before their respective functions. -5. Add the Roxygen documentation using {{< var keybind.roxygen >}}. -6. Use {{< var keybind.styler >}} to style the file to fix up the code. -7. Cut and paste the function over into the `R/functions.R` file. -8. Commit the changes you've made so far with {{< var keybind.git >}}. +Taking the code above, we'll insert it into the `calculate_estimates()` +function so it has a nicer looking and more readable output. While in the +`R/functions.R`, go to the `calculate_estimates()` function. Use this scaffold +to help with the tasks below for adding more code to the `calculate_estimates()` +function: ``` r -___ <- function(___, ___) { - ___ |> +calculate_estimates <- function(data) { + # 1. Assign the output to a object. + ___ <- data |> + split_by_metabolite() |> + purrr::map(generate_model_results) |> + purrr::list_rbind() |> + dplyr::filter(stringr::str_detect(term, "metabolite_")) + + data |> + # 2. Add the code we created above. + ___ |> + # 3. Include the object from the first step here. + right_join(___, by = "term") } ``` -```{r solution-new-function-add-original-metabolite-names} +1. Assign the output of the first chunk of code that creates the model + estimates and name the new variable `model_estimates`. +2. Paste the code we created into the function, so that the `data` + variable pipes into it. +3. Add `model_estimates` to the `right_join()` function. +4. Add `dplyr::` and `stringr::` before their respective functions. +5. Use {{< var keybind.styler >}} to style the file to fix up the code. +7. Commit the changes you've made so far with {{< var keybind.git >}}. + +```{r solution-add-new-code-to-calculate-estimates} #| eval: false #| code-fold: true #| code-summary: "**Click for the solution**. Only click if you are struggling or are out of time." -#' Add the original metabolite names (not as snakecase) to the model results. +#' Calculate the estimates for the model for each metabolite. #' -#' @param model_results The data frame with the model results. -#' @param data The original, unprocessed lipidomics dataset. +#' @param data The lipidomics dataset. #' #' @return A data frame. #' -add_original_metabolite_names <- function(model_results, data) { +calculate_estimates <- function(data) { + model_estimates <- data |> + split_by_metabolite() |> + purrr::map(generate_model_results) |> + purrr::list_rbind() |> + dplyr::filter(stringr::str_detect(term, "metabolite_")) + data |> dplyr::mutate(term = metabolite) |> column_values_to_snake_case(term) |> dplyr::mutate(term = stringr::str_c("metabolite_", term)) |> dplyr::distinct(term, metabolite) |> - dplyr::right_join(model_results, by = "term") + dplyr::right_join(model_estimates, by = "term") } ``` -**Part 2**: Do the same thing with the code that creates the model -results, using the scaffold below as a starting point. +## :technologist: Exercise: Add a target for the model results -``` r -calculate_estimates <- function(data) { - ___ |> - # All the other code to create the results - ___ |> - add_original_metabolite_names(data) -} -``` +> Time: ~8 minutes -```{r solution-new-function-calculate-estimates} -#| eval: false -#| code-fold: true -#| code-summary: "**Click for the solution**. Only click if you are struggling or are out of time." -#' Calculate the estimates for the model for each metabolite. -#' -#' @param data The lipidomics dataset. -#' -#' @return A data frame. -#' -calculate_estimates <- function(data) { - data |> - split_by_metabolite() |> - purrr::map(generate_model_results) |> - purrr::list_rbind() |> - dplyr::filter(stringr::str_detect(term, "metabolite_")) |> - add_original_metabolite_names(data) -} -``` +Taking the function code from above, you'll next add the model results output to +end of the `_targets.R` file, using the below scaffold as a guide. -Lastly, add the model results output to end of the `_targets.R` file, -using the below scaffold as a guide. +``` r +list( + ..., + tar_target( + name = ___, + command = ___(___) + ) +) +``` 1. Use `df_model_estimates` for the `name`. 2. Use the `calculate_estimates()` function in `command`, with @@ -434,12 +475,15 @@ using the below scaffold as a guide. `targets::tar_make()` with {{< var keybind.targets-make >}}. 4. Commit the changes to the Git history with {{< var keybind.git >}}. -``` r +```{r solution-target-plot-estimates} +#| 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_estimates, + command = calculate_estimates(lipidomics) ) ) ``` @@ -574,11 +618,6 @@ Hopefully you've gotten comfortable with the function-oriented workflow, because we'll need to convert this plot code into a function and add it as a target in the pipeline. Use the scaffold below as a guide. -1. Replace `model_estimates` with `results`. -2. Move the function into the `R/functions.R` file, add the Roxygen - documentation using {{< var keybind.roxygen >}}, and use - {{< var keybind.styler >}} to style. - ``` r plot_estimates <- function(results) { ___ |> @@ -587,6 +626,11 @@ plot_estimates <- function(results) { } ``` +1. Replace `model_estimates` with `results`. +2. Move the function into the `R/functions.R` file, add the Roxygen + documentation using {{< var keybind.roxygen >}}, and use + {{< var keybind.styler >}} to style. + ```{r solution-new-function-plot-estimates} #| eval: false #| code-fold: true From da36df0beadcbc79452e122a7c11419b7e815a5e Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 5 Dec 2024 22:07:15 +0100 Subject: [PATCH 3/4] style: :art: ran styler and reformatted markdown --- appendix/model-variability.qmd | 10 ++--- sessions/pipelines.qmd | 60 +++++++++++++++------------- sessions/stats-analyses-multiple.qmd | 57 +++++++++++++------------- 3 files changed, 67 insertions(+), 60 deletions(-) diff --git a/appendix/model-variability.qmd b/appendix/model-variability.qmd index fd30313..8f25a0a 100644 --- a/appendix/model-variability.qmd +++ b/appendix/model-variability.qmd @@ -302,7 +302,7 @@ metabolites_with_bootstrap_results > Time: \~8 minutes. -Continue the workflow we've applied throughout the course, convert the +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. @@ -314,8 +314,8 @@ calculate_variation <- function(___) { } ``` -1. Move the code we made right before this exercise into a function structure - (use the scaffold below as a guide). +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 >}}. @@ -346,8 +346,8 @@ calculate_variation <- function(data) { > Time: \~8 minutes. -Continuing from the previous exercise, add the `calculate_variation()` function -to `_targets.R`. Use this code as a scaffold: +Continuing from the previous exercise, add the `calculate_variation()` +function to `_targets.R`. Use this code as a scaffold: ``` r list( diff --git a/sessions/pipelines.qmd b/sessions/pipelines.qmd index f40e009..762ed4e 100644 --- a/sessions/pipelines.qmd +++ b/sessions/pipelines.qmd @@ -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") @@ -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. @@ -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))) } ``` @@ -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) + ) ) ``` @@ -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 @@ -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") @@ -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( @@ -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)", @@ -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", @@ -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 @@ -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.") @@ -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.") @@ -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.") diff --git a/sessions/stats-analyses-multiple.qmd b/sessions/stats-analyses-multiple.qmd index 8bacef7..8ee2234 100644 --- a/sessions/stats-analyses-multiple.qmd +++ b/sessions/stats-analyses-multiple.qmd @@ -89,7 +89,7 @@ the `lipidomics_wide` dataset. However, these types of long form. So we'll start with the original `lipidomics` dataset. Create a header and code chunk at the end of the `doc/learning.qmd` file: -````{.markdown filename="doc/learning.qmd"} +```` {.markdown filename="doc/learning.qmd"} ## Running multiple models ```{{r}} @@ -267,8 +267,8 @@ model_estimates <- lipidomics |> model_estimates ``` -Wow! We're basically at our first `{targets}` output! Before continuing, do an -exercise to convert the code into a function. +Wow! We're basically at our first `{targets}` output! Before continuing, +do an exercise to convert the code into a function. ## :technologist: Exercise: Create a function to calculate the model estimates @@ -288,9 +288,10 @@ calculate_estimates <- function(data) { 1. Name the new function `calculate_estimates`. 2. Within the `function()`, add one argument called `data`. -3. Paste the code we created from above into the function, replacing `lipidomics` - with `data`. -4. Add `dplyr::`, `purrr::`, and `stringr::` before their respective functions. +3. Paste the code we created from above into the function, replacing + `lipidomics` with `data`. +4. Add `dplyr::`, `purrr::`, and `stringr::` before their respective + functions. 5. Add the Roxygen documentation using {{< var keybind.roxygen >}}. 6. Use {{< var keybind.styler >}} to style the file to fix up the code. 7. Cut and paste the function over into the `R/functions.R` file. @@ -317,15 +318,15 @@ calculate_estimates <- function(data) { ## Improve the readability of the model output -We now have a function that calculates the model estimates, but it +We now have a function that calculates the model estimates, but it outputs the variable names in snake case. While this is a good practice for programming, it is not the most aesthetic for reporting. Let's add -back the original variable names, rather than the snake case version. Since the -original variable still exists in our `lipidomics` dataset, we can join it to -the `model_estimates` object with `right_join()`, along with a few other minor -changes. First, we'll `select()` only the `metabolite` and then create a -duplicate column of `metabolite` called `term` (to match the `model_estimates`) -using `mutate()`. +back the original variable names, rather than the snake case version. +Since the original variable still exists in our `lipidomics` dataset, we +can join it to the `model_estimates` object with `right_join()`, along +with a few other minor changes. First, we'll `select()` only the +`metabolite` and then create a duplicate column of `metabolite` called +`term` (to match the `model_estimates`) using `mutate()`. ```{r duplicate-original-vars} #| filename: "doc/learning.qmd" @@ -384,18 +385,19 @@ lipidomics |> right_join(model_estimates, by = "term") ``` -Awesome :smile: Now let's bring this code into the `calculate_estimates()` -function we created. Then afterwards we can add our first `{targets}` output! +Awesome :smile: Now let's bring this code into the +`calculate_estimates()` function we created. Then afterwards we can add +our first `{targets}` output! ## :technologist: Exercise: Incorporate additional code into an existing function > Time: \~12 minutes. Taking the code above, we'll insert it into the `calculate_estimates()` -function so it has a nicer looking and more readable output. While in the -`R/functions.R`, go to the `calculate_estimates()` function. Use this scaffold -to help with the tasks below for adding more code to the `calculate_estimates()` -function: +function so it has a nicer looking and more readable output. While in +the `R/functions.R`, go to the `calculate_estimates()` function. Use +this scaffold to help with the tasks below for adding more code to the +`calculate_estimates()` function: ``` r calculate_estimates <- function(data) { @@ -415,13 +417,13 @@ calculate_estimates <- function(data) { ``` 1. Assign the output of the first chunk of code that creates the model - estimates and name the new variable `model_estimates`. -2. Paste the code we created into the function, so that the `data` + estimates and name the new variable `model_estimates`. +2. Paste the code we created into the function, so that the `data` variable pipes into it. 3. Add `model_estimates` to the `right_join()` function. 4. Add `dplyr::` and `stringr::` before their respective functions. 5. Use {{< var keybind.styler >}} to style the file to fix up the code. -7. Commit the changes you've made so far with {{< var keybind.git >}}. +6. Commit the changes you've made so far with {{< var keybind.git >}}. ```{r solution-add-new-code-to-calculate-estimates} #| eval: false @@ -451,10 +453,11 @@ calculate_estimates <- function(data) { ## :technologist: Exercise: Add a target for the model results -> Time: ~8 minutes +> Time: \~8 minutes -Taking the function code from above, you'll next add the model results output to -end of the `_targets.R` file, using the below scaffold as a guide. +Taking the function code from above, you'll next add the model results +output to end of the `_targets.R` file, using the below scaffold as a +guide. ``` r list( @@ -475,7 +478,7 @@ list( `targets::tar_make()` with {{< var keybind.targets-make >}}. 4. Commit the changes to the Git history with {{< var keybind.git >}}. -```{r solution-target-plot-estimates} +```{r solution-target-model-estimates} #| eval: false #| code-fold: true #| code-summary: "**Click for the solution**. Only click if you are struggling or are out of time." @@ -530,7 +533,7 @@ inside the `## Results` section. We'll want to use `tar_read(df_model_estimates)` so that `{targets}` is aware that the R Markdown file is dependent on this target. -````{.markdown filename="doc/learning.qmd"} +```` {.markdown filename="doc/learning.qmd"} ### Figure of model estimates ```{{r}} From 3440d2dad15d2a2159afd7fadd51531767fd1094 Mon Sep 17 00:00:00 2001 From: "Luke W. Johnston" Date: Thu, 5 Dec 2024 22:07:54 +0100 Subject: [PATCH 4/4] chore: :memo: wrong word used in justfile recipe --- justfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/justfile b/justfile index aaef9a2..de3e6bc 100644 --- a/justfile +++ b/justfile @@ -20,6 +20,6 @@ style: #!/usr/bin/Rscript styler::style_dir(here::here()) -# Build pkgdown website +# Build website build-site: quarto render