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]]
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. >
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!You can load() each to a new enviroment: see the arguments of load().> > -- 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
>>>>> "TK" == Talbot Katz <topkatz at msn.com> >>>>> on Mon, 10 Dec 2007 14:16:52 -0500 writes:TK> Hi. TK> I have a procedure that reads a directory, loops through TK> a set of particular .RData files, loading each one, and TK> feeding its object(s) into a function, as follows: TK> cvListFiles<-list.files(fnDir); TK> for(i in grep(paste("^",pfnStub,".*\\.RData$",sep=""),cvListFiles)){ TK> load(paste(fnDir,cvListFiles[i],sep="/")); TK> myFunction(rliObject); TK> rm(rliObject); TK> }; [.............] I don't know where you got the idea to end R statements with ";". I find it atrocious. It just does not belong to the S language and its R implementation. Please don't do it -- it hurts the eyes of most experienced R users -- it's extra clutter -- internally each extra ";" is an extra empty statement OTOH, *please* do -- indent R code and -- use spaces to increase readability This would make your above code into something like cvListFiles <- list.files(fnDir) for(i in grep(paste("^", pfnStub,".*\\.RData$", sep = ""), cvListFiles)) { load(paste(fnDir, cvListFiles[i], sep = "/")) myFunction(rliObject) rm(rliObject) } Something many readers on this list will much prefer to your original, and hence start considering to look at .. Martin Maechler, (S user since ca. 1987; member of R-core)
Hi, depending on what you do and how (and why) you save objects in RData files in the first place, you might be interested in knowing of the loadObject()/saveObject() methods of R.utils, as well as loadCache()/saveCache() in R.cache. The R.utils methods are basically "clever" wrappers around load()/save() in the 'base' package that does not rely on saving and loading the variable name but rather the object. To save multiple objects you have wrap them up in a list structure or in an environment. Example: x <- 1:100 saveObject(x, file="foo.RData") y <- loadObject("foo.RData") stopifnot(identical(x,y)) u <- list(x=x, y=y) saveObject(u, file="bar.RData") v <- loadObject("bar.RData") stopifnot(identical(u,v)) The R.cache methods let you store objects/results to a file cache without having to worry about filenames. Instead the objects are identified by lookup keys generated from other R objects. This is useful for temporary/semi-temporary storing of results, especially computationally expensive results. The file cache is persistent between sessions. Example: x <- 1:100 key <- list("x") saveCache(x, key=key) y <- loadCache(key) stopifnot(identical(x,y)) u <- list(x=x, y=y) key <- list("u") saveCache(u, key=key) v <- loadCache(key) stopifnot(identical(u,v)) Although not of immediate interest, the pathname of the above cache files can be found by findCache(key), e.g. "~/.Rcache/78488a47006df5d333db9e200fc539c5.Rcache". There are methods for specifying the root of the file cache, and having different subdirectories for different projects. The above example is not showing the full power of using R.cache. Instead consider this example: slowFcn <- function(x, y, force=FALSE) { # Cached results? key <- list(x=x, y=y) if (!force) { res <- loadCache(key=key) if (!is.null(res)) return(res); } # Emulate a computational expensive calculation Sys.sleep(10) res <- list(x=x, y=y, xy=x*y) # Save to cache saveCache(res, key=key) res } # First call takes time> system.time(res1 <- slowFcn(x=1, y=2))user system elapsed 0 0 10 # All successive calls with the same arguments are instant> system.time(res2 <- slowFcn(x=1, y=2))user system elapsed 0.02 0.00 0.01> stopifnot(identical(res1, res2))Cheers Henrik On 10/12/2007, Talbot Katz <topkatz at msn.com> 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. >