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"))
相关推荐
水w2 分钟前
【Python爬虫】简单介绍
开发语言·爬虫·python·beautifulsoup
终身学习基地18 分钟前
第二篇:go包管理
开发语言·后端·golang
1024熙1 小时前
【C++】——lambda表达式
开发语言·数据结构·c++·算法·lambda表达式
Linux编程用C1 小时前
Rust编程学习(一): 变量与数据类型
开发语言·后端·rust
双叶8361 小时前
(51单片机)点阵屏LED显示图片(点阵屏LED教程)(74Hc595教程)
c语言·开发语言·单片机·嵌入式硬件·51单片机
xiongmaodaxia_z72 小时前
python每日一练
开发语言·python·算法
Chandler242 小时前
Go:接口
开发语言·后端·golang
Jasmin Tin Wei2 小时前
css易混淆的知识点
开发语言·javascript·ecmascript
&白帝&2 小时前
java HttpServletRequest 和 HttpServletResponse
java·开发语言
ErizJ2 小时前
Golang|Channel 相关用法理解
开发语言·后端·golang