개체를 저장하기 전에 누락 된 디렉터리를 만들 수 있도록 R에서 save()
을 무시하려고합니다. 하나의 함수를 통해 다른 객체로 줄임표를 사용하여 객체를 전달하는 데 문제가 있습니다.중첩 함수에서 객체를 전달하는 방법은 무엇입니까?
내 예 :
save <- function(...,file){ #Overridden save()
target.dir <- dirname(file) #Extract the target directory
if(!file.exists(target.dir)) {
#Create the target directory if it doesn't exist.
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)
이 오류가 발생 위의 코드 :
Error in base::save(..., file = file.path(target.dir, basename(file))) : object ‘obj1’ not found
내가 문제가 오브젝트 '으로 obj1'저장 내 사용자 정의 내부에 존재하지 않는 것을 실현 () 함수를 사용하지만 fun1에서 base :: save로 전달하는 방법을 아직 찾지 못했습니다.
이 나는 시도했다 :
base::save(parent.frame()$...,file=file.path(target.dir,basename(file)))
과 :
base::save(list=list(...),file=file.path(target.dir,basename(file)))
을 성공하지.
제안 사항? 매개 변수가 전화를 저장 ::베이스에 추가
save <- function(...,file){ #Overridden save()
target.dir <- dirname(file) #Extract the target directory
if(!file.exists(target.dir)) {
#Create the target directory if it doesn't exist.
dir.create(target.dir,showWarnings=T,recursive=T)
}
base::save(...,file=file.path(target.dir,basename(file)),envir=parent.frame())
}
참고 :
수정 base.name 위의 코드베이스 이름. Matthew에게 감사드립니다. –