问题描述
当我在函数定义中使用可选参数时,省略号存在问题.为了澄清起见,我定义了以下功能:
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)
}
通过调用func2,可对func1进行优化,并通过func3调用进行绘制(需要软件包 Rsolnp ).
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)
但是假设用户拼错了n.restarts
并编写了nrestarts
:
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)
在这种情况下,我希望R实施以下计划以应对n.restarts
的缺失:
In this case, I expects R to implement the following plans to deal with absence of n.restarts
:
- 将默认值(即5)分配给n.作为可选参数重新启动
- 最后声明警告:"nrestarts"不是图形参数
但这不会发生,并且 R将n的值(200)分配给n.重新启动!
有人可以帮助我解决此问题吗?
Can anyone help me to fix this problem?
非常感谢
推荐答案
当用户未提供n
参数和n.restarts
时,它部分匹配.取而代之的是,与@Andrie的建议相反(当然会起作用),有一种机制可以让您以自己的方式继续使用参数n
和参数n.restarts
.诀窍是将要匹配的参数完全 放置在...
之后.
It is partial matching the n
argument to n.restarts
when one is not supplied by the user. Instead, and contrary to the advice of @Andrie (which will work, of course), there is a mechanism that allows you to continue in the manner you have with an argument n
and and argument n.restarts
. The trick is to place arguments you want to match exactly after the ...
.
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
}
在使用中,它给出:
> 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
这篇关于在可选参数存在的情况下如何处理省略号(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!