Adam Ryczkowski
2013-Sep-13 09:10 UTC
[R] How to write a wrapper function which can honor default values, when the target function has ones?
(This is crosspost from [1]http://stackoverflow.com/questions/18670895/how-to-write-a-wrapper-functi on-which-can-honour-default-values-when-the-target, posted week ago, where although the question did receive some attention, nobody was able to help me.) I'd like to write a more-or-less generic caller to `targetf` that retains its default parameters. Suppose we have a provided by some 3rd party library `targetf`: targetf<-function(x=1,...){ print(paste("x =",x)) } How to write `wrapperf`, that will respect `targetf`s default arguments, so calling `wrapperf()` would not yield the error massage `Error in paste("x =", x) : argument "x" is missing, with no default`? The obvious candidate wrapperf1<-function(x,y) { targetf(x=x) } doesn't seem to respect targetf's default value for parameter `x`. OTH the wrapperf2<-function(...) { targetf(...) } behaves correctly, but it doesn't work for me, because I only care to pass the `x` argument, (and possibly reserve the `...` to other functions in `wrapperf` body). Maybe to solve the issue I'd have to play with ellipsis filtering, which is a *terra incognita* for me at the moment... * * * One idea on how to solve the problem: maybe I'd need to create a specially crafted `...` object from scratch in `wrapperf` to do pseudo code like this: wrapperfX<-function(x,y,...) { ...<-if(missing(x){ list() }else{ list(x=x) } targetf(...) } But I have no idea how to even start doing assignments into ellipsis... is it possible at all? kind regards, Adam Ryczkowski [2]www.statystyka.net [3]+48505919892 [4]Skype:sisteczko References 1. http://stackoverflow.com/questions/18670895/how-to-write-a-wrapper-function-which-can-honour-default-values-when-the-target 2. http://www.google.com/ 3. callto:+48505919892 4. skype:sisteczko
Gerrit Eichner
2013-Sep-13 11:21 UTC
[R] How to write a wrapper function which can honor defaultvalues, when the target function has ones?
Hello, Adam, I'm rather uncertain about your goal (and consequently even more so about how to reach it), but anyway, maybe the function match.call() with its argument expand.dots is of some help for you. From its help page: "match.call is most commonly used in two circumstances: To record the call for later re-use: for example most model-fitting functions record the call as element call of the list they return. Here the default expand.dots = TRUE is appropriate. To pass most of the call to another function, often model.frame. Here the common idiom is that expand.dots = FALSE is used, and the ... element of the matched call is removed. An alternative is to explicitly select the arguments to be passed on, as is done in lm." Hth -- Gerrit On Fri, 13 Sep 2013, Adam Ryczkowski wrote:> > (This is crosspost from > [1]http://stackoverflow.com/questions/18670895/how-to-write-a-wrapper-functi > on-which-can-honour-default-values-when-the-target, posted week ago, where > although the question did receive some attention, nobody was able to help > me.) > I'd like to write a more-or-less generic caller to `targetf` that retains > its default parameters. > Suppose we have a provided by some 3rd party library `targetf`: > targetf<-function(x=1,...){ > print(paste("x =",x)) > } > How to write `wrapperf`, that will respect `targetf`s default arguments, so > calling `wrapperf()` would not yield the error massage `Error in paste("x > =", x) : argument "x" is missing, with no default`? > The obvious candidate > wrapperf1<-function(x,y) { > targetf(x=x) > } > doesn't seem to respect targetf's default value for parameter `x`. > OTH the > wrapperf2<-function(...) { > targetf(...) > } > behaves correctly, but it doesn't work for me, because I only care to pass > the `x` argument, (and possibly reserve the `...` to other functions in > `wrapperf` body). > Maybe to solve the issue I'd have to play with ellipsis filtering, which is > a *terra incognita* for me at the moment... > * * * > One idea on how to solve the problem: maybe I'd need to create a specially > crafted `...` object from scratch in `wrapperf` to do pseudo code like this: > wrapperfX<-function(x,y,...) > { > ...<-if(missing(x){ > list() > }else{ > list(x=x) > } > targetf(...) > } > But I have no idea how to even start doing assignments into ellipsis... is > it possible at all? > kind regards, > Adam Ryczkowski > > [2]www.statystyka.net > [3]+48505919892 > [4]Skype:sisteczko > > References > > 1. http://stackoverflow.com/questions/18670895/how-to-write-a-wrapper-function-which-can-honour-default-values-when-the-target > 2. http://www.google.com/ > 3. callto:+48505919892 > 4. skype:sisteczko > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.