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"))
相关推荐
丰锋ff7 分钟前
基于 Qt 的智能门禁系统(二)
开发语言·qt
fpcc19 分钟前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
SomeB1oody34 分钟前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
mqiqe2 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim2 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
脉动数据行情2 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学2 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
zx_741484813 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
liangshanbo12154 小时前
虚拟列表深度面试题整理
java·开发语言·前端
ZISHU_9874 小时前
用 TLabel 给 SynTouch BioTac 数据做语义标注:从原始信号到结构化标注
开发语言·人工智能·python·数据·机器人触觉