Skip to content

Commit

Permalink
differences for PR #61
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 26, 2024
1 parent 31b641d commit e0ecef6
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 100 deletions.
235 changes: 167 additions & 68 deletions basic-targets.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: 'First targets Workflow'
teaching: 10
exercises: 2
teaching: 30
exercises: 10
---

:::::::::::::::::::::::::::::::::::::: questions
Expand Down Expand Up @@ -69,48 +69,10 @@ Once you work through these steps, your RStudio session should look like this:

Our project now contains a single file, created by RStudio: `targets-demo.Rproj`. You should not edit this file by hand. Its purpose is to tell RStudio that this is a project folder and to store some RStudio settings (if you use version-control software, it is OK to commit this file). Also, you can open the project by double clicking on the `.Rproj` file in your file explorer (try it by quitting RStudio then navigating in your file browser to your Desktop, opening the "targets-demo" folder, and double clicking `targets-demo.Rproj`).

OK, now that our project is set up, we are ready to start using `targets`!
OK, now that our project is set up, we are (almost) ready to start using `targets`!

## Create a `_targets.R` file
## Background: non-`targets` version

Every `targets` project must include a special file, called `_targets.R` in the main project folder (the "project root").
The `_targets.R` file includes the specification of the workflow: directions for R to run your analysis, kind of like a recipe.
By using the `_targets.R` file, you won't have to remember to run specific scripts in a certain order.
Instead, R will do it for you (more reproducibility points)!

### Anatomy of a `_targets.R` file

We will now start to write a `_targets.R` file. Fortunately, `targets` comes with a function to help us do this.

In the R console, first load the `targets` package with `library(targets)`, then run the command `tar_script()`.


``` r
library(targets)
tar_script()
```

Nothing will happen in the console, but in the file viewer, you should see a new file, `_targets.R` appear. Open it using the File menu or by clicking on it.

We can see this default `_targets.R` file includes three main parts:

- Loading packages with `library()`
- Defining a custom function with `function()`
- Defining a list with `list()`.

The last part, the list, is the most important part of the `_targets.R` file.
It defines the steps in the workflow.
The `_targets.R` file must always end with this list.

Furthermore, each item in the list is a call of the `tar_target()` function.
The first argument of `tar_target()` is name of the target to build, and the second argument is the command used to build it.
Note that the name of the target is **unquoted**, that is, it is written without any surrounding quotation marks.

## Set up `_targets.R` file to run example analysis

### Background: non-`targets` version

We will use this template to start building our analysis of bill shape in penguins.
First though, to get familiar with the functions and packages we'll use, let's run the code like you would in a "normal" R script without using `targets`.

Recall that we are using the `palmerpenguins` R package to obtain the data.
Expand All @@ -135,7 +97,6 @@ penguins_csv_file

We will use the `tidyverse` set of packages for loading and manipulating the data. We don't have time to cover all the details about using `tidyverse` now, but if you want to learn more about it, please see the ["Manipulating, analyzing and exporting data with tidyverse" lesson](https://datacarpentry.org/R-ecology-lesson/03-dplyr.html), or the Carpentry incubator lesson [R and the tidyverse for working with datasets](https://carpentries-incubator.github.io/r-tidyverse-4-datasets/).


Let's load the data with `read_csv()`.


