Bug 16577

Summary: Vectorize(combn) fails strangly
Product: R Reporter: Neal Fultz <nfultz>
Component: MiscAssignee: R-core <R-core>
Status: CLOSED FIXED    
Severity: minor CC: murdoch, pd.mes
Priority: P5    
Version: R-devel (trunk)   
Hardware: x86_64/x64/amd64 (64-bit)   
OS: Linux   

Description Neal Fultz 2015-10-23 03:15:53 UTC
I would like to calculate all combinations of all sizes.

With m=1, works as expected:

> combn(4, 1)
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4

I get an appropriate error message here:

> combn(4, 1:4)
Error: length(m) == 1L is not TRUE

Try to Vectorize() it:

> combnV <- Vectorize(combn)
> combnV(4, 1:4)
Error in match.fun(FUN) : argument "FUN" is missing, with no default

My best guess is that combn's FUN=NULL somehow interferes with Vectorize. Maybe combn should be changed to check using missing(FUN) instead of is.null(FUN). Maybe just update ?Vectorize if this is a known limitation.

Here is the traceback if that helps, it shows FUN has disappeared from the mapply call.

> traceback()
4: match.fun(FUN)
3: mapply(x = 4, m = 1:4, MoreArgs = list(), SIMPLIFY = TRUE, USE.NAMES = TRUE)
2: do.call("mapply", c(FUN = FUN, args[dovec], MoreArgs = list(args[!dovec]), 
1: combnV(4, 1:4)

I am submitting this because ?bug.report says: 

> If a command you are familiar with causes an R error message in a
> case where its usual definition ought to be reasonable, it is
> probably a bug.

Here is my session info:

> sessionInfo()
R Under development (unstable) (2015-10-09 r69501)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: openSUSE 13.2 (Harlequin) (x86_64)

 [1] LC_CTYPE=en_US.utf8       LC_NUMERIC=C             
 [3] LC_TIME=en_US.utf8        LC_COLLATE=en_US.utf8    
 [5] LC_MONETARY=en_US.utf8    LC_MESSAGES=en_US.utf8   
 [7] LC_PAPER=en_US.utf8       LC_NAME=C                
 [9] LC_ADDRESS=C              LC_TELEPHONE=C           

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base  

Thanks for looking in to this.

-Neal Fultz
Comment 1 Peter Dalgaard 2015-10-23 09:32:26 UTC
There's an inadvertent variable capture of FUN in the function generated by Vectorize. AFAICS it will break on every function that has an argument named FUN. 

For the case at hand, you can work around it with


or just sidestep the whole thing with 

sapply(1:4, combn, x=4)
Comment 2 Duncan Murdoch 2015-12-14 13:35:35 UTC
This is now "fixed", by documenting the limitation and checking for it, in R-devel and R-patched.