How can I build flexibility into a wrapper function so that it can handle particular arguments being absent or present.
More specifically, I am building a wrapper for the gowdis function in the FD package and I want to give the user the option of including or excluding the arguments 'asym.bin' and 'ord'. Any advice given the example below:
library(FD)
?gowdis
x<-data.frame("Trait1" =c(1,1,0),
"Trait2"=c(1,1,1),
"Trait3" =c(1,1,0),
"Trait4" =c(0,0,1))
rownames(x)<-c("A","B","C")
w<-c(0.25,0.25,0.25,0.25)
m<-2
asym.bin<-c(1,4)
wrapper.function = function(x,w,m) {
gdis<-gowdis(x,w)
gdis2<-gdis*m
gdis2
}
#excluding the ord and asym.bin works fine
wrapper.function(x,w,m)
A B
B 0.5
C 1.0 1.5
#but I want to give the user the option of including these wrapped arguments i.e.
wrapper.function = function(x,w,m,asym.bin,ord) {
gdis<-gowdis(x,w,asym.bin,ord)
gdis2<-gdis*m
gdis2
}
wrapper.function(x,w,m)
However, this returns an error message
'Error in match.arg(ord) : argument "ord" is missing, with no default'
You can provide default values for arguments:
Alternative way of solving your problem could be (like @Jilber proposed in the comments) using ellipsis.