使用“...”和“复制”
在 sapply
和 replicate
的文档中,有一个关于使用 ...
的警告
,现在,我可以接受它,但想了解其背后的内容。所以我创建了这个小例子:
innerfunction<-function(x, extrapar1=0, extrapar2=extrapar1)
{
cat("x:", x, ", xp1:", extrapar1, ", xp2:", extrapar2, "\n")
}
middlefunction<-function(x,...)
{
innerfunction(x,...)
}
outerfunction<-function(x, ...)
{
cat("Run middle function:\n")
replicate(2, middlefunction(x,...))
cat("Run inner function:\n")
replicate(2, innerfunction(x,...))
}
outerfunction(1,2,3)
outerfunction(1,extrapar1=2,3)
outerfunction(1,extrapar1=2,extrapar2=3)
也许我做了一些明显错误的事情,但我发现这个结果相当令人沮丧。那么任何人都可以向我解释为什么在上述所有对 outerfunction
的调用中,我得到这个输出:
Run middle function:
x: 1 , xp1: 0 , xp2: 0
x: 1 , xp1: 0 , xp2: 0
Run inner function:
x: 1 , xp1: 0 , xp2: 0
x: 1 , xp1: 0 , xp2: 0
就像我说的:文档似乎对此发出警告,但我不明白为什么会这样。
In the documentation of sapply
and replicate
there is a warning regarding using ...
Now, I can accept it as such, but would like to understand what is behind it. So I've created this little contrived example:
innerfunction<-function(x, extrapar1=0, extrapar2=extrapar1)
{
cat("x:", x, ", xp1:", extrapar1, ", xp2:", extrapar2, "\n")
}
middlefunction<-function(x,...)
{
innerfunction(x,...)
}
outerfunction<-function(x, ...)
{
cat("Run middle function:\n")
replicate(2, middlefunction(x,...))
cat("Run inner function:\n")
replicate(2, innerfunction(x,...))
}
outerfunction(1,2,3)
outerfunction(1,extrapar1=2,3)
outerfunction(1,extrapar1=2,extrapar2=3)
Perhaps I've done something obvious horribly wrong, but I find the result of this rather upsetting. So can anyone explain to me why, in all of the above calls to outerfunction
, I get this output:
Run middle function:
x: 1 , xp1: 0 , xp2: 0
x: 1 , xp1: 0 , xp2: 0
Run inner function:
x: 1 , xp1: 0 , xp2: 0
x: 1 , xp1: 0 , xp2: 0
Like I said: the docs seem to warn for this, but I do not see why this is so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
木格2024-12-01 16:40:38
实际上有一种方法可以做到这一点:
# Simple function:
ff <- function(a,b) print(a+b)
# This will NOT work:
testf <- function(...) {
replicate(expr = ff(...), n = 5)
}
testf(45,56) # argument "b" is missing, with no default
# This will:
testf <- function(...) {
args <- as.list(substitute(list(...)))[-1L]
replicate(expr = do.call(ff, args), n = 5)
}
testf(45,56) # 101
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
示例部分中的
?replicate
明确告诉我们您尝试做的事情不会也不会起作用。在?replicate
的Note
部分中,我们有:如果我们查看示例,我们会看到:
我对文档的阅读是,它们的作用远不止警告您使用
...
在replicate()
调用中;他们明确证明它不起作用。该帮助文件中的大部分讨论都与其他函数的...
参数有关,不一定与replicate()
有关。?replicate
, in the Examples section, tells us explicitly that what you are trying to do does not and will not work. In theNote
section of?replicate
we have:And if we look at Examples, we see:
My reading of the docs is that they do far more than warn you about using
...
inreplicate()
calls; they explicitly document that it does not work. Much of the discussion in that help file relates to the...
argument of the other functions, not necessarily toreplicate()
.