From hb at stat.berkeley.edu  Sat Aug 23 01:30:05 2008
From: hb at stat.berkeley.edu (Henrik Bengtsson)
Date: Fri, 22 Aug 2008 16:30:05 -0700
Subject: [Rd] save() should not overwrite a file if an error occurs
	(PR#12583)
In-Reply-To: <20080822224508.CD3C8282C763@mail.pubhealth.ku.dk>
References: <20080822224508.CD3C8282C763@mail.pubhealth.ku.dk>
Message-ID: <59d7961d0808221630p3180c974ie0ffac4f7e388e9a@mail.gmail.com>

I don't know what save() "should" do - your use case is quite special
- but I agree it would be better if save() tests for the existence of
all object(s) to be saved before opening the connection (and thereby
overwrite the existing file).  A workaround for you is to do:

dummy <- a;
save(a, file="a.rda");

This should give an error before save() is called.

FYI, saveObject() of R.utils does protect you this way too, e.g.

> library("R.utils");
> a <- 1:9
> saveObject(a, file="a.rda")
> rm(a)
> a <- loadObject(file="a.rda")
> a
[1] 1 2 3 4 5 6 7 8 9
> rm(a)
> saveObject(a, file="a.rda")
Error in saveObject(a, file = "a.rda") : object "a" not found
> a <- loadObject(file="a.rda")
> a
[1] 1 2 3 4 5 6 7 8 9

/Henrik

On Fri, Aug 22, 2008 at 3:45 PM,  <timhesterberg at gmail.com> wrote:
> If save() fails because an object is not found,
> it should not overwrite an existing file.
>
>> a <- 1:9
>> save(a, file = "a.rda")
>> rm(a)
>> load("a.rda")
>> a
>  [1]  1  2  3  4  5  6  7  8  9
>> rm(a)
>> save(a, file = "a.rda")
> Error in save(a, file = "a.rda") : object 'a' not found
>> load("a.rda")
> Error in load("a.rda") : error reading from connection
>
> I've been saving copies of huge objects into
> files like a.rda, then removing the objects.
> If I then happen to save again, I don't want to lose
> my data.
>
>
> --please do not edit the information below--
>
> Version:
>  platform = i486-pc-linux-gnu
>  arch = i486
>  os = linux-gnu
>  system = i486, linux-gnu
>  status =
>  major = 2
>  minor = 7.1
>  year = 2008
>  month = 06
>  day = 23
>  svn rev = 45970
>  language = R
>  version.string = R version 2.7.1 (2008-06-23)
>
> Locale:
> LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C
>
> Search Path:
>  .GlobalEnv, package:Rfixes, package:Rcode, package:aggregate,
> package:stats, package:graphics, package:grDevices, package:utils,
> package:datasets, package:showStructure, package:splus2R, package:methods,
> Autoloads, package:base
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] save() should not overwrite a file if an error occurs (PR#12583)
Henrik Bengtsson