[R] style question: returning multiple arguments - structure orlist

Thomas Lumley tlumley at u.washington.edu
Sat Jul 28 23:43:25 CEST 2001


On 28 Jul 2001, Peter Dalgaard BSA wrote:

> A little summer exercise: Can one write a list assignment function,
> i.e. "list<-" so that
>
> list(a,b,c) <- f()
>
> would be equivalent to
>
> r <- f()
> a <- r[[1]]
> b <- r[[2]]
> c <- r[[3]]
>
> Even better, do something useful with named list elements. (And what
> are the odds of finding that this is really an exercise hidden
> somewhere in a book by Venables and Ripley?)

I think you'll find that the first argument of an assignment function has
to exist already -- the function is passed the evaluated first argument.

This means it isn't quite true that
  a<-"foo<-"(a,value)
is the same as
  foo(a)<-value

eg
> "foo<-"<-function(x,value){list(substitute(x),value)}
> foo(a)<-4
Error: Object "a" not found
> "foo<-"(a,1)
[[1]]
a

[[2]]
[1] 1
> a<-1
> foo(a)<-4
> a
[[1]]
*tmp*

[[2]]
[1] 4


If this is correct, then you can't really write "list<-"

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list