R Function Call with Ellipsis Trap/Pitfall

April 2, 2017
By

(This article was first published on R-posts.com, and kindly contributed to R-bloggers)

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

To leave a comment for the author, please follow the link and comment on their blog: R-posts.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)