The answers that were previously given allow you to easily extract results
from your returned list, but if I understand well, this list is created only
because you cannot return several arguments whereas you need to keep the
values of a, b, c, etc. Am I right?
Another solution would be to directly "send" the values you want to keep
into the environment where they are needed. The following example supposes
you need to keep "a" only in the upper environment from which your function
was launched, and "b" in another one (e.g. .GlobalEnv).
Hope this may help.
Nael

> # Here is a function such as yours:
> test <- function(){
+ a <- 1
+ b <- 2
+ return(list(a=a, b=b, c=c))
+ }
>
> result <- test()
> (a <- result$a)
[1] 1
> (b <- result$b)
[1] 2
>
> rm(a, b)
>
> # Now our variables will be automatically assigned into the chosen
environment
> test2 <- function(){
+ a <- 1
+ b <- 2
+ assign("a", a, envir=parent.frame(n=1))
+ assign("b", b, envir=.GlobalEnv)
+ return(NULL)
+ }
>
> # Suppose test2 is launched by another function
> test2.launcher <- function() {
+ test2()
+ print(paste("a exists inside test2.launcher:", exists("a")))
+ print(paste("b exists inside test2.launcher:", exists("b")))
+ return (NULL)
+ }

> test2.launcher()
[1] "a exists inside test2.launcher: TRUE"
[1] "b exists inside test2.launcher: TRUE"
NULL
> exists("a")# a still exists in the upper environment
[1] FALSE
> exists("b")# b does not
[1] TRUE




On Fri, Sep 26, 2008 at 9:39 PM, Wacek Kusnierczyk <
Waclaw.Marcin.Kusnierczyk@idi.ntnu.no> wrote:

> Mike Prager wrote:
> > "Stefan Fritsch" <fritsch@bips.uni-bremen.de> wrote:
> >
> >
> >> I have several output variables which I give back with the list command.
> >>
> >> test <- function     {return(list(a,b,c,d,e,f,g,...))}
> >>
> >> After the usage of the function I want to assign the variables to the
> output variables.
> >>
> >> result <- test()
> >>
> >> a <- result$a
> >> b <- result$b
> >> c <- result$c
> >> d <- result$d
> >> ...
> >>
> >> is there a more elegant way to assign these variables, without writing
> them all down?
> >>
> >>
>
> arguably ugly and risky, but simple:
>
> for (name in names(result)) assign(name, result[[name]])
>
> (note, for this to work you actually need to name the components of the
> returned list: return(list(a=a,b=b,...)))
>
> vQ
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]

