【发布时间】:2021-06-09 06:54:35
【问题描述】:
尝试 catch 块没有处理错误并且错误块甚至没有进入错误块
错误:手动刻度中的值不足。需要 3 个,但只有 2 个 提供。
c("#B71D48","#ECE189") -> colors_set
tryCatch({
iris %>%
ggplot(aes(x=Petal.Length, y=Petal.Width, color=Species))+
geom_point()+
scale_color_manual(values = colors_set)
},error = function(e){
#error handling code
print("inside error")
ggplot() +
theme_void() +
geom_text(aes(0,0) ,
label = "No data",
size=20)+
theme(plot.background = element_rect(fill = "white",color = "#ffbf00", size=7))
})
【问题讨论】:
-
代码在我的环境中运行得非常好导致“无数据”图。
-
您不需要创建在 {shiny} 中显示错误消息的图。您可以使用
validate/need为您执行此操作。参见例如here。 -
绘图仅在打印时构建。该图未打印在您的
tryCatch中,因此tryCatch无法处理构建图期间的错误。