R代码忽略了nargs()if语句
无法理解为什么R在下面的代码中忽略了R代码忽略了nargs()if语句,r,args,R,Args,无法理解为什么R在下面的代码中忽略了nargs() foo <- function(x=NULL) { if (nargs() > 1){ stop("Enter 1 argument only") } cat("call was ", deparse(match.call()), "\n", sep = "") } foo 1){ 停止(“仅输入1个参数”) } cat(“调用was”,deparse(match.call()),“\n”,sep=”“) }
nargs()
foo <- function(x=NULL) {
if (nargs() > 1){
stop("Enter 1 argument only")
}
cat("call was ", deparse(match.call()), "\n", sep = "")
}
foo 1){
停止(“仅输入1个参数”)
}
cat(“调用was”,deparse(match.call()),“\n”,sep=”“)
}
当我执行foo(“a”,“b”)
时,我在foo(“a”,“b”)中得到错误:未使用的参数(“b”)
而不是仅输入1个参数
请告知对于多个参数,我们可以使用三个点(
..
),并且使用nargs
的条件将对其进行评估
foo <- function(...) {
if (nargs() > 1){
stop("Enter 1 argument only")
}
cat("call was ", deparse(match.call()), "\n", sep = "")
}
foo("a", "b")
如果有多个参数,请使用
…
。i、 e.function(…){
在您的函数中,只有一个参数“x”,您提供了两个foo(“a”,“b”)
foo("a")
#call was foo("a")