Reputation: 18610
I have noticed below error is not catched properly by tryCatch: it doesnt print TRUE, and it doesnt go to the browser...
Could it be a bug in the tryCatch function?
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()
}
)
Upvotes: 5
Views: 465
Reputation: 2747
Why this happens:
This is because the expr
you are passing to tryCatch()
does actually not cause an error. You can easily reproduce by:
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
# ...
What actually is causing your error is that when running the tryCatch
, R will try to print it's result, which causes the error:
print(result)
# Error in `[<-`(`*tmp*`, row, col, value = format(fv)) :
# subscript out of bounds
How you can investigate:
You can also investigate by running traceback()
after you run your code and you will see where the issue comes from:
# 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)
How to get the behavior you (probably) want:
Now the solution to your issue is actually dependent on what is your expected behavior. Assuming you want to get the error you refer to in the question, just include print()
in your 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()
}
)
Now you get:
# [1] TRUE
# Called from: value[[3L]](cond)
# Browse[1]>
Upvotes: 0
Reputation: 2563
There is no error when evaluating expression formattable::formattable(df1, L)
. You can verify by running:
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"
In case of error the class should be "try-error"
. The error appears when you try to print
the output of your expression to the console. I think you want:
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()
}
)
Upvotes: 4