1. 利用 if 条件判断,stop 语句报错
比如 对一个应该传入数值型,数值为 0 或1 的参数:
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"))