R语言【技巧】——判断自定义函数的传参内容是否符合要求

1. 利用 if 条件判断,stop 语句报错

比如 对一个应该传入数值型,数值为 0 或1 的参数:

R 复制代码
if(add.strat<0 | add.strat>1){
    stop("add.strat represents the fraction of pseudoabsences that are
         sampled environmentally stratified and should be between 0 and 1!")
  }

比如 对一个应该传入字符型,字符串为 某个向量元素之一 的参数,类似于选项框:

R 复制代码
possibtype=c("geographic","random","target.group","geo.strat","density",
               "env.strat","env.semi.strat")
  if(length(type)!=1 | !(type%in%possibtype)){
    stop("Invalid specification of pseudoabsence sampling type!")
  }

2. 利用 match.arg 语句判断传参是否在接收范围内

R 复制代码
match.arg(value, choices = c("spatialvalid", "flagged", "clean"))

match.arg(centroids_detail, choices = c("both", "country", "provinces"))

match.arg(outliers_method, choices = c("distance", "quantile", "mad"))
相关推荐
全栈陈序员5 分钟前
【Python】基础语法入门(二十九)—— 正则表达式实战:文本处理利器
开发语言·人工智能·python·正则表达式
Wang's Blog11 分钟前
Java 项目实战: 交友项目 - 发布评论
java·开发语言·交友
屈冠成12 分钟前
ShopXO商城系统测试报告
开发语言·spring·maven·lua
多弗朗皮卡丘19 分钟前
C++STL之空间配置器
开发语言·c++·内存池·空间配置器
tommyjiatong22 分钟前
什么是PHP漏洞
开发语言·php
CodeYp201931 分钟前
第19讲:封装红黑树实现 mymap 和 myset
开发语言·c++·红黑树
超级架构师1 小时前
让 COBOL 长出 AI 大脑:原生 Agent 核心框架 praxo-core 深度剖析
c语言·开发语言·c++·人工智能·python·架构·ai编程
Non-existent9871 小时前
在 WPS/Excel 里做地理数据处理:功能市场 + 奥维 / Google Earth / QGIS 三种跳转实战
开发语言·javascript·ecmascript
xiaolang_8616_wjl2 小时前
c++游戏_宝可梦大世界(源代码)
开发语言·c++·游戏