I am trying to override save()in R so that it creates all the missing directories before saving the object. I am having trouble passing an object through a single function using the ellipsis method.
My example:
save <- function(...,file){
target.dir <- dirname(file)
if(!file.exists(target.dir)) {
dir.create(target.dir,showWarnings=T,recursive=T)
}
base::save(...,file=file.path(target.dir,basename(file)))
}
fun1 <- function(obj) {
obj1 <- obj + 1
save(obj1,file="~/test/obj.RData")
}
fun1(obj = 1)
The above code results in an error:
Error in base::save(..., file = file.path(target.dir, basename(file))) :
object βobj1β not found
I understand that the problem is that the obj1 object does not exist inside my user-defined save () function, but I still have not figured out how to pass it from fun1 to base :: save.
I tried:
base::save(parent.frame()$...,file=file.path(target.dir,basename(file)))
and
base::save(list=list(...),file=file.path(target.dir,basename(file)))
without success.
Any suggestions?
source
share