I was going through some examples in hadley's guide to functionals, and came across an unexpected problem.
Suppose I have a list of model objects,
x=1:3;y=3:1; bah <- list(lm(x~y),lm(y~x))
and want to extract something from each (as suggested in hadley's question about a list called "trials"). I was expecting one of these to work:
lapply(bah,`$`,i='call') # or...
lapply(bah,`$`,call)
However, these return nulls. It seems like I'm not misusing the $
function, as these things work:
`$`(bah[[1]],i='call')
`$`(bah[[1]],call)
Anyway, I'm just doing this as an exercise and am curious where my mistake is. I know I could use an anonymous function, but think there must be a way to use syntax similar to my initial non-solution. I've looked through the places $
is mentioned in ?Extract
, but didn't see any obvious explanation.
I just realized that this works:
lapply(bah,`[[`,i='call')
and this
lapply(bah,function(x)`$`(x,call))
Maybe this just comes down to some lapply
voodoo that demands anonymous functions where none should be needed? I feel like I've heard that somewhere on SO before.
This is documented in ?lapply
, in the "Note" section (emphasis mine):
For historical reasons, the calls created by
lapply
are unevaluated, and code has been written (e.g.bquote
) that relies on this. This means that the recorded call is always of the formFUN(X[[0L]], ...)
, with0L
replaced by the current integer index. This is not normally a problem, but it can be ifFUN
usessys.call
ormatch.call
or if it is a primitive function that makes use of the call. This means that it is often safer to call primitive functions with a wrapper, so that e.g.lapply(ll, function(x) is.numeric(x))
is required in R 2.7.1 to ensure that method dispatch foris.numeric
occurs correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With