简体   繁体   中英

R: How can I save all objects within a functions local environment?

Is there a way to use save.image() so that it saves the objects within the environment of a function? Take this minimal example:

outside = "not in function"
testFun <- function(){
  a = 1
  b = 2
  c = 3
  save.image(file="environmentTest.Rdata")
}

testFun()

When I open environmentTest.Rdata, the object outside is there, but not a , b , or c . Is there a way to use save.image() (or another function) to save all the objects within the scope of the current function (without explicitly listing them)?

Use the indicated save command.

outside = "not in function"
testFun <- function(){
  a = 1
  b = 2
  c = 3
  save(list = ls(all.names = TRUE), file = "environment.RData")
}
testFun()

load("environment.RData", e <- new.env())
ls(e)
## [1] "a" "b" "c"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM