Bug 14495 - Map, Reduce, etc. don't use correct environment for functions passed by name
Map, Reduce, etc. don't use correct environment for functions passed by name
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Misc
R 2.12.1
x86_64/x64/amd64 (64-bit) Windows 64-bit
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-02-04 22:12 UTC by Stavros Macrakis
Modified: 2011-02-22 15:30 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stavros Macrakis 2011-02-04 22:12:17 UTC
Map, Reduce, etc. allow both function names and function values for their function argument, just as lapply does:

Map(sum,list(1:5)) == Map("sum",list(1:5)) => list(15)
lapply(list(1:5),sum) == lapply(list(1:5),"sum") => list(15)

but Map, Reduce, etc. (unlike lapply) do not use the calling frame's environment to dereference functions passed by name:

local({a<-sum; Map("a",list(1:5))})
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'a' of mode 'function' was not found

local({a<-sum; lapply(list(1:5),"a")}) => list(15)

PS: Personally, I think passing functions by name is a bad idea, but it is ubiquitous in R and should be supported uniformly.

PPS: Shouldn't there be a Bugzilla category for standard library functions?
Comment 1 Brian Ripley 2011-02-22 15:30:40 UTC
Actually, Reduce seems to, and no evidence is provided otherwise.

The issue is simply where match.fun() is called from:
it needs to be called in Map as it is in Reduce, Negate ....