[R] How to write a wrapper function which can honor default values, when the target function has ones?

Adam Ryczkowski adam.ryczkowski at statystyka.net
Fri Sep 13 11:10:17 CEST 2013


   (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


More information about the R-help mailing list