我想創建一個包裝函數來替換一些默認參數。R:使用省略號參數(...)
在這裏,問題的核心,我掙扎:
Error in localWindow(xlim, ylim, log, asp, ...) :
formal argument "cex" matched by multiple actual arguments
現在有點背景。假設我定義的包裝函數情節是這樣的:
myplot <- function(x, ...) {
plot(x, cex= 1.5, ...)
}
如果我打電話myplot(1:10, cex= 2)
我會得到上述錯誤。我知道我可以把...
到列表
l <- list(...)
,然後我可以做
if(is.null(l[["cex"]])) l[["cex"]] <- 2
但是,我怎麼能「插入」此列表回省略參數?喜歡的東西(我知道這不會工作):
... <- l
編輯:我可以在myplot
定義(比如從@Thomas答案建議)使用默認設置,但我不希望:功能界面會變得混亂。我想我可以定義一個輔助函數那樣:
.myfunchelper <- function(x, cex= 2.0, ...) {
plot(x, cex= cex, ...)
}
myfunc <- function(x, ...) {
.myfunchelper(x, ...)
}
但(我)這是那麼優雅和(ii)不滿足我的好奇心。
是的,但這正是我想要避免的。真正的問題是複雜的,我不想把默認值放在函數調用定義中 - 已經有太多的參數了。 – January
我已經根據以下答案更新:http://stackoverflow.com/questions/7028385/can-i-remove-an-element-in-dot-dot-dot-and-pass-it-on/7028786# 7028786 – Thomas