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

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

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

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"))
相关推荐
GIS阵地2 分钟前
QgsRasterDataProvider 完整详解(QGIS 3.40.13 C++)
开发语言·c++·qt·开源软件·qgis
yaoxin5211231 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
阿里嘎多学长1 小时前
2026-07-14 GitHub 热点项目精选
开发语言·程序员·github·代码托管
charlie1145141912 小时前
Cinux: 为大内核铺路
开发语言·c++·操作系统·现代c++
2501_914245932 小时前
C语言设计模式详解:从理论到实践的完整指南
c语言·开发语言·设计模式
Hazenix3 小时前
Go 指南:一篇文章速通 Golang
开发语言·后端·golang
灯澜忆梦3 小时前
GO_复合类型---指针
开发语言·后端·golang
Ulyanov3 小时前
雷达导引头Python仿真框架:GPU加速、6-DOF模型与半实物仿真接口
开发语言·python·雷达信号处理·雷达导引头
名字还没想好☜4 小时前
Go slice 的 append 陷阱:共享底层数组导致的数据串改
开发语言·后端·golang·go·slice
bitbrowser4 小时前
Claude 账号频繁被封?转向国内可直接使用的 AI 工具
开发语言·php