Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- targetf<-function(x=1,...){
- print(paste("x =",x))
- }
- wrapperfX<-function(x,y,...)
- {
- ...<-if(missing(x){
- list()
- }else{
- list(x=x)
- }
- targetf(...)
- }
- libfoo<-function(par1=1,...)
- {
- if (missing(par1))
- {
- warning("par1 is missing!!")
- }
- print(paste0('par1: ',par1))
- libbar(...)
- }
- libbar<-function(par2=1)
- {
- if (missing(par2))
- {
- warning("par2 is missing!!")
- }
- print(paste0('par2: ',par2))
- }
- libfoo2<-function(par3=1,...)
- {
- if (missing(par3))
- {
- warning("par3 is missing!!")
- }
- print(paste0('par3: ',par3))
- libbar2(...)
- }
- libbar2<-function(par4=1)
- {
- if (missing(par4))
- {
- warning("par4 is missing!!")
- }
- print(paste0('par4: ',par4))
- }
- wrapfun<-function(x,par1=3,...,par3,par4)
- {
- libfoo(par1,...) #With dots everything is simple
- pars<-list()
- if(!missing(par3))
- {
- pars<-c(pars,par3=par3)
- }
- if(!missing(par4))
- {
- pars<-c(pars,par4=par4)
- }
- do.call(libfoo2,pars) #This is how we can pass specific arguments, and respecting missings properly.
- }
- wrapfun(par1=5,par2=5,par3=5,par4=5)
- # [1] "par1: 5"
- # [1] "par2: 5"
- # [1] "par3: 5"
- # [1] "par4: 5"
- wrapfun()
- # [1] "par1: 3"
- # [1] "par2: 1"
- # [1] "par3: 1"
- # [1] "par4: 1"
- # Warning messages:
- # 1: In libbar(...) : par2 is missing!!
- # 2: In (function (par3 = 1, ...) : par3 is missing!!
- # 3: In libbar2(...) : par4 is missing!!
- wrapfun(par4=5)
- # [1] "par1: 3"
- # [1] "par2: 1"
- # [1] "par3: 1"
- # [1] "par4: 5"
- # Warning messages:
- # 1: In libbar(...) : par2 is missing!!
- # 2: In (function (par3 = 1, ...) : par3 is missing!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement