我无法理解 ...
参数在 R 中的工作原理,我找不到完整的文档,它是某种可编辑对象吗?
我正在编写一个 R 包,我想编写一个使用点的函数,将它们传递给另一个函数 (plot
),但我想将一些参数传递给内部仅当它们不在 ...
中时才起作用。我知道使用 list(...)
我可以检查它们是否已指定,但我不知道是否可以将它们添加到 ...
否则。完成这项工作的最佳方法是什么?
我尝试将编辑后的 pars= list(...)
传递给内部函数 plot(...= pars)
,但它不起作用。实际上最后我找到了一个解决方案,即通过 modifyList
更新列表 pars
,然后使用 do.call
将它们传递给内部函数,但这对我来说有点复杂,有没有更简单的解决方案?
最佳答案
您可以像这样传递...
:
# weird way to construct a linear function
f0 <- function (x, b=1) x+b
f1 <- function (x, a=2, ...){
# INITIAL WRONG ANSWER stopifnot(exists("b")) # see comments
if("b" %in% names(list(...))){
f0(a*x, ...)
}else{
f0(a*x, b=4, ...) # Only makes sense if b=4 is a default that has meaning
# in f1 but not outside of f1 ; or if you cannot change
# the definition of f0 (imported functions). Otherwise,
# you'd better change the default in f0.
}
}
f1(10)
f1(10, b=3)
关于r - 在 R 中编辑...参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856910/