I have a problem with ellipsis when I use optional arguments in my function definition. To clarify, I define following functions:
func1 <- function (x) (x-2)^2
func3 <- function (fun, arg.curve.user){
arg.curve.user$expr <- substitute(func1)
arg.curve.default <- list(col = "blue", n = 1000, main = "This is a test")
arg.curve <- modifyList (arg.curve.default, arg.curve.user)
do.call("curve", arg.curve)
}
# optimizes func1 and call func2 to plot func1
func2 <- function (lb, ub, n.restarts = 5, n.sim = 10, ...){
arg.curve.user <- as.list(substitute(list(...)))
output <- gosolnp(fun = func1, LB = lb, UB = ub, n.restarts = n.restarts,
n.sim = n.sim)$par
func3(fun = func1, arg.curve.user = arg.curve.user)
return(output)
}
By calling func2, func1 is optimized and also plotted through a func3 call (package Rsolnp is required).
func2 ( lb = 0, ub = 8, n.restarts = 5, n.sim = 10, n = 200, from = 0, to = 8)
But suppose a user misspells n.restarts
and writes nrestarts
:
func2 ( lb = 0, ub = 8, nrestarts = 5, n.sim = 10, n = 200, from = 0, to = 8)
In this case, I expects R to implement the following plans to deal with absence of n.restarts
:
- assigns default value, i.e. 5, to n.restarts as an optional argument
- declares a warning at the end: "nrestarts" is not a graphical parameter
But this does not happen and R assigns value of n (200) to n.restarts instead!!
Can anyone help me to fix this problem?
Many thanks