2

我想使用 drake 来审核数据框的一系列验证和清理步骤。我认为会有许多函数形成一个链,其中将传入一个数据帧,将进行验证或清理,并且(可能已清理的)数据帧将被传递到下一步。有没有办法创建一个函数调用链而不在计划中明确命名它们?

计划可能如下所示:

plan <- drake_plan(
    raw_data = load_data(),
    clean_data_1 = clean_step_1(raw_data, parms = "some parm"),
    clean_data_2 = clean_step_2(clean_data_1, parms = "some parm"),
    clean_data_3 = clean_step_3(clean_data_2, parms = "some parm"),
    ...
    clean_data_100 = clean_step_100(clean_data_99, parms = "some parm"),
)

有没有办法创建这个计划而不必想出中间名称clean_data_<n>,并让德雷克生成这些名称?按顺序保留配置文件或一些类似的清理步骤会很好,并且不必跟踪数据名称,以便它们可以按照它们在我的配置文件中出现的顺序进行组装。

4

2 回答 2

2

我在下面对@landau 的回答做了一些微调。它没有在不同的函数中进行拼接,我添加了一个部分,我在其中拼接了一个 params 参数,该参数也是动态的,但特定于每个函数。

# https://stackoverflow.com/q/58139703/1022967

library(drake)
library(rlang)
library(tibble)

functions <- syms(paste0("f", seq_len(4)))
index <- as.numeric(seq_len(4))
inputs <- syms(paste0("x_", index - 1))
#params = letters[1:4]
params = c('{"a":1, "b":"z"}', '{"a":2, "b":"z"}', '{"a":3, "b":"z"}', '{"a":4, "b":"z"}')

grid <- tibble(
  functions = functions,
  index = index,
  inputs = inputs,
  params = params
)

plan <- drake_plan(
  x = target(
    f(inputs, param = p),
    transform = map(.data = !!grid, .id = index, f = !!functions, p = !!params)
  )
)

plan
#> # A tibble: 4 x 2
#>   target command                                  
#>   <chr>  <expr>                                   
#> 1 x_1    f1(x_0, param = "{\"a\":1, \"b\":\"z\"}")
#> 2 x_2    f2(x_1, param = "{\"a\":2, \"b\":\"z\"}")
#> 3 x_3    f3(x_2, param = "{\"a\":3, \"b\":\"z\"}")
#> 4 x_4    f4(x_3, param = "{\"a\":4, \"b\":\"z\"}")

# config <- drake_config(plan)
# vis_drake_graph(config)

reprex 包(v0.3.0)于 2019 年 9 月 27 日创建

于 2019-09-27T21:21:38.620 回答
1

我可以想到几种不同的方式rlang::syms()drake_plan(). 第一:

library(drake)
library(rlang)

functions <- syms(paste0("f", seq_len(4)))
index <- as.numeric(seq_len(4))
inputs <- syms(paste0("x_", index - 1))

plan <- drake_plan(
  x = target(
    f(x, param = "some param"),
    transform = map(f = !!functions, x = !!inputs, id = !!index, .id = id)
  )
)

plan
#> # A tibble: 4 x 2
#>   target command                      
#>   <chr>  <expr>                       
#> 1 x_1    f1(x_0, param = "some param")
#> 2 x_2    f2(x_1, param = "some param")
#> 3 x_3    f3(x_2, param = "some param")
#> 4 x_4    f4(x_3, param = "some param")

config <- drake_config(plan)
vis_drake_graph(config)

reprex 包(v0.3.0)于 2019 年 9 月 27 日创建

第二个:

library(drake)
library(rlang)
library(tibble)

f <- syms(paste0("f", seq_len(4)))
index <- as.numeric(seq_len(4))
inputs <- syms(paste0("x_", index - 1))

grid <- tibble(
  f = f,
  index = index,
  inputs = inputs
)

plan <- drake_plan(
  x = target(
    f(inputs, param = "some param"),
    transform = map(.data = !!grid, .id = index)
  )
)

plan
#> # A tibble: 4 x 2
#>   target command                      
#>   <chr>  <expr>                       
#> 1 x_1    f1(x_0, param = "some param")
#> 2 x_2    f2(x_1, param = "some param")
#> 3 x_3    f3(x_2, param = "some param")
#> 4 x_4    f4(x_3, param = "some param")

config <- drake_config(plan)
vis_drake_graph(config)

reprex 包(v0.3.0)于 2019 年 9 月 27 日创建

于 2019-09-27T20:24:30.010 回答