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"))
相关推荐
leiming6几秒前
C++ vector容器
开发语言·c++·算法
SystickInt23 分钟前
C语言 strcpy和memcpy 异同/区别
c语言·开发语言
CS Beginner33 分钟前
【C语言】windows下编译mingw版本的glew库
c语言·开发语言·windows
FJW02081442 分钟前
Python_work4
开发语言·python
大学生资源网1 小时前
java毕业设计之儿童福利院管理系统的设计与实现(源码+)
java·开发语言·spring boot·mysql·毕业设计·源码·课程设计
JasmineWr1 小时前
JVM栈空间的使用和优化
java·开发语言
Poetinthedusk1 小时前
C#实现图片统一位深
开发语言·c#
吴佳浩 Alben2 小时前
Python入门指南(四)
开发语言·后端·python
一然明月2 小时前
QT之基础控件
开发语言·qt
一勺-_-3 小时前
mermaid图片如何保存成svg格式
开发语言·javascript·ecmascript