I'm trying to find a good way of attaching a list within a function such that the attached variables (the list's members) precede the global environment (.GlobalEnv) in the search list. Here is a non-working example using attach(), which hopefully explains better what I'm trying to do: > foo <- function(x=0, input=list(a=10)) { + attach(input) + on.exit(detach(input)) + print(search()) + print(a) + } > > a Error: Object "a" not found > foo() ## this prints out a = 10 [1] ".GlobalEnv" "input" "package:methods" "package:stats" [5] "package:graphics" "package:utils" "Autoloads" "package:base" [1] 10 > > a <- 0 > foo() ## this prints out a = 0, not a = 10 [1] ".GlobalEnv" "input" "package:methods" "package:stats" [5] "package:graphics" "package:utils" "Autoloads" "package:base" [1] 0 > Thanks, Gardar ___________________________________________________________________ Gardar Johannesson Lawrence Livermore National Laboratory 7000 East Avenue, L-229 Livermore, CA 94550 johannesson1@llnl.gov Tel: (925) 422-3901, Fax: (925) 422-4141 [[alternative HTML version deleted]]
Dear Gardar, You could use with(); to modify your example slightly,> a <- "a" > b <- 5 > foo <- function(x=0, input=list(a=10)) {+ with(input, { + print(c(a, b, x)) + }) + }> foo()[1] 10 5 0>I hope that this helps, John> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gardar > Johannesson > Sent: Wednesday, April 14, 2004 1:14 PM > To: r-help at stat.math.ethz.ch > Subject: [R] attaching data.frame/list within a function > > I'm trying to find a good way of attaching a list within a > function such that the attached variables (the list's > members) precede the global environment (.GlobalEnv) in the > search list. Here is a non-working example using attach(), > which hopefully explains better what I'm trying to do: > > > foo <- function(x=0, input=list(a=10)) { > + attach(input) > + on.exit(detach(input)) > + print(search()) > + print(a) > + } > > > > a > Error: Object "a" not found > > foo() ## this prints out a = 10 > [1] ".GlobalEnv" "input" "package:methods" > "package:stats" > [5] "package:graphics" "package:utils" "Autoloads" > "package:base" > [1] 10 > > > > a <- 0 > > foo() ## this prints out a = 0, not a = 10 > [1] ".GlobalEnv" "input" "package:methods" > "package:stats" > [5] "package:graphics" "package:utils" "Autoloads" > "package:base" > [1] 0 > > > > > Thanks, > > Gardar >
What you are asking for doesn't even work at the R prompt, let alone inside a function. The _really_ ugly kludge I manage to come up with is to manually assign the components of the list (or variables in the data frame) into the function's environment: f <- function(mylist=list(a=1:5)) { nm <- names(mylist) for (i in seq(along=nm)) { assign(nm[i], mget(nm[i], envir=NULL, ifnotfound=mylist[i])) } sapply(names(mylist), function(x) print(get(x))) ls() } Here are a couple of tests:> f()$a [1] 1 2 3 4 5 [1] "a" "i" "mylist" "nm"> f(list(b=5:1, z="ha!"))$b [1] 5 4 3 2 1 $z [1] "ha!" [1] "b" "i" "mylist" "nm" "z" Andy> From: Gardar Johannesson > > I'm trying to find a good way of attaching a list within a > function such > that the attached variables (the list's members) precede the global > environment (.GlobalEnv) in the search list. Here is a > non-working example > using attach(), which hopefully explains better what I'm trying to do: > > > foo <- function(x=0, input=list(a=10)) { > + attach(input) > + on.exit(detach(input)) > + print(search()) > + print(a) > + } > > > > a > Error: Object "a" not found > > foo() ## this prints out a = 10 > [1] ".GlobalEnv" "input" "package:methods" > "package:stats" > [5] "package:graphics" "package:utils" "Autoloads" > "package:base" > [1] 10 > > > > a <- 0 > > foo() ## this prints out a = 0, not a = 10 > [1] ".GlobalEnv" "input" "package:methods" > "package:stats" > [5] "package:graphics" "package:utils" "Autoloads" > "package:base" > [1] 0 > > > > > Thanks, > > Gardar > > > ___________________________________________________________________ > Gardar Johannesson > Lawrence Livermore National Laboratory > 7000 East Avenue, L-229 > Livermore, CA 94550 > > johannesson1 at llnl.gov > Tel: (925) 422-3901, Fax: (925) 422-4141 > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >