How to relate a drake dynamic subtarget to the actual dataframe / source target?

94 views Asked by At

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

1

There are 1 answers

4
landau On

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.