[Rd] control list gotcha

Gabor Grothendieck ggrothendieck at gmail.com
Sat Sep 10 19:08:59 CEST 2011


On Sat, Sep 10, 2011 at 12:31 PM, John C Nash <nashjc at uottawa.ca> wrote:
> This is mainly a reminder to others developing R packages to be careful not to supply
> control list items that are not used by the called package. Optimx is a wrapper package
> that aims to provide a common syntax to a number of existing optimization packages.
> Recently in extending optimx package I inadvertently introduced a new control for optimx
> which is NOT in any of the wrapped optimization packages. There are probably other methods
> of keeping things tidy, but I copy the control list and null out unwanted elements for
> each of the called packages. I missed this in a couple of places in the R-forge
> development version of optimx (Iam working on fixing these, but they are still there at
> the moment).
>
> The "nasty" here was that the package mostly works, with plausible but not very good
> results for some of the optimizers. If it crashed and burned, it would have been noticed
> sooner. There is also a potential interaction with a use of the dot-dot-dot variable to
> pass scaling information.
>
> If there are ideas on how to quickly reveal errors related to calling sequences involving
> control lists and "...", I'd welcome them (off-list?), and be prepared to summarize
> findings in a vignette.
>


Suppose we wish to call f with the control.list components plus
those in the default.args not already specified in the control.list.
If any such arg is not an arg of f exclude it:

# test data - f, default.args and control.list
f <- function(a, b, c = 0, d = 1) print(match.call())
default.args <- list(a = 2, b = 1)
control.list <- list(a = 1, d = 2, e = 3)

# override default.args with control.list
use.args <- modifyList(default.args, control.list)

# exclude components of use.args that are not args of f
sel <- names(use.args) %in% names(as.list(args(f)))
final.args <- use.args[sel]

# run f
do.call("f", final.args)

The last line gives:

> do.call("f", final.args)
f(a = 1, b = 1, d = 2)

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-devel mailing list