drake-r-package

Create groups of targets


Let's say that I have the following plan:

test_plan = drake_plan(
    foo = target(x + 1, transform = map(x = c(5, 10))),
    bar = 42
)

Now I want to create a new target that contains the two subtargets foo_5, foo_10 and the target bar. How can I do this? I feel it must be super simple but I don't manage to get a solution.

Thanks!


Solution

  • Yes, it is both possible and simple. The built-in solution is to use tags: https://books.ropensci.org/drake/static.html#tags. Example:

    library(drake)
    drake_plan(
      foo = target(
        x + 1,
        transform = map(x = c(5, 10), .tag_out = group)
      ),
      bar = target(
        42,
        # You need a transform to use a tag, even for 1 target.
        transform = map(tmp = 1, .tag_out = group)
      ),
      baz_map = target(group, transform = map(group)),
      baz_combine = target(c(group), transform = combine(group))
    )
    #> # A tibble: 7 x 2
    #>   target         command                
    #>   <chr>          <expr>                 
    #> 1 foo_5          5 + 1                  
    #> 2 foo_10         10 + 1                 
    #> 3 bar_1          42                     
    #> 4 baz_map_foo_5  foo_5                  
    #> 5 baz_map_foo_10 foo_10                 
    #> 6 baz_map_bar_1  bar_1                  
    #> 7 baz_combine    c(foo_5, foo_10, bar_1)
    

    Created on 2019-11-16 by the reprex package (v0.3.0)