Skip to content

Return a list

10 messages · Stefan Fritsch, Baptiste Auguie, Henrique Dallazuanna +5 more

#
Dear R Users,

another problem for me is the output of a function.

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?

thank you very much for your help!

Stefan Fritsch
#
?attach
or ?with

Hope this helps,

baptiste
On 26 Sep 2008, at 14:57, Stefan Fritsch wrote:

            
_____________________________

Baptiste Augui?

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
#
Try this:

sapply(names(result), function(nm)assign(nm, result[[nm]], envir = globalenv()))


On Fri, Sep 26, 2008 at 10:57 AM, Stefan Fritsch
<fritsch at bips.uni-bremen.de> wrote:

  
    
#
"Stefan Fritsch" <fritsch at bips.uni-bremen.de> wrote:

            
I don't have a good answer for your question, but I do encourage
you to choose a method that will be readily intelligible to you
when you revisit your code X years later.
#
Mike Prager wrote:
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
#
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  

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Wacek Kusnierczyk
Sent: Friday, September 26, 2008 12:40 PM
To: Mike Prager
Cc: R help
Subject: Re: [R] Return a list
Mike Prager wrote:
output variables.
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.
#
Bert Gunter wrote:
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

  
    
#
that's likely to work, but it's even worse than my non-functional
version: the destructive operation on the test's caller's environment is
performed here by the callee (test), and you can get ugly surprises if
you forget that test does that. 

i'd consider this (and the 'for (name in names(result)) assign(...)'
solution) an anti-pattern. 


vQ
N. Lapidus wrote:
#
What about within ?

Hadley