Skip to content

Weird feature when creating function lists with apply

6 messages · Uffe Høgsbro Thygesen, Brian Ripley, Peter Dalgaard +3 more

#
Hi R-fellows,

can anyone explain this weird feature? I am trying to create a list of
functions with apply, and it appears that there is something strange going
on when the function evaluates the argument a. When I duplicate a into a
local variable b, the result changes !?!

Any pointers? Thank in advance. Cheers,

Uffe


# Create a function which returns a function
# Create a list of functions, parameterised by 1:4
[1] 4

# Repeat
[1] 2

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Wed, 10 Oct 2001, [iso-8859-1] Uffe Høgsbro Thygesen wrote:

            
I don't think this is what you intended.

ll <- apply(as.array(1:4),1,f1)
ll
[[1]]
function(x) a*x
<environment: a6d374>

[[2]]
function(x) a*x
<environment: a6d09c>

[[3]]
function(x) a*x
<environment: a6dd60>

[[4]]
function(x) a*x
<environment: a6da88>

which is not what your comment implies.

And
[1] 4

shows why they differ.


1) This is a strange way to produce a list via arrays:
ll <- lapply(1:4, f1) is what is normally used.

2) I think what you really wanted was

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

[[2]]
function(x) 2 * x

[[3]]
function(x) 3 * x

[[4]]
function(x) 4 * x
#
Uffe H?gsbro Thygesen <uht at dfu.min.dk> writes:
This should work too:
f3 <- function(a) {a;function(x) a*x}
apply(as.array(1:4),1,f3)[[2]](1)

It is essentially the same situation that caused Luke Tierney
substantial headscratching recently. The key is lazy evaluation.
Here's part of the conversation from then:

[Snip from email from Luke]
On Wed, Aug 29, 2001 at 11:25:54PM +0200, Peter Dalgaard BSA wrote:
Now imagine getting bitten by this while experimenting with some new
threading code, which is of course then the prime suspect for all bad
things that happen, and you can see why Duncan and I were tearing our
hair out for a bit :-).

  
    
#
Hello All,

I am creating a graph in a loop, and want to use letters for marking
symbols.  Is there a function to convert an integer into a character.  I
am looking for something similar to the ASC() function of good old
BASIC, or the (char) type conversion of C.

Thanks,

John.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
LETTERS or letters might do what you want; they are character vectors
containing the (upper- or lowercase) alphabet.  (If they didn't exist you
could create them ...)
On Wed, 10 Oct 2001, John Janmaat wrote:

            

  
    
#
On Wed, 10 Oct 2001 09:55:25 -0300, you wrote in message
<3BC4453D.94ACB0E0 at acadiau.ca>:
There are the letters and LETTERS vectors.  For example,

 > letters[1:3]
 [1] "a" "b" "c"

I don't think there's any function that does general ASCII
conversions, but you could fairly easily enter your own vector of
characters if letters aren't sufficient.  I imagine you'll have a hard
time entering a null character.  If you're on some platforms, you
could write your own general typecasting function using a fifo, but
those aren't supported everywhere.

Duncan Murdoch
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._