Achieve your target

Achieve your target

Want to understand the targets package? It won’t get any easier than this..

Last week I had to talk my colleagues through the architecture of an R project that we’ve been working on for a while.

This is a large project, as we make our first moves into Reproducible Analystic Pipelines, and makes heavy use of the {targets} package.

As I was going through it, I realised that it was way too complex, and it wasn’t reasonable to expect relatively new R users to understand such a complex layout, in addition to getting to grips with {renv}, and functional programming.

A couple of days later, I had an idea for a much simpler introduction. You don’t need to do any data munging or statistics with this, so you can focus on the intention behind each step.

Here is the idea:

  • get some images
  • create some captions for them
  • create a final, amalagamated image
  • change the captions if required and regenerate the image
  • do it all in R
  • use {targets} to track the inputs, functions and outputs
  • feel good about having a better understanding and sharing the appreciation for {targets}

What do I need to do?

The code is available here

  • Create a new R project and copy the files from the repo over, or fork/dowload from GitHub
  • install the targets and magick packages if you don’t already have them.
install.packages(c("targets","magick"), dependencies = TRUE)

# you might need visNetwork too
# install.packages(c("visNetwork"))

  • resist the temptation to look at the images
  • pay attention to the comments in the code
  • you can take a look at the R functions and try to figure out what the functions do
  • look at the _targets.R file and see if you can understand what is happening.
  • in particular, note how the names of the targets are passed as inputs to others
  • run through the code in run.R line by line
  • laugh, or not, at the final result (a new image should be created in the main folder when the code runs successfully for the first time)
  • peek at the _targets folder and sub-folders
  • use tar_read() and tar_load with some of the other files in the objects subfolder

How will this help me?

It introduces you to the concepts, without having to engage with any intensive data manipulation or plotting.

The key things to understand:

  • targets encourages you to create functions, rather than large, procedural R scripts
  • targets keeps track of any changes to your code, inputs and outputs, and only re-runs the steps it needs to as a result of those changes
  • you can track your raw data as a target in “file” format
  • once you grasp {targets}, you will never view numbered / sequential R scripts in the same light

Why Chandler?

A few years ago, I was asked to do a speaker bio for an event (possibly the first NHS-R conference).
While everybody else did a proper, professional one, all I could think of was that I was “a poor man’s Chandler Bing”.

Quite a lot of people seemed to think this was a fair asssessment.

Can this be improved?

Sure. Here are some ideas for more transformations :

  • Monica Geller -> Monica Yeller
  • Rachel Green -> Rachel Rage Against the Machine
  • Pheobe Buffet -> The Fonz saying “Hey”

Further help and guidance

You will find a link to an earlier post on the targets package in the “related posts” section below, which you may also find helpful.

© 2016 - 2023. All rights reserved.