R-posts.com

R Function Call with Ellipsis Trap/Pitfall

Interested in publishing a one-time post on R-bloggers.com? Press here to learn how.
Objective if this post: alerting all users to double check case and spelling of all function parameters

I am newbie in R and was trying RSNNS mlp function and wasted a lot of time due to some typos. RSNNS mlp function silently ignores misspelled keywords
Example:
model<-mlp(iris[,1:4],decodeClassLabels(iris[,5]),hize=7,mazit=100)
I intentionally misspelled size as hize and maxit as mazit
There are no warnings or errors.

I think that many packages may have same problem as package writers may not always validate ellipsis arguments. I made a small spelling mistake and got puzzling results as there was no parameter validation, but I expected that great eminent packages should be robust and help users recover from typos Let us see what happens with no ellipsis
> square <-function(num ){
+ return(num*num)
+ }
> 
> square(num=4)
[1] 16
> square(numm=4)
Error in square(numm = 4) : unused argument (numm = 4)

# With ellipsis added
> square <-function(num, …){
+ print(names(list(…)));
+ return(num*num)
+ }
> 
> square(num=3,bla=4,kla=9)
[1] “bla” “kla”
[1] 9
As you can see names(list(…)) does give access to parameter names The problem is that ellipsis function calls are for flexibility but package writers should take extra care to throw exception “unused argument” when parameters of functions are misspelled.

This to my mind is a major weakness of the R ecosystem. Most parameters have defaults and small case or spelling mistake can lead to really wrong conclusions!

RSNNS  is simply fantastic but given simply as an example of the ellipsis function call trap. Hope other newbies benefit and learn to avoid the trap of wrong arguments.

Jayanta Narayan Choudhuri
Kolkata India
Exit mobile version