r - 不处理 r 中的 trycatch
问题描述
Try 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))
})
解决方案
将绘图存储为变量并稍后打印有助于 trycatch 捕获错误!没有打印就行不通。
library(tidyverse)
c("#B71D48","#ECE189") -> colors_set
iris %>%
ggplot(aes(x=Petal.Length, y=Petal.Width, color=Species))+
geom_point() +
scale_color_manual(values = colors_set) -> x
tryCatch(print(x), 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))
})
推荐阅读
- loops - 如何在 Ansible 中更新字典
- python - While循环增量位置
- python - 访问字典值的特定字符
- php - PHP搜索栏不返回任何结果
- reactjs - 即使鼠标移动,长按事件也会触发(Ionic React)
- angular - 为什么我们应该在 Angular 单元测试中使用 Jasmine Spy 对象?
- android - 树莓派和android之间通过wifi的两种方式通信
- javascript - 用鼠标创建画布绘制并保存坐标?
- flutter - 为什么导入的小部件未显示在主屏幕颤动中
- c# - 为什么 C# 中的 async/await 会返回可为空的值,即使被告知不要这样做?