【发布时间】:2016-09-20 23:28:28
【问题描述】:
我正在尝试巧妙地使用 R 中的省略号 (...) 参数,但遇到了一些问题。
我试图在函数的开头传递一些默认参数,而不会通过使用... 和覆盖函数的参数区域来混淆函数的参数区域,如果它们在那里提供。但不知何故,省略号参数似乎并没有得到我的完整向量
test <- function(dat,
# I don't want to have to put default col,
# ylim, ylab, lty arguments etc. here
...) {
# but here, to be overruled if hasArg finds it
color <- "red"
if(hasArg(col)) { # tried it with both "col" and col
message(paste("I have col:", col))
color <- col
}
plot(dat, col = color)
}
函数调用:
test(data.frame(x = 1:10, y = 11:20), col = c("purple", "green", "blue"))
抛出错误:
Error in paste("I have col:", col) (from #8) :
cannot coerce type 'closure' to vector of type 'character'
所以这里出了点问题。如果我立即将省略号参数传递给绘图函数,它确实可以正常工作。
【问题讨论】:
-
听起来你需要阅读some Advanced R。在我链接的页面上搜索
dots。
标签: r function plot arguments ellipsis