[R] use object within rda file in for loop

Henrik Bengtsson hb at stat.berkeley.edu
Thu May 20 15:50:21 CEST 2010


On Thu, May 20, 2010 at 3:06 PM, Ivan Calandra
<ivan.calandra at uni-hamburg.de> wrote:
> I've found the answer:
> get() is exactly what I need, I completely forgot about this function.
>
> There might be a better way, but that works for me.

I R.utils, there are saveObject() and loadObject(), which allows you
to save and load objects without having to worry about their names.
For example:

library("R.utils");
x <- list(a=2, b=3:40, fcn=rnorm);
saveObject(x, "objs.Rbin");

Then later you can load the list object that 'x' held as whatever you
want, say, 'y', e.g.

library("R.utils");
y <- loadObject("objs.Rbin");

This way you don't "contaminate" the working environment with
variables named according to the file, which sometimes can be a
surprise (since you don't always know what the file contain).  Except
from that it works just like save()/load().

/Henrik

> Ivan
>
> Le 5/20/2010 14:13, Ivan Calandra a écrit :
>>
>> Dear users,
>>
>> I would like to process all the lists from all *.rda files that I have in
>> one folder.
>> Up to now, I can load all the *.rda files without any problem.
>>
>> The problem is when I want to access the list saved within each *.rda file
>> (only one list per rda file).
>>
>> Here is my code:
>> fpath <- "D:/R"
>> listnames <- list.files(path=fpath, pattern=glob2rx("*.rda"),
>> full.names=FALSE)
>> for (i in 1:length(listnames)) {
>>  load(paste(fpath, listnames[i], sep="/"))
>>  z <- list_in_listnames[i]  ## here is my problem
>>  **do something on z**
>> }
>>
>> It might be really simple, but listnames is a character vector and I
>> cannot find how to store the values within each element into z.
>> I think there would be a function to get and use an object with a given
>> pattern in its name. The pattern itself is no problem, the problem is the
>> function.
>> I've tried to look on RSiteSearch but, probably because I couldn't figure
>> out the keywords, haven't found anything helpful.
>> Maybe I'm just on the wrong path to do it or missed something obvious...
>>
>> I hope my question is clear. If not, please let me know what would help
>> you to understand.
>>
>> Thanks in advance
>> Ivan
>>
>
> --
> Ivan CALANDRA
> PhD Student
> University of Hamburg
> Biozentrum Grindel und Zoologisches Museum
> Abt. Säugetiere
> Martin-Luther-King-Platz 3
> D-20146 Hamburg, GERMANY
> +49(0)40 42838 6231
> ivan.calandra at uni-hamburg.de
>
> **********
> http://www.for771.uni-bonn.de
> http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>
> ______________________________________________
> 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