Skip to content
Prev 32077 / 398506 Next

<-

Damon Wischik <djw1005 at cam.ac.uk> wrote:
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