Skip to content

Weird feature when creating function lists with apply

2 messages · Uffe Høgsbro Thygesen, Peter Dalgaard

#
Thanks, Peter and Prof. Ripley, for your helpful replies.

Referring to Prof. Ripley's reply, when I do

f3 <- function(a) substitute(function(x) a*x, list(a = a))
f <- lapply(1:4, f3)[[1]]

then f is not a function:
[1] FALSE

but
[1] TRUE

Consistently, if I do

f4 <- function(a) eval(substitute(function(x) a*x, list(a = a)))
g <- lapply(1:4, f4)[[1]]

then g is indeed a function:
[1] TRUE

I do still not understand what f is, then, except something that evaluates
to a function. My conclusion is that I need to start my head, too, and
understand this lazy evaluation.

Thanks again. Cheers,

Uffe



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Uffe H. Thygesen, M.Sc.&Eng., Ph.D.
Danish Institute of Fisheries Research
http://www.dfu.min.dk


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Uffe H?gsbro Thygesen <uht at dfu.min.dk> writes:
...
This part is not about lazy evaluation. This is because substitute
returns an object of mode "call", sometimes called an "unevaluated
expression", (which is a bit unfortunate because there is also an
"expression" mode, of which it is not...)

You could try picking it apart:
function
$x
1 * x
*
[..etc..]
function(x) a*x

!!! Now *that* is truly weird.... The "source" atribute for g is
getting set, seemingly out of nowhere, to something that should not be
part of f. (This is a bug, but some nasty trickery is involved in
these matters and if might be difficult to fix.)

However if we clear
function (x) 
1 * x

you can see that g is the actual function, not the call to the
function creator.