[R] Sourcing data files into different array fields

Gabor Grothendieck ggrothendieck at gmail.com
Sun Apr 2 03:23:07 CEST 2006

Use the local= argument of source.

Here we createa a list, L, whose ith component is an environment,
viz. the environment of the instance of f that did the sourcing,
containing the sourced objects.  First we create some test
files: /a1 and /a2 and then we create L.  The components
of L can be referred to as L[[1]], L[[2]] or using filenames
L[["a1"]], L[["a2"]] and the components can be extracted
via L[["a1"]]$a, etc.

# create test files
files <- paste("/a", 1:2, sep = "")
for(i in seq(files)) cat("a <-", i, "\n", file = files[i])

# read them back
f <- function(.x) { source(.x, local = TRUE); environment() }
L <- sapply(files, f, simplify = FALSE)
ls(L[[1]]) # "a"
L[[1]]$a # 1
L[["/a2"]][["a"]] # 2

On 4/1/06, Werner Wernersen <pensterfuzzer at yahoo.de> wrote:
> Hi,
> I make a number of experiments with a software which
> spits out one .R file of data objects to be sourced
> for each experiment. Now, the files repeat the data
> object names but ultimately I also want to perform
> comparision statistics between several objects.
> Now my question is, is there a slick way to "wrap"
> those data objects while sourcing each experiment file
> so that for instance I get an array m[] for each
> experiment and can access the objects by using
> m[1].object1 and so on?
> I am thankful for any suggestion!
>  Werner
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list