[R] load() into a data frame with my chosen name => .ThisEnv ?
William Dunlap
wdunlap at tibco.com
Tue Jun 18 01:23:22 CEST 2013
> 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.
More information about the R-help
mailing list