[Rd] A trap for young players with the lapply() function.

Rolf Turner r.turner at auckland.ac.nz
Wed Mar 29 06:04:36 CEST 2017


On 29/03/17 11:03, William Dunlap wrote:

>> I think that the suggestion I made, in response to a posting by
>> Barry Rowlingson, that the first argument of lapply() be given the name of
>> ".X" rather than just-plain-X, would be (a) effective, and (b) harmless.
>
> It would break any call to *apply() that used X= to name the first
> argument. There are currently 3020 such calls in the R code in CRAN.

Okay.  Scratch that idea.

> One can avoid the problem by creating the function given as the FUN
> argument when one calls lapply() and the like.  Don't give that
> function arguments named "X", "FUN", "USE.NAMES", etc. and perhaps
> make use of R's lexical scoping to avoid having to use many arguments
> to the function.  E.g., instead of
>     sapply(1:5, sin)
> use
>     sapply(1:5, function(theta) sin(theta))
> or instead of
>     myY <- 3
>     sapply(1:5, atan2, y=myY)
> use
>     myY <- 3
>     sapply(1:5, function(x) atan2(myY, x))

Again, all very sound advice but it does not address the problem of 
there being a trap for young players.  The advice can only be applied by 
a user only if the user is *aware* of the trap.

At this point it would appear that the problem is fundamentally 
unsolvable. :-(

cheers,

Rolf

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-devel mailing list