[R] Reading through a group of .RData files
Benilton Carvalho
bcarvalh at jhsph.edu
Mon Dec 10 20:31:19 CET 2007
note that load() returns, invisibly, a string with the names of the
objects that were loaded. something in the lines of:
myObj <- load(file.path(fnDir, cvListFiles[i]))
myFunction(get(myObj))
rm(list=myObj)
might be closer to what you want.
moreover, if length(myObj) > 1, you might want sth like:
lapply(myObj, function(x) myFunction(get(x)))
instead...
best
b
On Mon, 10 Dec 2007, Talbot Katz wrote:
>
> Hi.
>
> I have a procedure that reads a directory, loops through a set of particular .RData files, loading each one, and feeding its object(s) into a function, as follows:
>
> cvListFiles<-list.files(fnDir);
> for(i in grep(paste("^",pfnStub,".*\\.RData$",sep=""),cvListFiles)){
> load(paste(fnDir,cvListFiles[i],sep="/"));
> myFunction(rliObject);
> rm(rliObject);
> };
>
> where fnDir is the directory I'm reading, and pfnStub is a string that begins the name of each of the files I want to load. As you can see, I'm assuming that each of the selected .RData files contains an object named "rliObject" and I'm hoping that nothing in any of the files I'm loading overwrites an object in my environment. I'd like to clean this up so that I can extract the object(s) from each data file, and feed them to my function, whatever their names are, without corrupting my environment. I'd appreciate any assistance. Thanks!
>
> -- TMK --212-460-5430 home917-656-5351 cell
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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