R Function Call with Ellipsis Trap/Pitfall
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.