【问题标题】:How to combine and filter dynamic file targets in R drake?如何在 R drake 中组合和过滤动态文件目标?
【发布时间】:2020-09-24 17:01:56
【问题描述】:

我在我的德雷克计划中创建了一组文件。我想将这些文件的一个子集复制到另一个位置。

以下代码几乎可以实现这一点。但是,在获取我要复制的文件目标的子集后,drake 对文件更改的依赖关系跟踪丢失了。

如何在不丢失 drake 依赖跟踪的情况下合并/子集动态文件目标?

copy_file <- function(file) {
  file_copy <- paste0(file, "_copy")
  file.copy(from = file, to = file_copy, overwrite = TRUE)
  file_copy
}

herb_1_a <- "parsley"
plan <- drake::drake_plan(
  file_1 = target(
    {
      writeLines(herb_1_a, "file_1_a") # Second run
      writeLines("sage", "file_1_b")
      c("file_1_a", "file_1_b")
    },
    format = "file"
  ),

  file_2 = target(
    {
      writeLines("rosemary", "file_2_a")
      writeLines("thyme", "file_2_b")
      c("file_2_a", "file_2_b")
    },
    format = "file"
  ),

  files_to_copy = str_subset(
    c(file_1, file_2),
    "_a$"
  ),

  file_copies = target(
    copy_file(files_to_copy),
    dynamic = map(files_to_copy),
    format = "file"
  )
)

drake::make(plan)
#> ▶ target file_2
#> ▶ target file_1
#> ▶ target files_to_copy
#> ▶ dynamic file_copies
#> > subtarget file_copies_5e57e9ee
#> > subtarget file_copies_ae26ecf9
#> ■ finalize file_copies
readLines("file_1_a")
#> [1] "parsley"
readLines("file_1_a_copy")
#> [1] "parsley"
herb_1_a <- 'banana'
drake::make(plan)
#> ▶ target file_1
#> ▶ target files_to_copy
readLines("file_1_a")
#> [1] "banana"
readLines("file_1_a_copy") # I want this banana
#> [1] "parsley"

reprex package (v0.3.0) 于 2020 年 9 月 24 日创建

【问题讨论】:

    标签: r drake-r-package


    【解决方案1】:

    我认为解决这个问题的方法是在复制步骤之前创建一组动态映射的动态输入文件。换句话说,files_to_copy 应该是动态文件的动态目标。草图:

    plan <- drake::drake_plan(
      file_1 = target(
        {
          writeLines(herb_1_a, "file_1_a") # Second run
          writeLines("sage", "file_1_b")
          c("file_1_a", "file_1_b")
        },
        format = "file"
      ),
      
      file_2 = target(
        {
          writeLines("rosemary", "file_2_a")
          writeLines("thyme", "file_2_b")
          c("file_2_a", "file_2_b")
        },
        format = "file"
      ),
      
      files_to_copy_group = str_subset(
        c(file_1, file_2),
        "_a$"
      ),
      
      files_to_copy = target(
        files_to_copy_group,
        dynamic = map(files_to_copy_group),
        format = "file"
      ),
      
      file_copies = target(
        copy_file(files_to_copy),
        dynamic = map(files_to_copy),
        format = "file"
      )
    )
    

    【讨论】:

    • 谢谢你,效果很好!我很惊讶它确实如此。 readd(files_to_copy_group) 的输出在第一个和第二个make(plan) 之间根本没有变化。如果files_to_copy 的唯一上游依赖项没有改变,德雷克怎么知道这两个文件之一改变了?使用format = "file" 创建目标是否意味着除了检查上游依赖项的任何更改之外,drake 还直接检查文件内容(或哈希)是否有更改?
    • 是的,完全正确。 format = "file" 告诉 drake 检查返回的文件路径的哈希值。
    猜你喜欢
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多