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]>