tryCatch没有捕获错误并跳过错误参数

Roc*_*nce 5 r try-catch r-base

我注意到下面的错误没有被tryCatch正确捕获:它没有打印TRUE,它没有去浏览器...

它可能是tryCatch函数中的错误吗?

library(formattable)
df1 = structure(list(date = c("2018-12-19", "2018-12-19"), 
                     imo = c(9453391, 9771298), 
                     name = c("SFAKIA WAVE", "MEDI KYOTO"), 
                     speed = c(10.3000001907349, 11.6999998092651), 
                     destination = c("ZA DUR", "ZA RCB"), 
                     subsize = c("Post Panamax", "Post Panamax"), 
                     eta = c("2018-12-27 09:00:00", "2018-12-27 09:00:00"), 
                     ToSAF = c(TRUE, TRUE)), 
                .Names = c("date", "imo", "name", "speed", "destination", "subsize", "eta", "ToSAF"), 
                row.names = c(NA, -2L), 
                class = "data.frame")

tryCatch(expr = {
  L = list(formattable::area(row = 3)  ~ formattable::formatter('span', style = x ~ formattable::style(display = 'block', 'border-radius' = '4px', 'padding-right' = '4px')))
  formattable::formattable(df1, L)
  }, 
  error = function(e) {
    print(TRUE)
    browser()
  } 
)
Run Code Online (Sandbox Code Playgroud)

pie*_*eca 4

计算表达式时没有错误formattable::formattable(df1, L)。您可以通过运行来验证:

L <- list(formattable::area(row = 3)  ~ formattable::formatter('span', style = x ~ formattable::style(display = 'block', 'border-radius' = '4px', 'padding-right' = '4px')))
test <- try(formattable::formattable(df1, L))
class(test)
[1] "formattable" "data.frame" 
Run Code Online (Sandbox Code Playgroud)

如果出现错误,该类应该是"try-error". print当您尝试将表达式输出到控制台时,会出现该错误。我想你想要:

L <- list(formattable::area(row = 3)  ~ formattable::formatter('span', style = x ~ formattable::style(display = 'block', 'border-radius' = '4px', 'padding-right' = '4px')))
test <- formattable::formattable(df1, L)
tryCatch(expr = {
  print(test)
  }, 
  error = function(e) {
    print(TRUE)
    browser()
  } 
)
Run Code Online (Sandbox Code Playgroud)