I am using drake
to create multiple output files, where I want to specify the path by a variable. Something like
outpath <- "data"
outfile <- file.path(outpath, "mydata.csv")
write.csv(df, outfile)
But file_out
doesn't seem to work with arguments given to it other than literal characters.
To give a small code example:
Code setup
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))
Working Code
directly specifying the file:
p0 <- drake_plan(
df = make_data(),
write.csv(df, file_out("data/mydata0.csv"))
)
make(p0)
#> target file "data/mydata0.csv"
Failing Code
using file.path
to construct the 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.
I guess drake finds only the literal string as a target and not the result of file.path(...)
, for example, this fails as well
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))
Any idea how to fix that?