我正在使用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
构造输出文件
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.
我猜德雷克只找到文字字符串作为目标,而不是结果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))
知道如何解决吗?