0

我想为我的德雷克计划使用动态文件。我遵循了德雷克关于动态文件的文档

但是,drake 似乎没有将创建的文件路径合并到其缓存中。

在下面的代表中,plan_a遵循文档,但最终重建目标figure_export_path,因此figure_export每次运行。plan_b在第二次运行时不会重建任何东西,但它的代码与德雷克的文档不一致。

在像我这样的情况下使用动态文件的正确方法是什么?

library(drake)
library(tidyverse)

foo <- function(bar) {
  y <- pmax(1:3, bar)
  qplot(x = 1:3, y = y, geom = "point")
}

plan_a <- drake::drake_plan(
  parameter = 1:3,

  figure = target(
    foo(parameter),
    dynamic = map(parameter)
  ),

  figure_export_path = target(
    paste(parameter, 'pdf', sep = '.'),
    dynamic = map(parameter),
    format = "file" # This line seems to invalidate the cache
  ),
  
  figure_export = target(
    ggsave(figure_export_path, figure),
    dynamic = map(figure_export_path, figure)
  )
)

plan_b <- drake::drake_plan(
  parameter = 1:3,
  
  figure = target(
    foo(parameter),
    dynamic = map(parameter)
  ),
  
  figure_export_path = target(
    paste(parameter, 'pdf', sep = '.'),
    dynamic = map(parameter),
    # format = "file" # This line seems to invalidate the cache
  ),
  
  figure_export = target(
    ggsave(figure_export_path, figure),
    dynamic = map(figure_export_path, figure)
  )
)

rerun(2, drake::make(plan = plan_a))
#> ▶ target parameter
#> ▶ dynamic figure
#> > subtarget figure_0b3474bd
#> > subtarget figure_b2a5c9b8
#> > subtarget figure_71f311ad
#> ■ finalize figure
#> ▶ dynamic figure_export_path
#> > subtarget figure_export_path_0b3474bd
#> Warning: missing dynamic files for target figure_export_path_0b3474bd:
#>   1.pdf
#> > subtarget figure_export_path_b2a5c9b8
#> Warning: missing dynamic files for target figure_export_path_b2a5c9b8:
#>   2.pdf
#> > subtarget figure_export_path_71f311ad
#> Warning: missing dynamic files for target figure_export_path_71f311ad:
#>   3.pdf
#> ■ finalize figure_export_path
#> ▶ dynamic figure_export
#> > subtarget figure_export_25d05a3f
#> Saving 7 x 5 in image
#> > subtarget figure_export_4e67ff77
#> Saving 7 x 5 in image
#> > subtarget figure_export_5ac7bd49
#> Saving 7 x 5 in image
#> ■ finalize figure_export
#> ▶ dynamic figure_export_path
#> > subtarget figure_export_path_0b3474bd
#> > subtarget figure_export_path_b2a5c9b8
#> > subtarget figure_export_path_71f311ad
#> ■ finalize figure_export_path
#> ▶ dynamic figure_export
#> > subtarget figure_export_e284629c
#> Saving 7 x 5 in image
#> > subtarget figure_export_7278fc00
#> Saving 7 x 5 in image
#> > subtarget figure_export_72064f81
#> Saving 7 x 5 in image
#> ■ finalize figure_export
#> [[1]]
#> NULL
rerun(2, drake::make(plan = plan_b))
#> ▶ dynamic figure_export_path
#> > subtarget figure_export_path_0b3474bd
#> > subtarget figure_export_path_b2a5c9b8
#> > subtarget figure_export_path_71f311ad
#> ■ finalize figure_export_path
#> ▶ dynamic figure_export
#> > subtarget figure_export_5a8b1281
#> Saving 7 x 5 in image
#> > subtarget figure_export_05ed8067
#> Saving 7 x 5 in image
#> > subtarget figure_export_3c33e0b9
#> Saving 7 x 5 in image
#> ■ finalize figure_export
#> ✓ All targets are already up to date.
#> [[1]]
#> NULL

reprex 包(v0.3.0)于 2020 年 8 月 3 日创建

4

0 回答 0