[R] Return a list
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Sep 26 23:55:07 CEST 2008
Bert Gunter wrote:
> But why do this? Just leave the (preferably named) variables as list
> components and work with them there.
>
> 1. ?comment tells you how to add a comment attribute to the list for self
> documentation (what were the components? how are they related? etc.)
>
> 2. ?with shows you how to access the components of the list individually in
> the "usual" way.
>
> 3. ?lapply and friends shows you how to "loop" over list components .
>
> etc.
>
> Cheers,
> Bert
>
> Bert Gunter
> Genentech
>
>
it depends on what the original author wanted.
with constructs a new environment, and all assignments, if any, made in
the expression evaluated within with are invisible to the outside
(unless one plays with environments, again):
x = 1:10
a = 3
with(test(), { x[1:3] = c(a,b,c); x = x+d; a = a + 1 })
x # still 1:10, whatever test returns
a # still 3. whatever test returns
if the author wanted the values included in the list to be visible and
accessible by simple names, and used in assignments in a larger part of
code, using with might be inconvenient.
it does not mean that my solution below is a good one, it's just a quick
fix. i wouldn't do that in myself, it's badly non-functional ;)
vQ
>>
>>
>>> 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 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.
>
> ______________________________________________
> 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.
>
--
-------------------------------------------------------------------------------
Wacek Kusnierczyk, MD PhD
Email: waku at idi.ntnu.no
Phone: +47 73591875, +47 72574609
Department of Computer and Information Science (IDI)
Faculty of Information Technology, Mathematics and Electrical Engineering (IME)
Norwegian University of Science and Technology (NTNU)
Sem Saelands vei 7, 7491 Trondheim, Norway
Room itv303
Bioinformatics & Gene Regulation Group
Department of Cancer Research and Molecular Medicine (IKM)
Faculty of Medicine (DMF)
Norwegian University of Science and Technology (NTNU)
Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway
Room 231.05.060
More information about the R-help
mailing list