Re: [R] assigning from multiple return values

2004-06-24 Thread Gabor Grothendieck
Here is a minor update with support for empty arguments. They are just thrown away eliminating the need to use a dummy name for them. list <- structure(NA,class="result") "[<-.result" <- function(x,...,value) { args <- as.list(match.call()) args <- args[-c(1:2,length(args))] length(valu

Re: [R] assigning from multiple return values

2004-06-23 Thread Richard A. O'Keefe
Gabor Grothendieck <[EMAIL PROTECTED]> wrote: fn <- function(x,y) { assign(as.character(substitute(x)), "zz", sys.frame(-1)) assign(as.character(substitute(y)), 3, sys.frame(-1)) } fn(a,b) # sets a to "zz" and b to 3 "list2<-

Re: [R] assigning from multiple return values

2004-06-23 Thread Gabor Grothendieck
Just a few more examples: # swap a and b without explicitly creating a temporary a <- 1; b <- 2 list[a,b] <- list(b,a) # get eigenvectors and eigenvalues list[eval, evec] <- eigen(cbind(1,1:3,3:1)) # get today's month, day, year require(chron) list[Month, Day, Year] <- month.day.year(unclass(S

Re: [R] assigning from multiple return values

2004-06-23 Thread Gabor Grothendieck
I think I've found a workaround that avoids the two problems in the replacement function approach.With the definitions of list and [<-.result shown, one can write list[a,b] on the left side of an assignment where the right side of the assignment evaluates to a list of the same length (or if th

Re: [R] assigning from multiple return values

2004-06-23 Thread Jack Tanner
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

Re: [R] assigning from multiple return values

2004-06-23 Thread Peter Dalgaard
"Liaw, Andy" <[EMAIL PROTECTED]> writes: > My $0.02: > > 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, then I'd rather suggest that R is perhaps the wrong language for > him. > > To

RE: [R] assigning from multiple return values

2004-06-23 Thread Liaw, Andy
My $0.02: 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, then I'd rather suggest that R is perhaps the wrong language for him. To me the suggested `workarounds' are by far much more hideou

Re: [R] assigning from multiple return values

2004-06-23 Thread Gabor Grothendieck
Here are two approaches assuming foo is "zz" and bar is 3. FIRST You could pass the return variables in the argument list and then assign them in the caller's frame like this: fn <- function(x,y) { assign(as.character(substitute(x)), "zz", sys.frame(-1)) assign(as.character(subs

Re: [R] assigning from multiple return values

2004-06-23 Thread Paul Roebuck
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 <-

[R] assigning from multiple return values

2004-06-23 Thread Jack Tanner
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'