Skip to content

Instantly share code, notes, and snippets.

@AlbanSagouis
Created April 19, 2023 11:31
Show Gist options
  • Save AlbanSagouis/944a5c933e2da31e62cf3ac6f9114175 to your computer and use it in GitHub Desktop.
Save AlbanSagouis/944a5c933e2da31e62cf3ac6f9114175 to your computer and use it in GitHub Desktop.
Introduction to [targets] - 1 hour course plan

Introduction to targets

The Minimal targets example: the 4-minute video

https://vimeo.com/700982360

Will Landau's presentation:

https://wlandau.github.io/targets-intro/#/section

The Minimal targets example: online exercise

https://rstudio.cloud/project/1430691

Let's explore the repository and identify the key files:

  • _targets.R
  • R/functions.R

Let's install packages:
install.packages(c("targets","tarchetypes","rmarkdown","biglm", "dplyr","ggplot2","readr","tidyr","visNetwork"))

Now we can make sure everything is working by running targets: tar_make().

Let's delete index.html and run tar_outdated() and tar_visnetwork().

Exercises

Writing functions

Creating functions for targets raw_data and data

Dynamic branching

# _targets.R file:
library(targets)
list(
  tar_target(name = index, command = c(1, 2)),
  tar_target(
    name = result, 
    command = index + 7,
    pattern = map(index)
  )
)

There is also a way to easily branch over

  • all rows of a table
  • all groups of a tibble like dplyr::group_by() does (see here)
  • iterations (see here)
  • all combinations of a group of parameters (see here)

Switching from one core to several

Creating a function that could run on one CPU or several ones.
Install the clustermq package and modify _targets.R.

# _targets.R file:
library(targets)
source('R/functions.R')
options(clustermq.scheduler = "multiprocess")

count_until <- function(x) {
  count = 0L
  while (count != x) {
    count = count + 1L
  }
}


list(
  tar_target(name = x, command = rep(100000006,10)),
  tar_target(
    name = result,
    command = count_until(x),
    pattern = map(x)
  )
)

Setting up a local session

Installing packages

renv::restore() # Yes you want to initiate first

If it does not work, let's focus on just some packages:
renv::install(c("targets","tarchetypes","biglm", "dplyr","ggplot2","readr","tidyr"))

If you don't have renv installed:
install.packages(c("targets","tarchetypes","biglm", "dplyr","ggplot2","readr","tidyr"))

Or try pak, it's very fast and friendly

install.packages("pak")
pak::pkg_install(c("targets","tarchetypes","biglm", “dplyr","ggplot2","readr","tidyr"))

Resources

The Minimal targets example: the 4-minute video

https://vimeo.com/700982360

The introduction to targets by Will Landau:

https://wlandau.github.io/targets-intro/#/section

The stantargets tutorial:

The presentation: https://wlandau.github.io/targets-tutorial/#1

The code: https://github.com/wlandau/stantargets-example-validation

The targets manual book:

https://books.ropensci.org/targets/

The 4 hour tutorial with code and presentation

https://github.com/wlandau/targets-tutorial

The rOpenSci Community call on targets:

https://ropensci.org/commcalls/jan2023-targets/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment