Skip to content

Serious bug in save.image

2 messages · Martyn Plummer, Peter Dalgaard

#
I am using a  package which tends to crash R, so I have been saving
my work at regular intervals with "save.image()". Unfortunately, this
overwrites .RData with an empty workspace, so when R crashed I lost
my workspace and a day's work.

The problem is easily fixed: "save.image" looks like this

function () 
save(list = ls(), file = ".RData")

The expression "ls()" is evaluated in the calling environment, which
is empty. You need to evaluate it in the global environment, like this:

function () 
save(list = eval(expression(ls()), .GlobalEnv), file = ".RData")

Martyn
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Martyn Plummer <plummer@iarc.fr> writes:
Ouch! Fixed in my sources. Will get to the prerelease versions after I
squash an elusive memory corruption bug.

Actually, I did you one better:
function (f = ".RData") 
eval(substitute(save(list = ls(), file = f)), .GlobalEnv)

so that you can now save the image under a different name.