[R] save an object by dynamicly created name

Henrik Bengtsson hb at stat.berkeley.edu
Mon Nov 2 05:28:07 CET 2009


On Sun, Nov 1, 2009 at 7:48 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Nov 1, 2009, at 10:16 PM, Henrik Bengtsson wrote:
>
>> path <- "data";
>> dir.create(path);
>>
>> for (i in 1:10) {
>>  m <- i:5;
>>  filename <- sprintf("m%02d.Rbin", i);
>>  pathname <- file.path(path, filename);
>>  save(m, file=pathname);
>> }
>>
>
> That would result in each of the ten files containing an object with the
> same  name == "m". (Also on my system R data files have type Rdta.) So I
> thought what was requested might have been a slight mod:
>
> path <- "~/";
> dir.create(path);
>
> for (i in 1:10) {
>  assign( paste("m", i, sep=""),  i:5)
>  filename <- sprintf("m%02d.Rdta", i)
>  pathname <- file.path(path, filename)
>  obj =get(paste("m", i, sep=""))
>  save(obj, file=pathname)
> }

Then a more convenient solution is to use saveObject() and
loadObject() of R.utils.  saveObject() does not save the name of the
object save.  If you want to save multiple objects, the wrap them up
in a list.  loadObject() does not assign variable, but instead return
them. Example:

library("R.utils");
x <- list(a=1,b=LETTERS,c=Sys.time());
saveObject(x, file="foo.Rbin");
y <- loadObject("foo.Rbin");
stopifnot(identical(x,y));

So, for the original example, I'd recommend:

library("R.utils");
path <- "data";
mkdirs(path);

for (i in 1:10) {
  m <- i:5;
  filename <- sprintf("m%02d.Rbin", i);
  saveObject(m, file=filename, path=path);
}

and loading the objects back as:

for (i in 1:10) {
  filename <- sprintf("m%02d.Rbin", i);
  m <- loadObject(filename, path=path);
  print(m);
}

/Henrik

>
> --
> David.
>
>> /H
>>
>> On Sun, Nov 1, 2009 at 6:53 PM, jeffc <hcen at andrew.cmu.edu> wrote:
>>>
>>> Hi,
>>>
>>> I would like to save a few dynamically created objects to disk. The
>>> following is the basic flow of the code segment
>>>
>>> for(i = 1:10) {
>>>  m = i:5
>>>  save(m, file = ...) ## ???
>>> }
>>> To distinguish different objects to be saved, I would like to save m as
>>> m1,
>>> m2, m3 ..., to file /home/data/m1, /home/data/m2, home/data/m3, ...
>>>
>>> I tried a couple of methods on translating between object names and
>>> strings
>>> (below) but couldn't get it to work.
>>> https://stat.ethz.ch/pipermail/r-help/2008-November/178965.html
>>> http://tolstoy.newcastle.edu.au/R/help/04/08/2673.html
>>>
>>> Any suggestions would be appreciated.
>>>
>>> thanks
>>>
>>> Hao
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/save-an-object-by-dynamicly-created-name-tp26155437p26155437.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list