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

时间:2018-12-20 10:34:03

标签: 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()
  } 
)

2 个答案:

答案 0 :(得分: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" 

如果发生错误,则该类应为"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()
  } 
)

答案 1 :(得分:0)

为什么会这样:

这是因为您传递给expr的{​​{1}}实际上没有引起错误。您可以通过以下方式轻松复制:

tryCatch()

实际上导致您出错的是,在运行result <- 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() } ) str(result) # Classes ‘formattable’ and 'data.frame': 2 obs. of 8 variables: # $ date : chr "2018-12-19" "2018-12-19" # $ imo : num 9453391 9771298 # $ name : chr "SFAKIA WAVE" "MEDI KYOTO" # $ speed : num 10.3 11.7 # ... 时,R会尝试打印其结果,从而导致错误:

tryCatch

如何调查:

运行代码后,您还可以通过运行print(result) # Error in `[<-`(`*tmp*`, row, col, value = format(fv)) : # subscript out of bounds 进行调查,您将看到问题的根源:

traceback()

如何获得(可能)想要的行为:

现在,解决问题的方法实际上取决于您的预期行为。假设您想得到问题中所涉及的错误,只需在# 14: render_html_matrix.data.frame(x, formatters, digits) # 13: render_html_matrix(x, formatters, digits) # 12: format_table(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)), list(formattable::area(row = 3) ~ # formattable::formatter("span", style = x ~ formattable::style(display = "block", # `border-radius` = "4px", `padding-right` = "4px"))), # format = "html") # 11: do.call(attrs$formatter, c(list(value), format_args)) # 10: format.formattable(x, format = list(format = "html")) # 9: format(x, format = list(format = "html")) # 8: gsub("th align=\"", "th class=\"text-", format(x, format = list(format = "html")), # fixed = TRUE) # 7: as.htmlwidget.formattable(x) # 6: as.htmlwidget(x) # 5: print(as.htmlwidget(x), ...) # 4: print_formattable.data.frame(x, ...) # 3: print_formattable(x, ...) # 2: print.formattable(x) # 1: function (x, ...) # UseMethod("print")(x) 中加入print()

expr

现在,您得到了:

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