1

The drake manual gives the following example of using dynamic subtargets:

https://books.ropensci.org/drake/dynamic.html#dynamic-transformations

library(gapminder)
library(drake)

plan <- drake_plan(
  subset = head(gapminder),
  row = target(subset, dynamic = map(subset))
)

make(plan)
#> ▶ target subset
#> ▶ dynamic row
#> > subtarget row_9939cae3
#> > subtarget row_e8047114
#> > subtarget row_2ef3db10
#> > subtarget row_f9171bbe
#> > subtarget row_7d6002e9
#> > subtarget row_509468b3
#> ■ finalize row

Created on 2020-09-02 by the reprex package (v0.3.0)

Now lets say that for some reason, one or more these subtargets fail, e.g. row_9939cae3. I would like to investigate the reason for that, and to do that I need to know the exact arguments that are being feed into the target function. How do I get a copy of that data?

Thanks for the help in advance.

Mark

Mark Payne
  • 557
  • 5
  • 12

1 Answers1

1

Unfortunately, drake does not make this easy, but it is possible. I recommend dropping into an interactive debugger for the failed sub-target. For example, suppose row_f9171bbe failed. In one of your custom functions, you can use cancel_if() and id_chr() to jump straight to row_f9171bbe and then run browser() right after that.

library(gapminder)
library(drake)
f <- function(x) {
  cancel_if(id_chr() != "row_f9171bbe")
  browser()
  x
}
plan <- drake_plan(
  subset = head(gapminder),
  row = target(f(subset), dynamic = map(subset))
)
make(plan, targets = "row")
#> ▶ target subset
#> ▶ dynamic row
#> > subtarget row_9939cae3
#> ■ cancel row_9939cae3
#> > subtarget row_e8047114
#> ■ cancel row_e8047114
#> > subtarget row_2ef3db10
#> ■ cancel row_2ef3db10
#> > subtarget row_f9171bbe
#> Called from: f(subset)
Browse[1]> print(x)
#> # A tibble: 1 x 6
#>   country     continent  year lifeExp      pop gdpPercap
#>   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
#> 1 Afghanistan Asia       1967    34.0 11537966      836.
landau
  • 5,636
  • 1
  • 22
  • 50
  • 1
    Thanks, that's a useful trick. Unfortunately it doesn't work so well when working on an HPC cluster, where you don't have access to interactive debugging. An alternative could be to figure out the correspondence between the data.frame/tibble/list elements and the hash. Is there a public function that does this conversion e.g. `foo(readd(subset))`? – Mark Payne Sep 02 '20 at 14:27
  • 1
    `diagnose(row)$subtargets` lists the sub-targets of `row` in order. Once you verified that `row_f9171bbe` is the fourth sub-target, you can take the corresponding slice of `subset` using `vctrs::vec_slice(readd(subset), 4)`. – landau Sep 02 '20 at 14:56
  • does {targets} make this somewhat easier? – elikesprogramming Sep 27 '20 at 18:56
  • For debugging purposes, yes: https://wlandau.github.io/targets-manual/practice.html#debugging – landau Sep 27 '20 at 19:23