【发布时间】:2020-09-23 04:26:11
【问题描述】:
我有一个按逻辑接受输入和返回的函数,我是 trycatch 的新手,无法获得我想要的相应结果,如果有人可以提供帮助,我将不胜感激
-
如果库 SHAPforxgboost 不存在,我想停止该功能并返回异常,说明请安装库
-
我必须检查函数的输入,如果不是数据框或矩阵,请停止函数并返回异常/错误,说明输入错误,请仅输入数据框或矩阵作为输入。
如果可能的话,有没有更好的方法来检查输入,根据我的逻辑,这似乎是一种笨拙的方法来检查数据框或矩阵是否存在
example<-function(a){
library("SHAPforxgboost") ### Throw an custom error that library is not present please install
### the library and stop the function and return the error as o/p
### Similarly if the input is not dataframe or matrix, stop the function
### and return custom error wrong input
if("data.frame"==class(a)){
print("correct input")
}else if("matrix"==class(a)){
print("correct input")
} else{print("wrong input")}
return(x)
}
e<-as.vector(3)
s<-as.data.frame(4)
p<-as.matrix(2)
example(e)
example(s)
example(p)
【问题讨论】: