Search code examples
rtargets-r-package

Find the code used to run one dynamic branch from id with targets


I have a targets pipeline where a first target looks in a directory for data files and a second one uses dynamic branching to read and format it.

library(targets)
read <- function(file){
  f <- read.csv(file)
  f$date <- format.Date(f$date)
  return(f)
}

list(
  tar_target(files, dir(data, pattern = ".csv")),
  tar_target(my_target, read(files), pattern = files)
) 

Now one of the targets throws an error:

✖ errored branch my_target_ec58af61779dfe2e
...
── Last error message ──────────────────────────────────────────
    do not know how to convert 'x' to class “POSIXct”

I understand that one data file has a format error that I need to correct. But how do I know what data file this branch uses?

To clarify, I am looking for a function tar_document_branches() that would produce this kind of result:

tar_document_branches(my_target)

branche                    code_run
my_target_4ds5z78dq6qs45ds read(data/file1.csv)
my_target_ec58af61779dfe2e read(data/file2.csv)

So that I know that the error is in file2.csv


Solution

  • tar_workspace(target_id) loads the data used by each target and helps debugging. In the case above:

    > tar_workspace(my_target_ec58af61779dfe2e)
    

    loads object files in memory, and its value is the file that produced the error:

    > files
    data/file2.csv
    

    Sources: this github issue I started and the targets manual