Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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
错误时在R中执行tryCatch_R_Error Handling_Try Catch - Fatal编程技术网

错误时在R中执行tryCatch

错误时在R中执行tryCatch,r,error-handling,try-catch,R,Error Handling,Try Catch,在R中使用tryCatch时,如果出现错误,是否可以执行某些命令?我正在使用下面的代码,但它不执行X=可选值 tryCatch( { X = certain_function_that_sometimes_returns_error }, error=function(e) { X = alternative_value }) 将您的tryCatch直接分配给x foo <- function() stop("hello") bar <- function()

在R中使用tryCatch时,如果出现错误,是否可以执行某些命令?我正在使用下面的代码,但它不执行X=可选值

tryCatch(
{
  X = certain_function_that_sometimes_returns_error      
},
error=function(e) {
  X = alternative_value
})

将您的
tryCatch
直接分配给
x

foo <- function() stop("hello")
bar <- function() 'world'

x <- tryCatch(
    {
        foo()
    },
    error = function(e){
        bar()
    }
)

x
# [1] "world"
foo