How to deal with an ellipsis (...) in the presence of optional arguments?

I have an ellipsis problem when I use optional arguments in my function definition. To clarify, I define the 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 is also displayed through a call to func3 (requires the Rsolnp package).

func2 ( lb = 0, ub = 8, n.restarts = 5, n.sim = 10, n = 200, from = 0, to = 8)

But suppose the user made a mistake 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 expect R to fulfill the following plans to address the lack of n.restarts :

  • assigns a default value, i.e. 5, n.restarts as an optional argument
  • announces a warning at the end: "nrestarts" is not a graphical parameter

But this does not happen, and R assigns the value n (200) instead of n.restarts !!

Can someone help me fix this problem?

Thank you very much

+4
source share
2 answers

This is a partial match of argument n with n.restarts if the user has not provided it. Instead, and contrary to the advice of @Andrie (which will work of course), there is a mechanism that allows you to continue as you have argument n and argument n.restarts . The trick is to put the arguments you want to match exactly after ...

 func2 <- function (lb, ub, ..., n.restarts = 5, n.sim = 10){ writeLines(paste("Value of `n.restarts` is", n.restarts)) 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) output } 

In use, this gives:

 > func2 (lb = 0, ub = 8, n.restarts = 2, n.sim = 10, n = 200, + from = 0, to = 8) Value of `n.restarts` is 2 <---- Here! Iter: 1 fn: 6.926e-15 Pars: 2.00000 Iter: 2 fn: 2.501e-15 Pars: 2.00000 solnp--> Completed in 2 iterations Iter: 1 fn: 8.336e-16 Pars: 2.00000 Iter: 2 fn: 8.336e-16 Pars: 2.00000 solnp--> Completed in 2 iterations [1] 2 > func2 (lb = 0, ub = 8, nrestarts = 2, n.sim = 10, n = 200, + from = 0, to = 8) Value of `n.restarts` is 5 <---- Here! Default Iter: 1 fn: 2.83e-15 Pars: 2.00000 Iter: 2 fn: 2.5e-15 Pars: 2.00000 solnp--> Completed in 2 iterations Iter: 1 fn: 2.037e-15 Pars: 2.00000 Iter: 2 fn: 2.037e-15 Pars: 2.00000 solnp--> Completed in 2 iterations Iter: 1 fn: 1.087e-15 Pars: 2.00000 Iter: 2 fn: 1.087e-15 Pars: 2.00000 solnp--> Completed in 2 iterations Iter: 1 fn: 8.558e-16 Pars: 2.00000 Iter: 2 fn: 8.558e-16 Pars: 2.00000 solnp--> Completed in 2 iterations Iter: 1 fn: 7.147e-16 Pars: 2.00000 Iter: 2 fn: 7.147e-16 Pars: 2.00000 solnp--> Completed in 2 iterations [1] 2 Warning messages: 1: In plot.window(...) : "nrestarts" is not a graphical parameter 2: In plot.xy(xy, type, ...) : "nrestarts" is not a graphical parameter 3: In axis(side = side, at = at, labels = labels, ...) : "nrestarts" is not a graphical parameter 4: In axis(side = side, at = at, labels = labels, ...) : "nrestarts" is not a graphical parameter 5: In box(...) : "nrestarts" is not a graphical parameter 6: In title(...) : "nrestarts" is not a graphical parameter 
+7
source

If you adhere to a regular assessment of the arguments, you will most likely receive warnings right off the bat. Again, you don’t need to use a special rating to get the right behavior. Using a custom estimate is a bad idea, since it is unlikely that you will accurately reproduce the default behavior of R, causing subtle errors for you and your users.

 library(Rsolnp) func1 <- function (x) (x-2)^2 func3 <- function (fun, col = "blue", n = 1000, main = "This is a test", ...){ curve(func1, ..., n = n, col = col, main = main) } # optimizes func1 and call func2 to plot func1 func2 <- function (lb, ub, n.restarts = 5, n.sim = 10, ...){ output <- gosolnp(fun = func1, LB = lb, UB = ub, n.restarts = n.restarts, n.sim = n.sim)$par func3(fun = func1, ...) return(output) } 

Then at startup:

 func2 ( lb = 0, ub = 8, nrestarts = 5, n.sim = 10, n = 200, from = 0, to = 8) 

you get row warnings

 Warning messages: 1: In plot.window(...) : "nrestarts" is not a graphical parameter 2: In plot.xy(xy, type, ...) : "nrestarts" is not a graphical parameter 3: In axis(side = side, at = at, labels = labels, ...) : "nrestarts" is not a graphical parameter 4: In axis(side = side, at = at, labels = labels, ...) : "nrestarts" is not a graphical parameter 5: In box(...) : "nrestarts" is not a graphical parameter 6: In title(...) : "nrestarts" is not a graphical parameter 
+2
source

All Articles