tryCatch未捕获错误并跳过错误参数
我注意到tryCatch没有正确捕获以下错误:它不会打印为TRUE,也不会进入浏览器 这可能是tryCatch函数中的错误吗tryCatch未捕获错误并跳过错误参数,r,try-catch,R,Try Catch,我注意到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"
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()
}
)
计算表达式
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"
发生这种情况的原因: 这是因为传递给
tryCatch()
的expr
实际上不会导致错误。您可以通过以下方式轻松复制:
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
# ...
如何调查:
您还可以在运行代码后运行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)
如何获得您(可能)想要的行为:
现在,问题的解决方案实际上取决于您预期的行为。假设您想得到问题中提到的错误,只需在expr
中包含print()
:
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()
}
)
现在您可以:
# [1] TRUE
# Called from: value[[3L]](cond)
# Browse[1]>
除了@pieca先前的回答之外,我有点不清楚这又增加了什么?
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()
}
)
# [1] TRUE
# Called from: value[[3L]](cond)
# Browse[1]>