R语言tryCatch使用方法:判断Warning和Error
1. 出门掉坑里
因为一些情况,需要判断一行命令运行的状态,然后再做出反应,整体来说:
- 是否出现warning,出现了怎么处理?
- 是否出现Error,出现了怎么处理?
- 没有出现怎么处理?
R中判断warning和error状态的函数,我没有找到。一个玩Java
的同事说,try.....catch
多经典的方法,你怎么不用呢?
我知道R中有
tryCatch
这个函数,但是没有具体用过。我一直把R语言当作科学计算语言,没有当成编程语言,伪程序员的气质从没有用过tryCatch
表现出来了。当然,我不会说这是我的问题,我会甩锅,都是R语言
的锅,它根本不是编程语言……。
吐槽完毕,我还是要从编程的角度学习
R语言
,正所谓脑子一堆粪,看什么都是粪
说的就是我啊,一直把R语言当做高级计算器,怎么能知道R语言的编程功能呢!
。既然已经用到了,那我就研究一下它的用法,然后用个示例,解释一下它的用法,希望对后来者有用。
2. ***怎么用?
tryCatch
的整体逻辑:
tryCatch({ 命令 }, warning = function(w){ # 这里是出现warning状态时,应该怎么做,可以用print打印出来,可以执行其它命令 }, error = function(e){ # 这里时出现Error状态时,应该怎么做,可以用print打印出来,也可以执行其它命令 },finally = { # 这是运行正常时,应该怎么做,可以用print打印出来,也可以执行其它命令 })
示意图如下:
3. 走两步试试?
下面用一个示例,看一下tryCatch
是怎么使用的。
首先看一下R中warning
的报警信息:
warning
# 示例数据 library(data.table) dd = data.frame(ID = 1:10,y= rnorm(10)) dd # warning re1 = dcast(dd,y~.) re1
> re1 = dcast(dd,y~.) Using 'y' as value column. Use 'value.var' to override Warning message: In dcast(dd, y ~ .) : The dcast generic in data.table has been passed a data.frame and will attempt to redirect to the reshape2::dcast; please note that reshape2 is deprecated, and this redirection is now deprecated as well. Please do this redirection yourself like reshape2::dcast(dd). In the next version, this warning will become an error.
可以看到,代码执行成功了,但是会给出Warning message
然后我们看一下Error
的报警信息,假定R中没有aaaaa
的对象,你直接打印出来,会报错:
# error aaaaa
> # error > aaaaa 错误: 找不到对象'aaaaa'
那我们用tryCatch
走两步看看?
warning处理代码
- 在代码中,写下warning的语句,在
warning
的条件下,返回2
- 在代码中,写下error的语句,在
error
的条件下,返回3
re1 = tryCatch({ dcast(dd,y~.) # warning # aaaaa # error },warning = function(w){ 2 print("warning") },error = function(e){ 3 print("error") }) re1
结果如下:
> re1 = tryCatch({ + dcast(dd,y~.) # warning + # aaaaa # error + + },warning = function(w){ + print("warning") + 2 + + },error = function(e){ + print("error") + 3 + }) [1] "warning" > re1 [1] 2
可以看到,因为dcast(dd,y~.) # warning
返回warning
,所以会执行function(w){}
的内容,即re1 = 2
,同时打印出warning
.
error处理代码
如果我们执行error
的代码,那么他就re1=3
,同时打印出error
re1 = tryCatch({ # dcast(dd,y~.) # warning aaaaa # error },warning = function(w){ print("warning") 2 },error = function(e){ print("error") 3 }) re1
结果如下:
> re1 = tryCatch({ + # dcast(dd,y~.) # warning + aaaaa # error + + },warning = function(w){ + print("warning") + 2 + + },error = function(e){ + print("error") + 3 + }) [1] "error" > re1 [1] 3
可以看到,打印出了error
的警告,同时,re1赋值为了3
。
4. 意料之外的判断
我有一个大胆的想法,如果一个程序,既有warning
,又有error
怎么办?
一般情况下,是不会存在这种情况的,但是如果有,R语言是如何处理的呢?
看示例:
melt这个函数,运行melt(dd,y~.)
会报错,同时,因为这个函数在data.table
用法有变化,因此,也会给出warning
信息。
> # 既有warning,又有error时,以warning为主 > melt(dd,y~.) Error in match(x, y, 0L) : 'match'需要矢量参数 此外: Warning message: In melt(dd, y ~ .) : The melt generic in data.table has been passed a data.frame and will attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(dd). In the next version, this warning will become an error.
这个时候,如果用tryCatch
,它会捕捉到warning
,而不是error
代码:
re1 = tryCatch({ # dcast(dd,y~.) # warning # aaaaa # error melt(dd,y~.) },warning = function(w){ print("warning") 2 },error = function(e){ print("error") 3 }) re1
运行结果:
> re1 = tryCatch({ + # dcast(dd,y~.) # warning + # aaaaa # error + melt(dd,y~.) + + },warning = function(w){ + print("warning") + 2 + + },error = function(e){ + print("error") + 3 + }) [1] "warning" > re1 [1] 2
所以呢,要注意上面的情况,有时候warning
和error
同时存在时,warning
的优先级是靠前的。
5. 顺利上岸
上岸过程见3. 走两步试试?
6. 被借鉴的文章
http://www.mazamascience.com/WorkingWithData/?p=912
https://sarahpenir.github.io/r/Warning-and-Error-Handling-with-trycatch/
7. 技术创作101训练营
因为参加了技术创作101训练营
,要写一篇技术文档,所以这是一篇现爬现卖
的应试文章。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
机器学习 | R语言中的方差分析汇总
方差分析,是统计中的基础分析方法,也是我们在分析数据时经常使用的方法。下面我总结一下R语言如何对常用的方差分析进行操作。 1. 方差分析的假定 上面这个思维导图,也可以看出,方差分析有三大假定:正态,独立和齐次,如果不满足,可以使用广义线性模型或者混合线性模型,或者广义线性混合模型去分析。 本次我们的主题有: 2. 数据来源 这里,我们使用的数据来源于R包agridat,它是讲农业相关的论文,书籍中相关的数据收集在了一起,更加符合我们的背景。 包的下载地址:https://cran.r-project.org/web/packages/agridat/index.html 包的介绍 agridat: Agricultural Datasets Datasets from books, papers, and websites related to agriculture. Example graphics and analyses are included. Data come from small-plot trials, multi-environment trials, unif...
- 下一篇
Win7 x64 Eclipse无法识别手机 / adb interface有黄色感叹号,无法识别
今天公司停电,因此把安卓项目带回宿舍做。宿舍的笔记本,装的是Win7 x64,手机连上电脑后,windows可以识别,但Eclipse的DDMS中却无法识别,什么都没有: 然后打开设备管理器查看,发现windows能正常识别设备(HUAWEI C8817E),而ADB Interface(有的机器可能显示Android ADB什么的)却显示有黄色感叹号,是驱动程序没有正常安装: 那怎样正确安装ADB Interface的驱动程序呢?折腾了一个晚上加一个上午,还是没办法,什么system32目录、sysWOW64目录、winusb.sys等等都试过了,都不起作用。 但是公司那台机上32位的Win7 倒是能正常安装驱动的。 吃过中饭昏昏欲睡的状态下,继续漫无目的地百度,无意中就找到了一篇文章,解决了问题: 引用自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=78707&id=4397858 步骤如下: 1. 在设备管理器中,找到ADB Interface,右击,选择“更新驱动程序软件” 2. 在弹出的窗口中...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8编译安装MySQL8.0.19
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS关闭SELinux安全模块
- SpringBoot2整合Redis,开启缓存,提高访问速度