[R] assigning from multiple return values
Paul Roebuck
roebuck at odin.mdacc.tmc.edu
Wed Jun 23 21:06:24 CEST 2004
On Wed, 23 Jun 2004, Jack Tanner wrote:
> I know that if I have a function that returns multiple values, I should
> do return(list(foo, bar)). But what do I do on the recieving end?
>
> fn <- function(x) {
> return(list(foo, bar))
> }
>
> I know that at this point I could say
>
> values.list <- fn(x)
>
> and then access
>
> values.list[1]
> values.list[2]
>
> But that's hideous. I'd rather be able to say something like
>
> list(local_foo, local_bar) <- fn(x)
>
> and have the right thing happen. I realize that it's my responsibility
> to not screw up and say instead
>
> list(local_bar, local_foo)
>
> Any suggestions?
>
How about naming the list elements for clarity then?
fn <- function(x) {
foo <- x;
bar <- 1/x;
return(list(foo = foo,
bar = bar));
}
x <- 10;
values.list <- fn(x);
print(values.list$foo);
print(values.list$bar);
----------------------------------------------------------
SIGSIG -- signature too long (core dumped)
More information about the R-help
mailing list