Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从try、tryCatch或类似项返回逻辑TRUE/FALSE?_R_Error Handling_Try Catch - Fatal编程技术网

从try、tryCatch或类似项返回逻辑TRUE/FALSE?

从try、tryCatch或类似项返回逻辑TRUE/FALSE?,r,error-handling,try-catch,R,Error Handling,Try Catch,R中是否有一个函数,如果给出错误,则返回“FALSE”,否则返回“TRUE”?或者我需要通过计算类似于ifelse(class(try(stop())==“try error”,FALSE,TRUE)的值来创建自己的代码吗?您肯定不想使用ifelse,但这可能会显示一种更有效的方法: > if( inherits(res <- try( stop() ), "try-error") ){ FALSE} else{ res} Error in try(stop()) : [1] F

R中是否有一个函数,如果给出错误,则返回“FALSE”,否则返回“TRUE”?或者我需要通过计算类似于
ifelse(class(try(stop())==“try error”,FALSE,TRUE)
的值来创建自己的代码吗?

您肯定不想使用
ifelse
,但这可能会显示一种更有效的方法:

> if( inherits(res <- try( stop() ),  "try-error") ){ FALSE} else{ res}
Error in try(stop()) : 
[1] FALSE

> if( inherits( res <- try( "ppp" ),  "try-error") ){ FALSE} else{ res}
[1] "ppp"

>if(继承)(res-if(继承(res我有时不得不这样做。我所做的是沿着这些思路使用了
tryCatch
。如果您正在进行模拟,有时某些算法无法收敛并产生警告,这可能会很方便,但您希望忽略它,因为您不希望在超级计算机上运行100个节点时模拟失败

> out <- tryCatch(stop("bla"), error = function(e) e)
> any(class(out) == "error")
[1] TRUE
>输出任何(类(输出)=“错误”)
[1] 真的
若该过程不间断地(无错误地)进行,那个么将得到FALSE

> out <- tryCatch(1:5, error = function(e) e)
> out
[1] 1 2 3 4 5
> any(class(out) == "error")
[1] FALSE
退出 [1] 1 2 3 4 5 >任何(类(外)=“错误”) [1] 假的
测试中最多可以有一项。@Abe-
ifelse(真/假向量,向量==TRUE时要替换的值,向量==FALSE时要替换的值)
if(单条件==TRUE){do something}else{do something}
@最近的邮件收到了…那么使用
any
(如中所做)将向量转换为单个条件是否更合适?是,还是使用inherits()实现相同的目标