Chapter 7 Defining targets

In the previous section we demonstrated how to get a basic targets pipeline created. Now we need to populate it with analytical steps.

In this instance we’ll develop our list of targets upfront, as an outline, before executing any analyses. In a real-world research scenario you might not do it in this order. For example, you might already have some cleaning or anlaysis steps partially written in other scripts that you combine with the targets pipeline as you develop it.

In this example we will be automating the following tasks with our pipeline:

  1. Downloading the dataset
  2. Cleaning the dataset
  3. Making a plot of the dataset
  4. Fitting a basic model to the dataset
  5. Combining the plot and model in an R Markdown document

Each step will be defined in the list() portion of our _targets.R script.

First we should create a target that downloads the penguin dataset. To define a target we use the tar_target() function. This function requires us to give the target a name (name) and provide code to define it (command). For now we’ll name our targets and then write up code for them in the next section.

Our target list currently looks like this:

# End this file with a list of target objects.
list(
  
)

We’ll add a target called penguin_data:

# End this file with a list of target objects.
list(
  
  tar_target(name = penguin_data,
             command = ) 
  
)

Now add in the rest, making sure to separate them each with commas. Note that we don’t need to include the name and command argument names.

list(
  
  tar_target(name = penguin_data,
             command = ),
  
  tar_file(penguin_file,
  ),
  
  tar_target(cleaned_data,
  ),
  
  tar_target(exploratory_plot,
  ),
  
  tar_target(penguin_model,
  ),
  
  tar_render(analysis_report,
  )
  
)

Most of this is the same as above, but I’ve used tar_file() and tar_render() from the tarchetypes package here. tar_file() creates a target that returns the path to a file not included in the targets internal storage. tar_render() adds a target to our workflow that builds an R Markdown document. The tarchetypes package contains functions like this to define some special cases of targets that you might want to include in your pipeline.

So far these targets are just names with no code to define how they work. But this gives us a conceptual framework to work from in the following sections where we’ll design our analysis of the penguin dataset.