问题
Is it possible to execute certain commands in case of error when using tryCatch in R ? I am using the code below but it does not execute X = alternative_value
tryCatch(
{
X = certain_function_that_sometimes_returns_error
},
error=function(e) {
X = alternative_value
})
回答1:
Assign your tryCatch
directly to x
foo <- function() stop("hello")
bar <- function() 'world'
x <- tryCatch(
{
foo()
},
error = function(e){
bar()
}
)
x
# [1] "world"
来源:https://stackoverflow.com/questions/43380908/trycatch-in-r-execute-in-case-of-error