Skip to content
Prev 41215 / 63421 Next

control list gotcha

On Sat, Sep 10, 2011 at 12:31 PM, John C Nash <nashjc at uottawa.ca> wrote:
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:
f(a = 1, b = 1, d = 2)