簡體   English   中英

R drake文件輸出帶變量的名稱

[英]R drake file out name with variable

我正在使用drake創建多個輸出文件,我想通過變量指定路徑。 就像是

outpath <- "data"
outfile <- file.path(outpath, "mydata.csv")
write.csv(df, outfile)

但是file_out似乎不適用於除了文字字符之外的賦予它的參數。

給出一個小代碼示例:

代碼設置

library(drake)

outpath <- "data"
# for reproducibility only
if (!dir.exists(outpath)) dir.create(outpath)

make_data <- function() data.frame(x = 1:10, y = rnorm(10))

工作守則

直接指定文件:

p0 <- drake_plan(
  df = make_data(),
  write.csv(df, file_out("data/mydata0.csv"))
)
make(p0)
#> target file "data/mydata0.csv"

失敗的代碼

使用file.path構造outfile

p1 <- drake_plan(
  df = make_data(),
  write.csv(df, file_out(file.path(outpath, "mydata1.csv")))
)
make(p1)
#> target file "mydata1.csv"
#> Error: The file does not exist: mydata1.csv
#> In addition: Warning message:
#> File "mydata1.csv" was built or processed,
#> but the file itself does not exist. 

我猜drake只找到文字字符串作為目標,而不是file.path(...)的結果,例如,這也失敗了

p2 <- drake_plan(
  df = make_data(),
  outfile = file.path(outpath, "mydata1.csv"),
  write.csv(df, file_out(outfile))
)
#> Error: found an empty file_out() in command: write.csv(df, file_out(outfile))

知道怎么解決這個問題嗎?

對不起,我這個帖子太晚了。 我可以使用drake-r-package標簽更輕松地找到問題。

感謝@Alexis提供相關主題的鏈接。 通配符在這里真的很有幫助。

您需要事先明確指定所有目標,輸入文件和輸出文件。 這樣, drake可以在不評估計划中的任何代碼的情況下找出所有依賴關系。 由於drake負責確定何時建立目標,我可能不會在未來的發展中放松這一要求。

對於它的價值,整潔的評估也可能有所幫助。

library(drake) # version 5.3.0
pkgconfig::set_config("drake::strings_in_dots" = "literals")
file <- file.path("dir", "mydata1.csv")
drake_plan(
  df = make_data(),
  output = write.csv(df, file_out(!!file))
)
#> # A tibble: 2 x 2
#>   target         command                                       
#> * <chr>          <chr>                                         
#> 1 df             make_data()                                   
#> 2 output         "write.csv(df, file_out(\"dir/mydata1.csv\"))"

編輯:元編程

我最近在元編程手冊中添加了一個冗長的部分 如果您想要更靈活和自動化的方式來生成工作流計划數據框,您可能不得不放棄drake_plan()函數並進行更多涉及整潔的評估。 關於問題跟蹤器討論也是相關的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM