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"))
相关推荐
Cthy_hy13 分钟前
Python算法竞赛:排列组合核心用法
开发语言·python·算法
大圣编程20 分钟前
面向对象深度理解
java·开发语言·算法
爱喝水的鱼丶26 分钟前
SAP-ABAP:SAP 简单报表输出开发系列(共6篇) 第四篇:SAP 报表异常处理机制:数据校验与消息提示规范落地
开发语言·数据库·学习·算法·sap·abap
影寂ldy27 分钟前
C# const 常量 / readonly 只读 / static readonly
java·开发语言·c#
iCxhust1 小时前
c#多串口重量采集上位机程序
开发语言·汇编·c#·微机原理·8088单板机
QK_001 小时前
volatile 关键字核心作用
开发语言
Dxy12393102161 小时前
Python Tensor 向量入门:从零理解深度学习的“数据语言“
开发语言·python·深度学习
林森lsjs1 小时前
【日耕一题】3. 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
java·开发语言
yzy852 小时前
数据同步工具 -- syncthing
开发语言