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"))
相关推荐
码兄科技20 小时前
Java AI智能体开发实战:从零构建智能对话系统指南
java·开发语言·人工智能
Drone_xjw1 天前
从 GDB 到 CDB:C/C++ 程序调试的两把“手术刀”
c语言·开发语言·c++
SL-staff1 天前
智慧园区2000+设备统一管理:JVS-IoT如何降低运维成本40%
java·开发语言·物联网·智慧园区·设备管理·运维优化·jvs-iot
2zcode1 天前
基于MATLAB图像处理的饮料瓶灌装液位检测系统设计与实现
开发语言·图像处理·matlab
必须得开心呀1 天前
QT解决中文乱码问题
开发语言·qt
这不小天嘛1 天前
JAVA八股——redis篇
java·开发语言·redis
逝水无殇1 天前
C# 字符串(String)详解
开发语言·后端·c#
精明的身影1 天前
C++自学之路1:Hello world
开发语言·c++
leo__5201 天前
基于导航数据的扩展卡尔曼滤波(EKF)MATLAB 实现
开发语言·matlab
gugucoding1 天前
25. 【C语言】二进制文件与随机读写
c语言·开发语言