Skip to content

Commit

Permalink
style: 💄 show where code should be written in
Browse files Browse the repository at this point in the history
Closes #149
  • Loading branch information
lwjohnst86 committed Oct 28, 2024
1 parent ac83a29 commit 86ed669
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 37 deletions.
12 changes: 8 additions & 4 deletions preamble/pre-course.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ If you *have not* used it, please do these tasks:
from the introduction course. Specifically, type in the RStudio
Console:

``` r
```{.r filename="Console"}
# There will be a pop-up to type in your name (first and
# last), as well as your email
r3::setup_git_config()
Expand All @@ -134,7 +134,7 @@ If you *have not* used it, please do these tasks:
Regardless of whether you've done the steps above or not, *everyone*
needs to run:

``` r
```{.r filename="Console"}
r3::check_setup()
```

Expand Down Expand Up @@ -207,6 +207,7 @@ When the RStudio Project opens up again, run these commands in the R
Console to finish the setup:

```{r more-project-setup}
#| filename: Console
#| purl: true
# Add Git to the project
prodigenr::setup_with_git()
Expand Down Expand Up @@ -258,6 +259,7 @@ Please do these two tasks:
`doc/` folder.

```{r add-rmarkdown-doc}
#| filename: Console
#| purl: true
r3::create_qmd_doc()
```
Expand Down Expand Up @@ -295,6 +297,7 @@ your `AdvancedR3` R Project, go to the Console pane in RStudio and type
out:

```{r create-data-raw}
#| filename: Console
#| purl: true
usethis::use_data_raw("nmr-omics")
```
Expand All @@ -313,7 +316,7 @@ The R script should have opened up for you, otherwise, go into the
thing to do is delete all the code in the script. Than, copy and paste
the code below into the script.

```{r insert-data-raw-script, file=here::here('data-raw/nmr-omics.R')}
```{r insert-data-raw-script, file=here::here('data-raw/nmr-omics.R'), filename="data-raw/nmr-omics.R"}
```

```{r purl-only-paste-processing-code}
Expand Down Expand Up @@ -367,6 +370,7 @@ ignore the files created in the `data-raw/nmr-omics/` folder. In the
Console, type out the code below. You only need to do this once.

```{r ignore-xlsx-files}
#| filename: Console
#| purl: true
usethis::use_git_ignore("data-raw/nmr-omics/")
```
Expand All @@ -380,7 +384,7 @@ git_ci(
)
```

``` r
```{.r filename="Console"}
r3::check_project_setup_advanced()
```

Expand Down
70 changes: 42 additions & 28 deletions sessions/pipelines.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ file with:

```{r add-targets-as-dep}
#| purl: true
#| filename: Console
use_package("targets")
```

Expand All @@ -210,6 +211,7 @@ to start using it!

```{r use-targets}
#| purl: true
#| filename: Console
targets::use_targets()
```

Expand Down Expand Up @@ -354,6 +356,7 @@ project library. `{tidyverse}` is a special "meta"-package so we need to
add it to the `"depends"` section of the `DESCRIPTION` file.

```{r add-tidyverse-deps}
#| filename: Console
#| purl: true
use_package("tidyverse", "depends")
```
Expand All @@ -365,7 +368,7 @@ Now, let's start doing some data analysis so that we can add to our
pipeline later on. First, open up the `doc/learning.qmd` file and create
a new header and code chunk at the bottom of the file.

````
````{.markdown filename="doc/learning.qmd"}

```{{r setup}}
library(tidyverse)
Expand Down Expand Up @@ -407,6 +410,7 @@ column (`across(where(is.numeric))`) to `round()` to 1 digit. Let's
write out the code!

```{r mean-sd-by-each-metabolite}
#| filename: "doc/learning.qmd"
#| eval: true
lipidomics %>%
group_by(metabolite) %>%
Expand All @@ -429,7 +433,14 @@ to take the code we wrote above and convert it into a function. Complete
these tasks:

1. Wrap the code with `function() {...}` and name the new function
`descriptive_stats`.
`descriptive_stats`. Here is some scaffolding to help you get started:

```{.r filename="doc/learning.qmd"}
descriptive_stats <- function(___) {
___
}
```

2. Replace `lipidomics` with `data` and put `data` as an argument
inside the brackets of `function()`.
3. Add `dplyr::` to the start of each `{dplyr}` function used inside
Expand Down Expand Up @@ -458,14 +469,6 @@ be the output of the function. This is called an "implicit return" and
we will be using this feature throughout the rest of this course.
:::
Here is some scaffolding to help you get started:

``` r
descriptive_stats <- function(___) {
___
}
```

```{r solution-descriptive-stats}
#| eval: true
#| code-fold: true
Expand Down Expand Up @@ -495,7 +498,9 @@ target to load the lipidomic data. In the second, replace it with the
what the target output is, we can add `df_` to remind us that it is a
data frame. It should look like:

``` r
```{r}
#| eval: false
#| filename: "targets.R"
list(
tar_target(
name = lipidomics,
Expand All @@ -511,7 +516,9 @@ list(
Let's run `{targets}` to see what happens! You can either use
{{< var keybind.targets-make >}} or run this code in the Console:

``` r
```{r}
#| eval: false
#| filename: "Console"
targets::tar_make()
```

Expand All @@ -524,7 +531,9 @@ data file. We can accomplish this by using the argument
`format = "file"` inside the `tar_target()` before loading the data
using `{readr}`.

