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"))
相关推荐
a诠释淡然9 分钟前
C++模板元编程—现代C++的黑魔法
开发语言·c++
charlie11451419117 分钟前
现代C++工程:constexpr 基础:编译期求值的艺术
开发语言·c++
MemoriKu21 分钟前
Flutter 相册 APP 视频模态稳定化实战:从视频抽帧、Embedding 元数据到 Android 真机启动修复
android·开发语言·前端·flutter·架构·音视频·embedding
SilentSamsara25 分钟前
特征工程系统方法论:编码、分箱、交互特征与特征选择
开发语言·人工智能·python·机器学习·青少年编程·信息可视化·pandas
morning_judger26 分钟前
Agent开发系列(十)-知识库建设(架构总览)
开发语言·人工智能
ch.ju30 分钟前
Java程序设计(第3版)第四章——继承的特点
java·开发语言
geovindu32 分钟前
python:Coroutines Pattern
开发语言·python·设计模式·协程模式
A.说学逗唱的Coke34 分钟前
【运维专题】playbooks保姆级使用指南
运维·开发语言·python
牛油果子哥q35 分钟前
【C++运算符重载】C++运算符重载终极精讲:单目/双目/关系/赋值运算符重载、成员与全局重载、重载禁区、底层原理与企业级工程规范
开发语言·c++
AI视觉网奇41 分钟前
stl转glb glb缩放
开发语言·3d