Skip to content

Commit

Permalink
style: 🎨 ran styler and reformatted markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjohnst86 committed Dec 5, 2024
1 parent f5ff5cf commit da36df0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 60 deletions.
10 changes: 5 additions & 5 deletions appendix/model-variability.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 >}}.
Expand Down Expand Up @@ -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(
Expand Down
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
57 changes: 30 additions & 27 deletions sessions/stats-analyses-multiple.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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."
Expand Down Expand Up @@ -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}}
Expand Down

0 comments on commit da36df0

Please sign in to comment.