错误时在R中执行tryCatch
在R中使用tryCatch时,如果出现错误,是否可以执行某些命令?我正在使用下面的代码,但它不执行X=可选值错误时在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()
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