<-
I rather object to this sort of solution. One of the strong points of the S language is lists which allow items that belong together to be in a single object. It's hard to believe that the results of a single call to a function don't belong together. R without an accent (or at least with my accent) would attach the list: res <- myfunc() attach(res) summary(a) Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User")
David Brahm wrote:
Damon Wischik <djw1005 at cam.ac.uk> wrote:
One thing I would find very handy is a shortcut for res <- myfunc() a <- res$val1 b <- res$val2 Something along the lines of list(a=val1,b=val2) <- myfunc() but I don't know what the right syntax would be or how I'd go about programming it. Any suggestions?
I agree this would be handy! And appealing to Perl folks who are used to:
Perl> ($arg1, $arg2) = @ARGV;
I couldn't figure out how to do it with replacement functions (see Prof Brian
Ripley's <ripley at stats.ox.ac.uk> reply), but here's another approach:
multi.assign <- function(x, ...) {
mycall <- match.call()[-2]
mycall[1] <- call("list")
mylist <- eval(mycall, x)
for (i in names(mylist)) assign(i, mylist[[i]], parent.frame())
}
Here's an example:
R> myfunc <- function() list(val1=7, val2=c(5,5))
R> multi.assign(myfunc(), a=val1, b=val2, d=val1+val2)
R> a
[1] 7
R> b
[1] 5 5
R> d
[1] 12 12