Skip to content

load() into a data frame with my chosen name => .ThisEnv ?

3 messages · ivo welch, William Dunlap, Neal Fultz

#
Did you try using load's 'envir' argument, which should be mentioned in its help file?
E.g., define the function
   getMyData <- function(RDataFile) {
       # RDataFile should be *.RData file containing exactly one object
       .ThisEnv <- new.env(parent = emptyenv())
       loadedNames <- load(file=RDataFile, envir=.ThisEnv)
       stopifnot(length(loadedNames)==1)
       .ThisEnv[[ loadedNames ]]
   }
and use it in the loop as
  d <- getMyData(RDataFile = fname)
   

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
For your problem, you might want to use saveRDS and readRDS instead of
save and load. I would also use dir to get a list of files rather
than paste. 

Then your solution might look like:


run <- function(filename) {
    df <- readRDS(filename);
    coef( lm(df[,1] ~ df[,2]) ) 
}

results <- lapply( dir(pattern=glob2rx('d*.rds')), run)

If this is disk-bound, using multiple threads probably won't help much.
You'll have to try it out for yourself.

-nfultz
On Mon, Jun 17, 2013 at 04:07:50PM -0700, ivo welch wrote: