Skip to content
Prev 310707 / 398502 Next

Is function(x){x}(5) a valid expression?

The syntax
    expression(arg)
means to call the value of 'expression' as a function with argument 'arg'.
Hence you can do things like
   { function(x)x^2 } (11:13) # gives 121, 144, 169
or, weirder,
   eval(call("function", pairlist(x=NULL, base=2), quote(log(x,base)))) (512) # gives 9

If 'expression' is not a function you get an error of the sort you saw, similar
to what you would get if you did
   x <- 1:10
   x(17)
The parser doesn't know if 'expression' will evaluate to a function or not
so it accepts it and you get the error at run time.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com