r - R Code ignored nargs() if statement
问题描述
Cannot figure out why R is ignoring nargs()
in following
foo <- function(x=NULL) {
if (nargs() > 1){
stop("Enter 1 argument only")
}
cat("call was ", deparse(match.call()), "\n", sep = "")
}
When I execute foo("a","b")
, I get Error in foo("a", "b") : unused argument ("b")
instead of Enter 1 argument only
.
Please advise
解决方案
对于多个参数,我们可以使用三个点 ( ...
) 并且条件 withnargs
将评估它
foo <- function(...) {
if (nargs() > 1){
stop("Enter 1 argument only")
}
cat("call was ", deparse(match.call()), "\n", sep = "")
}
foo("a", "b")
foo("a", "b") 中的错误:仅输入 1 个参数
foo("a")
#call was foo("a")
推荐阅读
- java - HttpUrlConnection - 离线缓存数据
- css - 在 scss 导入上设置 `font-display: swap`
- powershell - IIS 10 - 使用 Powershell 导入 SSL 证书 - “指定的登录会话不存在”
- delphi - TGaussianBlurEffect 图像质量低
- java - ArrayList.clone() 方法在 Java 中如何工作?
- java - 读取文件时,我试图将每一行存储到相关的 LinkedList,但无法读取第一个字符。这怎么样?
- sql - 在 SQL 变量上使用百分比符号的问题
- python - 忽略具有括号的列表中的单词的最有效(pythonic)方法是什么?
- r - For循环并向R中的数据框添加一个新列
- c++ - 如何捕获函数参数并存储函数指针以供以后在 C++11 中执行?