``` r
```{r}
#| eval: false
#| filename: "targets.R"
list(
tar_target(
name = file,
Expand Down Expand Up @@ -586,7 +595,7 @@ explicitly called via `::` (e.g. `%>%`). For now, we only need to add
We can now put this code in the `packages` argument of
`tar_option_set()` in the `_targets.R` file:

``` r
```{.r filename="targets.R"}
packages = c("tibble", "dplyr")
```

Expand Down Expand Up @@ -616,13 +625,15 @@ automatically):

```{r visualize-targets}
#| purl: true
#| filename: Console
targets::tar_visnetwork()
```

Or to see what pipeline targets are outdated:

```{r outdated-targets}
#| purl: true
#| filename: Console
targets::tar_outdated()
```

Expand All @@ -646,10 +657,11 @@ write this code, let's add it to our `DESCRIPTION` file.

```{r add-ggplot2-deps}
#| purl: true
#| filename: Console
use_package("ggplot2")
```

Next, we'll switch back to `doc/lesson.qmd` and write the code to this
Next, we'll switch back to `doc/learning.qmd` and write the code to this
plot of the distribution of each metabolite. We'll use
`geom_histogram()`, nothing too fancy. And since the data is already in
long format, we can easily use `facet_wrap()` to create a plot for each
Expand All @@ -659,6 +671,7 @@ have the same range of values (some are small, others are quite large).
```{r histogram-metabolites}
#| fig-cap: "Histograms showing the distribution of all metabolites in the lipidomics dataset."
#| eval: true
#| filename: "doc/learning.qmd"
metabolite_distribution_plot <- ggplot(lipidomics, aes(x = value)) +
geom_histogram() +
facet_wrap(vars(metabolite), scales = "free")
Expand All @@ -676,8 +689,16 @@ convert it into a function. Just like you did with the
`descriptive_stats()` function in the exercise above, complete these
tasks:

1. Wrap the plot code inside `doc/lesson.qmd` with `function() {...}`
1. Wrap the plot code inside `doc/learning.qmd` with `function() {...}`
and name the new function `plot_distributions`.
Use this scaffolding code to help guide you to write the code into a
function.

```{.r filename="doc/learning.qmd"}
plot_distributions <- function(___) {
___
}
```
2. Replace `lipidomics` with `data` and put `data` as an argument
inside the brackets of `function()`.
3. Add `ggplot2::` to the start of each `{ggplot2}` function used
Expand All @@ -697,15 +718,6 @@ tasks:
8. Save both files and then open the Git interface and commit the
changes you made to them with {{< var keybind.git >}}.

Use this scaffolding code to help guide you to write the code into a
function.

``` r
plot_distributions <- function(___) {
___
}
```

```{r solution-new-function-descriptive-plots}
#| eval: false
#| code-fold: true
Expand All @@ -732,7 +744,7 @@ this `tar_target()` item within the `list()` inside `_targets.R`. To
make it easier to track things, add `fig_` to the start of the `name`
given.

``` r
```{.r filename="targets.R"}
list(
...,
tar_target(
Expand Down Expand Up @@ -788,6 +800,7 @@ install the helper package `{tarchetypes}` first, as well as the

```{r tarchetypes-deps}
#| purl: true
#| filename: Console
use_package("tarchetypes")
use_package("quarto")
```
Expand All @@ -801,7 +814,7 @@ needs, and the file path to the Quarto file. Again, like the other
using the `doc/learning.qmd` as a sandbox, we won't include it as a
pipeline target. Instead we will use the `doc/learning.qmd` file:

``` r
```{.r filename="targets.R"}
list(
...,
tar_quarto(
Expand All @@ -828,7 +841,7 @@ use of the `targets::tar_read()`.

<!-- Because the Quarto file is in another folder, -->

````
````{.markdown filename="doc/learning.qmd"}
---
# YAML header
---
Expand Down Expand Up @@ -892,6 +905,7 @@ string, you can use columns from a data frame, like `value_mean`. So we
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) %>%
mutate(MeanSD = glue::glue("{value_mean} ({value_sd})")) %>%
select(Metabolite = metabolite, `Mean SD` = MeanSD) %>%
Expand Down
5 changes: 5 additions & 0 deletions sessions/smoother-collaboration.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ these tasks:
#| code-fold: true
#| code-summary: "**Click for the solution**. Only click if you are really struggling or are out of time for the exercise."
#| purl: true
#| filename: Console
usethis::use_package("stringr")
usethis::use_package("readxl")
usethis::use_package("dplyr")
Expand Down Expand Up @@ -319,6 +320,7 @@ and add some code into it.
```{r add-usethis-rprofile}
#| eval: false
#| filename: Console
usethis::use_usethis()
```
Expand All @@ -335,6 +337,7 @@ Let's restart R with {{< var keybind.restart-r >}} before using
`use_package()` to add `{usethis}` as a workflow dependency.

```{r suggests-dep}
#| filename: Console
#| eval: false
#| purl: true
use_package("usethis", "suggests")
Expand Down Expand Up @@ -429,6 +432,7 @@ it to the `DESCRIPTION` file.
```{r add-styler-dep}
#| eval: false
#| purl: true
#| filename: Console
use_package("styler", "suggests")
```

Expand Down Expand Up @@ -462,6 +466,7 @@ If you wanted to run `{styler}` on all the files, we can use:

```{r style-dir}
#| eval: false
#| filename: Console
styler::style_dir()
```

Expand Down
Loading

0 comments on commit 86ed669

Please sign in to comment.