【问题标题】:tryCatch in R execute in case of errorR中的tryCatch在出错的情况下执行
【发布时间】:2017-09-08 21:43:59
【问题描述】:

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

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

【问题讨论】:

    标签: r error-handling try-catch


    【解决方案1】:

    将您的tryCatch 直接分配给x

    foo <- function() stop("hello")
    bar <- function() 'world'
    
    x <- tryCatch(
        {
            foo()
        },
        error = function(e){
            bar()
        }
    )
    
    x
    # [1] "world"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多