[R] assigning from multiple return values

Jack Tanner ihok at hotmail.com
Wed Jun 23 23:06:00 CEST 2004


Rolf Turner wrote:

>	> fn <- function(x) {
>		list(local_foo=foo, local_bar=bar)
>	}
>  
>
OK, that works nicely. (And thanks to Paul Roebuck, who mentioned the
values.list$foo notation.)

>(the ``return(...)'' is brought to you by your Department of
>Redundancy Department.)
>  
>
Aha! LISP reards its head. So what happens if I do

fn <- function(x) {
  some stuff
  ...
  end of stuff
  list(foo = foo, bar = bar)
}

return.value <- fn(x)

Will return.value ever contain any residuals from "stuff", or will it
always be exactly the result of the last statement, list(foo...) here?

>The very fact that you are talking about ``multiple values''
>indicates that you're not really understanding what's going on.
>Functions in R always return just ***one*** value.  That value is an
>object which may have an arbitrarily complicated (list structure).
>Related objects get bundled up into lists, giving you just one object
>to deal with.  Then you can pass that one object to another function,
>which can then pick apart that object and use the components in the
>appropriate way without you, the user, having to fiddle with them.
>  
>
Fine, but the whole notion of loose coupling goes out the window if
you're passing lists of related (but very different) objects from
function to function, and each function has to be aware of the order of
the items in the list.

Liaw, Andy wrote:

> If Jack feels that the fact that functions usually return a single 
> list, and
> one needs to access the components of the list separately, is somehow
> hideous

I don't; I think that this is fine when you can access the components of
the list by name only, without regard to order.

Thanks to all for your help.




More information about the R-help mailing list