Fernando Cagua
Fernando Cagua

Reputation: 125

How to generate arguments to a target transformation dynamically in R drake?

I want to generate a large plan whose arguments depend on previously computed targets. Is that possible at all?

Specifically, I need something along the lines of:

drake_plan(
  data = get_data(),
  lots_of_sds = get_sds_from_a_complex_pipeline()
  analysis = target(
    fun(data, mean = mean_val, sd = sd_val),
    transform = cross(mean_val = c(2, 5), sd_val = !!lots_of_sds)
  )
)

The problem is that trying this (or similar variations) fails because lots_of_sds has not been defined and as such the transformation cannot expand the plan.

Has anyone faced a similar situation in the past? Any ideas/workarounds?

Thanks! I'm using drake 7.0.0 and R version 3.5.3

Upvotes: 0

Views: 166

Answers (1)

landau
landau

Reputation: 5841

You are almost there. All you need to do is define lots_of_sds beforehand outside drake_plan(), which is standard procedure when you use !!.

library(drake)

lots_of_sds <- c(1, 2)

drake_plan(
  data = get_data(),
  analysis = target(
    fun(data, mean = mean_val, sd = sd_val),
    transform = cross(mean_val = c(2, 5), sd_val = !!lots_of_sds)
  )
)
#> # A tibble: 5 x 2
#>   target       command                    
#>   <chr>        <expr>                     
#> 1 data         get_data()                 
#> 2 analysis_2_1 fun(data, mean = 2, sd = 1)
#> 3 analysis_5_1 fun(data, mean = 5, sd = 1)
#> 4 analysis_2_2 fun(data, mean = 2, sd = 2)
#> 5 analysis_5_2 fun(data, mean = 5, sd = 2)

Created on 2019-05-16 by the reprex package (v0.2.1)

The value of lots_of_sds needs to already exist before you run drake_plan() or make(). This limitation of drake will be difficult to overcome: https://github.com/ropensci/drake/issues/685.

Upvotes: 1

Related Questions