ivo welch
2013-Jun-17 23:07 UTC
[R] load() into a data frame with my chosen name => .ThisEnv ?
very simple question. I probably forgot the answer (early stage onset), and I could not find it via google and r-help. I have a number of files, each containing one saved data frame. I want to do the equivalent of d <- .GlobalEnv[[ load(file="dxxxx.Rdata") ] but inside a function, and I don't need the original name (dxxxx). in the end, I want to do something like run <- function( i ) { fname <- paste0( "d", i, ".Rdata") stopifnot( length(fname)==1 ) d <- .ThisEnv[[ load(file=fname) ]] coef( lm(d[,1] ~ d[,2]) ) ## calculate two coefficients and return them } results <- mclapply( 1:10000, run ) stumped over something that should be easy...pointer appreciated. /iaw ---- Ivo Welch (ivo.welch@gmail.com) [[alternative HTML version deleted]]
William Dunlap
2013-Jun-17 23:23 UTC
[R] load() into a data frame with my chosen name => .ThisEnv ?
> very simple question. I probably forgot the answer (early stage onset), > and I could not find it via google and r-help. > > I have a number of files, each containing one saved data frame. I want to > do the equivalent of > > d <- .GlobalEnv[[ load(file="dxxxx.Rdata") ]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> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of ivo welch > Sent: Monday, June 17, 2013 4:08 PM > To: r-help > Subject: [R] load() into a data frame with my chosen name => .ThisEnv ? > > very simple question. I probably forgot the answer (early stage onset), > and I could not find it via google and r-help. > > I have a number of files, each containing one saved data frame. I want to > do the equivalent of > > d <- .GlobalEnv[[ load(file="dxxxx.Rdata") ] > > but inside a function, and I don't need the original name (dxxxx). in the > end, I want to do something like > > run <- function( i ) { > fname <- paste0( "d", i, ".Rdata") > stopifnot( length(fname)==1 ) > d <- .ThisEnv[[ load(file=fname) ]] > coef( lm(d[,1] ~ d[,2]) ) ## calculate two coefficients and return > them > } > > results <- mclapply( 1:10000, run ) > > stumped over something that should be easy...pointer appreciated. > > /iaw > ---- > Ivo Welch (ivo.welch at gmail.com) > > [[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.
Neal Fultz
2013-Jun-18 01:49 UTC
[R] load() into a data frame with my chosen name => .ThisEnv ?
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:> very simple question. I probably forgot the answer (early stage onset), > and I could not find it via google and r-help. > > I have a number of files, each containing one saved data frame. I want to > do the equivalent of > > d <- .GlobalEnv[[ load(file="dxxxx.Rdata") ] > > but inside a function, and I don't need the original name (dxxxx). in the > end, I want to do something like > > run <- function( i ) { > fname <- paste0( "d", i, ".Rdata") > stopifnot( length(fname)==1 ) > d <- .ThisEnv[[ load(file=fname) ]] > coef( lm(d[,1] ~ d[,2]) ) ## calculate two coefficients and return > them > } > > results <- mclapply( 1:10000, run ) > > stumped over something that should be easy...pointer appreciated. > > /iaw > ---- > Ivo Welch (ivo.welch at gmail.com) > > [[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.