0

我想使用德雷克的动态目标来读取多个文件。我根据对动态文件工作原理的理解编写了以下计划。但是,当输入文件更改时,drake 不会正确更新所有目标。

使用drake的动态文件读取文件的正确方法是什么?

换句话说,file_in()解决这个问题的动态文件版本是什么:How can I import from multiple files in r-drake?

library(drake)
library(tidyverse)

content <- tibble(x1 = 1, x2 = 1)
walk(list("a", "b"), ~ write_csv(x = content, path = paste0(., ".csv")))
read_csv("b.csv", col_types = "dd")
#> # A tibble: 1 x 2
#>      x1    x2
#>   <dbl> <dbl>
#> 1     1     1

plan <- drake::drake_plan(
  import_paths = target(c(
    a = "a.csv",
    b = "b.csv"
  ),
  format = "file"
  ),

  data = target(
    read_csv(import_paths, col_types = "dd"),
    dynamic = map(import_paths)
  )
)

drake::make(plan)
#> ▶ target import_paths
#> ▶ dynamic data
#> > subtarget data_44119303
#> > subtarget data_ecc6ebe6
#> ■ finalize data
readd(data)
#> # A tibble: 2 x 2
#>      x1    x2
#>   <dbl> <dbl>
#> 1     1     1
#> 2     1     1

walk(list("b"), ~ write_csv(x = content + 1, path = paste0(., ".csv")))
read_csv("b.csv", col_types = "dd")
#> # A tibble: 1 x 2
#>      x1    x2
#>   <dbl> <dbl>
#> 1     2     2

drake::make(plan)
#> ▶ target import_paths
#> ■ finalize data
readd(data)
#> # A tibble: 2 x 2
#>      x1    x2
#>   <dbl> <dbl>
#> 1     1     1
#> 2     1     1

reprex 包(v0.3.0)于 2020-08-06 创建

4

1 回答 1

2

也许这并不明显,但动态文件目标是不可约的。如果c("a.csv", "b.csv")是您的动态文件,则不能将其分解为"a.csv"" b.csv". drake将所有这些文件的全局哈希存储在一起,并且它不会逐个文件跟踪哈希或时间戳。drake即使您从单个目标返回大量动态文件,这也有助于保持效率。

解决方案是使用"a.csv"动态. 您在开始时需要一个额外的目标来包含路径名,但它可以完成工作。"b.csv"map()

library(drake)
library(tidyverse)

content <- tibble(x1 = 1, x2 = 1)
walk(list("a", "b"), ~ write_csv(x = content, path = paste0(., ".csv")))
read_csv("b.csv", col_types = "dd")
#> # A tibble: 1 x 2
#>      x1    x2
#>   <dbl> <dbl>
#> 1     1     1

plan <- drake_plan(
  import_paths = c("a.csv", "b.csv"),
  import_files = target(
    import_paths,
    format = "file",
    dynamic = map(import_paths)
  ),
  data = target(
    read_csv(import_files, col_types = "dd"),
    dynamic = map(import_files)
  )
)

make(plan)
#> ▶ target import_paths
#> ▶ dynamic import_files
#> > subtarget import_files_4209ea92
#> > subtarget import_files_b8419eb2
#> ■ finalize import_files
#> ▶ dynamic data
#> > subtarget data_b59aea49
#> > subtarget data_e6b8ef3e
#> ■ finalize data

readd(data)
#> # A tibble: 2 x 2
#>      x1    x2
#>   <dbl> <dbl>
#> 1     1     1
#> 2     1     1

walk(list("b"), ~ write_csv(x = content + 1, path = paste0(., ".csv")))
read_csv("b.csv", col_types = "dd")
#> # A tibble: 1 x 2
#>      x1    x2
#>   <dbl> <dbl>
#> 1     2     2

make(plan)
#> ▶ dynamic import_files
#> > subtarget import_files_b8419eb2
#> ■ finalize import_files
#> ▶ dynamic data
#> > subtarget data_a0f1c4f0
#> ■ finalize data

readd(data)
#> # A tibble: 2 x 2
#>      x1    x2
#>   <dbl> <dbl>
#> 1     1     1
#> 2     2     2

reprex 包(v0.3.0)于 2020-08-06 创建

于 2020-08-07T00:31:28.230 回答