gpt4 book ai didi

r - tryCatch 未捕获错误并跳过错误参数

转载 作者:行者123 更新时间:2023-12-04 10:55:54 26 4
gpt4 key购买 nike

我注意到 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()
}
)

最佳答案

计算表达式 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()
}
)

关于r - tryCatch 未捕获错误并跳过错误参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53866915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com