如何在不合并数据集的情况下将多个Drake目标合并为一个交叉目标?

时间:2019-09-05 10:50:59

标签: r drake-r-package

德雷克岩石!我有一个复杂的多阶段处理问题。这个例子可以说明这个问题。我在级别1有2个进程,我希望级别1的 all 生成的所有数据集都由级别2的单个目标处理。

下面的代码可以满足我的要求,但是我必须在2级重复该代码,在我的复杂情况下,这似乎是错误的。



library(drake)
library(tidyverse)

f_process1a = function(x) {
  x
}


f_process1b = function(x) {
  x
}


f_process2 = function(data, x) {
  c(data, x )
}


drakeplan <- drake::drake_plan(
  process1a = target(
                    f_process1a (process1a_var),
                    transform = map( process1a_var = c(1,2) )
  )
  ,
  #
  process1b = target(
                    f_process1b ( process1b_var),
                    transform = map(process1b_var = c(2,3) )
  )
  ,
  process2a = target(
                    f_process2( process1a, process2_var ),
                    transform=cross( process1a,  process2_var = c(4,5))
  )
  ,
  process2b = target(
                    f_process2( process1b, process2_var ),
                    transform=cross( process1b,  process2_var = c(4,5))
  )
)


drake_plan_source(drakeplan )
#> drake_plan(
#>   process1a_1 = f_process1a(1),
#>   process1a_2 = f_process1a(2),
#>   process1b_2 = f_process1b(2),
#>   process1b_3 = f_process1b(3),
#>   process2a_4_process1a_1 = f_process2(process1a_1, 4),
#>   process2a_5_process1a_1 = f_process2(process1a_1, 5),
#>   process2a_4_process1a_2 = f_process2(process1a_2, 4),
#>   process2a_5_process1a_2 = f_process2(process1a_2, 5),
#>   process2b_4_process1b_2 = f_process2(process1b_2, 4),
#>   process2b_5_process1b_2 = f_process2(process1b_2, 5),
#>   process2b_4_process1b_3 = f_process2(process1b_3, 4),
#>   process2b_5_process1b_3 = f_process2(process1b_3, 5)
#> )


reprex package(v0.3.0)于2019-09-05创建

graph

我想将process2 [ab]步骤合并为一个目标。这可能吗?

看来我应该可以有一个目标,例如:


  process2 = target(
                    f_process2( data, process2_var ),
                    transform=cross( data=c(process1a, process2a),
                                      process2_var = c(4,5))
  )

但这不起作用。

1 个答案:

答案 0 :(得分:1)

一种紧凑的解决方案是向.data提供自定义的map()网格。

library(drake)
library(rlang)
library(tidyverse)

grid <- tibble(
  fun1 = syms(c("f1a", "f1a", "f1b", "f1b")),
  var1 = c(1, 2, 2, 3)
)

plan <- drake_plan(
  x = target(
    fun1(var1),
    transform = map(.data = !!grid)
  ),
  y = target(
    f2(x, var2),
    transform = cross(x, var2 = c(4, 5))
  )
)

config <- drake_config(plan)
vis_drake_graph(config)

reprex package(v0.3.0)于2019-09-05创建

但是我可能在这里过度适合您的示例。另一种方法是使用标签。每个转换都可以理解arguments .tag_in and .tag_out。在这里,.tag_out可以定义一个总体分组变量,以同时覆盖process1a_*process1b_*目标。然后,您可以在处理cross()时将该分组变量传递给process2

library(drake)

plan <- drake_plan(
  process1a = target(
    f_process1a(process1a_var),
    transform = map(process1a_var = c(1, 2), .tag_out = process1)
  ),
  process1b = target(
    f_process1b(process1b_var),
    transform = map(process1b_var = c(2, 3), .tag_out = process1)
  ),
  process2 = target(
    f_process2(process1, process2_var),
    transform = cross(process1, process2_var = c(4, 5))
  ),
  trace = TRUE
)

config <- drake_config(plan)
vis_drake_graph(config)

reprex package(v0.3.0)于2019-09-05创建