[R] use "save.image" inside a function?

From: Hua Li <hualihua_at_yahoo.com>
Date: Tue, 8 Jan 2008 09:10:42 -0800 (PST)


Hi, All

I'm trying to save the workspace as a Check Point for possible future come back. The "save.image" function works if the whole coding is in a main function:

rm(list=ls())
t = 1
t = t+1
print(paste("before",t))
# [1] "before 2"

save.image("tt.RData")  

t = t+1
print(paste("after",t))
# [1] "after 3"
 

load("tt.RData")
print(paste("check",t))
# [1] "check 2"

As you can see, the value right before I save the workspace is restored.

However, if I run the saving inside a function, it seems that it restores to the vale outside the function rather than the one before the save command:

rm(list=ls())
test.fcn <- function(t=1){

         t = t+1
         print(paste("before",t))
         save.image("tt.RData")

         t = t+1
         print(paste("after",t))
 
         load("tt.RData")
         print(paste("check",t))
 

}    

t = 1
test.fcn(t=t)
# [1] "before 2"
# [1] "after 3"
# [1] "check 1"

So it restores the value before the program enters the function (t=1), though what I really want is the value inside the function(t=2).

Is there anything wrong with the way I use the "save.image" function? Or is there other command I can use to reach my goal?

Thanks a lot!

Hua



Never miss a thing. Make Yahoo your home page.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Jan 2008 - 16:07:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jan 2008 - 17:30:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive