[R] I don't understand this

Simon Fear Simon.Fear at synequanon.com
Wed Sep 3 11:51:46 CEST 2003

Thanks to everyone for enlightening me about (f)(g). So I
suppose (f)(g)(h)... is OK as long as (f)(g) returns a function with
an argument (and so on)?

Meanwhile, back on Earth, in attempting a compromise between 
the clarity of Barry R's

if (cond) names(x) <- 10 else dim(x) <- 10

and the generality of Peter D's

eval(substitute(foo(x)<-10,list(foo=as.name(if (cond) "names" else

I tried to set it up as a clearer two-liner:

foo <- if (cond) names else dim
foo(x) <- 10

> foo <- if (TRUE) names else dim
> foo
function (x) 
<environment: namespace:base>

So far so good. But

> foo(x) <- 10
Error: couldn't find function "foo<-"

Shedding further light on the semantic problem with the construct
will only work if "f<-" is already explicitly defined, in addition to
being defined. (Or if "<-" has had its default overwritten.)
Making "foo" equivalent to "names" does not make "foo<-" equivalent
to "names<-". You need an on-the-fly explicit eval(substitute(...)) 
to do that.

The same should hold for "(f)(x)<-" except that (f) might find a
non-function value; I would expect (and hope) for a crash if so.

Sorry if this is obvious to many people on the list. It has been a very
enlightening discussion for me. At least, I thought I was learning
something, until at the next prompt I experimented with

> (foo)(x) <- 10
Error: couldn't find function "H-ì<-"

Now where did THAT error message come from??

(I am running R version 0.1 on a PDP 8. Same result with 1.7.0
under Win98)

Simon Fear
Senior Statistician
Syne qua non Ltd
Tel: +44 (0) 1379 644449
Fax: +44 (0) 1379 644445
email: Simon.Fear at synequanon.com
web: http://www.synequanon.com
Number of attachments included with this message: 0
This message (and any associated files) is confidential and\...{{dropped}}

More information about the R-help mailing list