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"))
相关推荐
仪器科学与传感技术博士14 分钟前
Matplotlib库:Python数据可视化的基石,发现它的美
开发语言·人工智能·python·算法·信息可视化·matplotlib·图表可视化
CHEN5_022 小时前
Java基础知识总结
java·开发语言
Kiri霧2 小时前
Kotlin反射
java·开发语言·kotlin
爱吃芒果的蘑菇3 小时前
使用pybind11封装C++API
开发语言·c++·python
慕y2744 小时前
Java学习第一百一十一部分——Jenkins(二)
java·开发语言·学习·jenkins
BUG再也不见4 小时前
Python爬虫 urllib 模块详细教程:零基础小白的入门指南
开发语言·网络·爬虫·python
钢铁男儿4 小时前
C# 异步编程(GUI程序中的异步操作)
开发语言·c#
weixin_307779134 小时前
C#实现Hive到Snowflake数据迁移
开发语言·数据仓库·hive·c#
weixin_493202635 小时前
R语言代码加密(1)
r语言
0wioiw06 小时前
Android-Kotlin基础(Jetpack③-LiveData)
android·开发语言·kotlin