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

Hua Li hualihua at yahoo.com
Tue Jan 8 18:10:42 CET 2008


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.




More information about the R-help mailing list