Expand Down Expand Up @@ -225,42 +186,159 @@ penguins_data
# ℹ 332 more rows
```

That's better!
We have not run the full analysis yet, but this is enough to get us started with the transition to using `targets`.

### `targets` version
## `targets` version

What does this look like using `targets`?
### About the `_targets.R` file

The biggest difference is that we need to **put each step of the workflow into the list at the end**.
One major difference between a typical R data analysis and a `targets` project is that the latter must include a special file, called `_targets.R` in the main project folder (the "project root").

We also define a custom function for the data cleaning step.
That is because the list of targets at the end **should look like a high-level summary of your analysis**.
You want to avoid lengthy chunks of code when defining the targets; instead, put that code in the custom functions.
The other steps (setting the file path and loading the data) are each just one function call so there's not much point in putting those into their own custom functions.
The `_targets.R` file includes the specification of the workflow: these are the directions for R to run your analysis, kind of like a recipe.
By using the `_targets.R` file, **you won't have to remember to run specific scripts in a certain order**; instead, R will do it for you!
This is a **huge win**, both for your future self and anybody else trying to reproduce your analysis.

Finally, each step in the workflow is defined with the `tar_target()` function.
### Writing the initial `_targets.R` file

We will now start to write a `_targets.R` file. Fortunately, `targets` comes with a function to help us do this.

In the R console, first load the `targets` package with `library(targets)`, then run the command `tar_script()`.


``` r
library(targets)
library(tidyverse)
library(palmerpenguins)
tar_script()
```

clean_penguin_data <- function(penguins_data_raw) {
penguins_data_raw |>
select(
species = Species,
bill_length_mm = `Culmen Length (mm)`,
bill_depth_mm = `Culmen Depth (mm)`
) |>
drop_na()
Nothing will happen in the console, but in the file viewer, you should see a new file, `_targets.R` appear. Open it using the File menu or by clicking on it.

```{.r}
library(targets)
# This is an example _targets.R file. Every
# {targets} pipeline needs one.
# Use tar_script() to create _targets.R and tar_edit()
# to open it again for editing.
# Then, run tar_make() to run the pipeline
# and tar_read(data_summary) to view the results.
# Define custom functions and other global objects.
# This is where you write source(\"R/functions.R\")
# if you keep your functions in external scripts.
summarize_data <- function(dataset) {
colMeans(dataset)
}
# Set target-specific options such as packages:
# tar_option_set(packages = "utils") # nolint
# End this file with a list of target objects.
list(
tar_target(data, data.frame(x = sample.int(100), y = sample.int(100))),
tar_target(data_summary, summarize_data(data)) # Call your custom functions.
)
```

Don't worry about the details of this file.
Instead, notice that that it includes three main parts:

- Loading packages with `library()`
- Defining a custom function with `function()`
- Defining a list with `list()`.

You may not have used `function()` before.
If not, that's OK; we will cover this in more detail in the [next episode](episodes/functions.Rmd), so we will ignore it for now.

The last part, the list, is the **most important part** of the `_targets.R` file.
It defines the steps in the workflow.
The `_targets.R` file **must always end with this list**.

Furthermore, each item in the list is a call of the `tar_target()` function.
The first argument of `tar_target()` is name of the target to build, and the second argument is the command used to build it.
Note that the name of the target is **unquoted**, that is, it is written without any surrounding quotation marks.

## Modifying `_targets.R` to run the example analysis

First, let's load all of the packages we need for our workflow.
Add `library(tidyverse)` and `library(palmerpenguins)` to the top of `_targets.R` after `library(targets)`.

Next, we can delete the `function()` statement since we won't be using that just yet (we will come back to custom functions soon!).

The last, and trickiest, part is correctly defining the workflow in the list at the end of the file.

From [the non-`targets` version](#background-non-targets-version), you can see we have three steps so far:

1. Define the path to the CSV file with the raw penguins data.
2. Read the CSV file.
3. Clean the raw data.

Each of these will be one item in the list.
Furthermore, we need to write each item using the `tar_target()` function.
Recall that we write the `tar_target()` function by writing the **name of the target to build** first and the **command to build it** second.

::::::::::::::::::::::::::::::::::::: {.callout}

## Choosing good target names

The name of each target could be anything you like, but it is strongly recommended to **choose names that reflect what the target actually contains**.

For example, `penguins_data_raw` for the raw data loaded from the CSV file and not `x`.

Your future self will thank you!

::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::: {.challenge}

## Challenge: Use `tar_target()`

Can you use `tar_target()` to define the first step in the workflow (setting the path to the CSV file with the penguins data)?

:::::::::::::::::::::::::::::::::: {.solution}


``` r
tar_target(name = penguins_csv_file, command = path_to_file("penguins_raw.csv"))
```

The first two arguments of `tar_target()` are the **name** of the target, followed by the **command** to build it.

These arguments are used so frequently we will typically omit the argument names, instead writing it like this:


``` r
tar_target(penguins_csv_file, path_to_file("penguins_raw.csv"))
```

::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::

Now that we've seen how to define the first target, let's continue and add the rest.

Once you've done that, this is how `_targets.R` should look:


``` r
library(targets)
library(tidyverse)
library(palmerpenguins)

list(
tar_target(penguins_csv_file, path_to_file("penguins_raw.csv")),
tar_target(penguins_data_raw, read_csv(
penguins_csv_file, show_col_types = FALSE)),
tar_target(penguins_data, clean_penguin_data(penguins_data_raw))
tar_target(
penguins_data_raw,
read_csv(penguins_csv_file, show_col_types = FALSE)
),
tar_target(
penguins_data,
penguins_data_raw |>
select(
species = Species,
bill_length_mm = `Culmen Length (mm)`,
bill_depth_mm = `Culmen Depth (mm)`
) |>
drop_na()
)
)
```

Expand All @@ -280,14 +358,35 @@ tar_make()
▶ dispatched target penguins_csv_file
● completed target penguins_csv_file [0.002 seconds, 190 bytes]
▶ dispatched target penguins_data_raw
● completed target penguins_data_raw [0.191 seconds, 10.403 kilobytes]
● completed target penguins_data_raw [0.189 seconds, 10.403 kilobytes]
▶ dispatched target penguins_data
● completed target penguins_data [0.006 seconds, 1.614 kilobytes]
▶ ended pipeline [0.346 seconds]
● completed target penguins_data [0.007 seconds, 1.614 kilobytes]
▶ ended pipeline [0.336 seconds]
```

Congratulations, you've run your first workflow with `targets`!

::::::::::::::::::::::::::::::::::::: {.callout}

## The workflow cannot be run interactively

You may be used to running R code interactively by selecting lines and pressing the "Run" button (or using the keyboard shortcut) in RStudio or your IDE of choice.

You *could* run the list at the of `_targets.R` this way, but it will not execute the workflow (it will return a list instead).

**The only way to run the workflow is with `tar_make()`.**

You do not need to select and run anything interactively in `_targets.R`.
In fact, you do not even need to have the `_targets.R` file open to run the workflow with `tar_make()`---try it for yourself!

Similarly, **you must not write `tar_make()` in the `_targets.R` file**; you should only use `tar_make()` as a direct command at the R console.

::::::::::::::::::::::::::::::::::::::::::

Remember, now that we are using `targets`, **the only thing you need to do to replicate your analysis is run `tar_make()`**.

This is true no matter how long or complicated your analysis becomes.

::::::::::::::::::::::::::::::::::::: keypoints

- Projects help keep our analyses organized so we can easily re-run them later
Expand Down
2 changes: 1 addition & 1 deletion cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Episode summary: Show how to get at the objects that we built

## Where does the workflow happen?

So we just finished running our first workflow.
So we just finished running our workflow.
Now you probably want to look at its output.
But, if we just call the name of the object (for example, `penguins_data`), we get an error.

Expand Down
22 changes: 22 additions & 0 deletions files/plans/plan_0.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
options(tidyverse.quiet = TRUE)
library(targets)
library(tidyverse)
library(palmerpenguins)

list(
tar_target(penguins_csv_file, path_to_file("penguins_raw.csv")),
tar_target(
penguins_data_raw,
read_csv(penguins_csv_file, show_col_types = FALSE)
),
tar_target(
penguins_data,
penguins_data_raw |>
select(
species = Species,
bill_length_mm = `Culmen Length (mm)`,
bill_depth_mm = `Culmen Depth (mm)`
) |>
drop_na()
)
)
Loading

0 comments on commit e0ecef6

Please sign in to comment.