Using R — Basic error Handing with tryCatch()

本文详细解析了R语言中的错误处理机制,包括警告、错误、finally块的使用方法,并通过实例展示了如何在R中实现类似于Java try-catch-finally结构的异常处理流程。重点介绍了tryCatch函数的用法及其在生成警告和错误信息、提供替代返回值、修改警告和错误消息、以及抑制警告信息等方面的应用。
This entry is part 4 of 14 in the series Using R

The R language definition section on Exception Handling describes a very few basics about exceptions in R but is of little use to anyone trying to write robust code that can recover gracefully in the face of errors. In fact, if you do a little searching you will find that quite a few people have read through the ?tryCatch documentation but come away just as confused as when they started. In this post we’ll try to clarify a few things and describe how R’s error handling functions can be used to write code that functions similarly to Java’s try-catch-finally construct.

List of error handling functions

Without any simple documentation on the subject, the first thing we need is a list of the functions involved in error handling. With this list in hand we can then start up R and type ?function_of_interest to read associated documentation or function_of_interest [without the '()'] to see how the function is implemented. Here is a minimal list of functions that anyone writing error handling code should read up on:

  • warning(…) — generates warnings
  • stop(…) — generates errors
  • suppressWarnings(expr) — evaluates expression and ignores any warnings
  • tryCatch(…) — evaluates code and assigns exception handlers

Other functions exist that relate to error handling but the above are enough to get started. (The documentation for these functions will lead to all the other error-related functions for any RTFM enthusiasts.)

R does try-catch-finally differently

In case you hadn’t noticed, R does a lot of things differently from most other programming languages. Java and Python and C and all other languages covered in Wikipedia’s excellent page on Exception handling syntax use language statements to enable try-catch-finally. R, needing to be different, uses a function.

But the tryCatch() function actually looks a lot like other languages’ try-catch syntax if you format it properly:

In tryCatch() there are two ‘conditions’ that can be handled: ‘warnings’ and ‘errors’. The important thing to understand when writing each block of code is the state of execution and the scope. Excerpting relevant text from the ?tryCatch documentation:

What this means is that ‘expr’ is evaluated a line at a time until a ‘condition’ is encountered and then execution is transferred to the handler with the state in tact. Code can often explain more than words and the example at the end of this post is a standalone R script that explores various features that might be required in a robust error handling system:

  • generating warnings and errors from within a function
  • setting warning and error handlers with tryCatch()
  • providing alternative return values when a function generates a warning or error
  • modifying the text of warning and error messages
  • suppressing warning messages

Just copy and paste the script at the end, make it executable and try it out with the following commands:

Pay special attention to what happens with ‘suppress-warnings’.

The are a couple of take home messages that result from this experimentation:

  1. tryCatch() isn’t that hard to use (Once  you know how!)
  2. warning() and stop() messages are accessible to the condition handlers.
  3. Do appropriate type conversion before passing arguments to functions.
  4. Ideally, the tryCatch() expression should be a single function.

And here is the tryCatch.Rscript example script. Happy error handling!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值