问问题
1676 次
1 回答
1
I'll reply myself with even more elaborate setup (I replaced targetf
with lib*
names). The bar
functions are nested inside foo
ones, so their default values are not easily visible to the user.
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))
}
The wrapper:
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.
}
This is some output:
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!!
于 2013-09-13T12:27:12.470 回答