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"))
相关推荐
南方的狮子先生4 分钟前
【数据结构】从线性表到排序算法详解
开发语言·数据结构·c++·算法·排序算法·1024程序员节
froginwe1113 分钟前
HTML5 Audio(音频)
开发语言
程序员皮皮林19 分钟前
Java 25 正式发布:更简洁、更高效、更现代!
java·开发语言·python
ArabySide1 小时前
【Java】理解Java内存中堆栈机制与装箱拆箱的底层逻辑
java·开发语言
superman超哥1 小时前
Rust 开发环境配置:IDE 选择与深度优化实践
开发语言·ide·rust
鹿鸣天涯1 小时前
网络安全等级保护测评高风险判定实施指引(试行)--2020与2025版对比
开发语言·php
好好学习啊天天向上1 小时前
多维c++ vector, vector<pair<int,int>>, vector<vector<pair<int,int>>>示例
开发语言·c++·算法
星河队长1 小时前
C#实现智能提示输入,并增色显示
开发语言·c#
song8546011341 小时前
uniapp如何集成第三方库
开发语言·uni-app