编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

如何在Python中捕获rpy2.rinterface.RRuntimeError?:How do I catch an rpy2.rinterface.RRuntimeError in Python?

Vivek Srinivas Python 2022-5-11 10:56 5人围观

腾讯云服务器
如何在Python中捕获rpy2.rinterface.RRuntimeError?的处理方法

设置:

  • Python 3.5.3 | Continuum Analytics,Inc. |(默认值,2017年3月6日,12:15:08)
  • Mac OSX 10.13.1

问题:

我已经下载了以下R脚本 https://github.com/daleroberts/heston/blob/master/heston.r 然后我使用RPy2软件包通过Python反复调用了其中的一个函数.现在,对于我输入到R函数中的某些输入,R返回以下错误:

I have downloaded the following R script https://github.com/daleroberts/heston/blob/master/heston.r and I repeatedly call one of the functions in there via Python using the package RPy2. Now, for some of my inputs that I feed into the R function, R returns the following error:

rpy2.rinterface.RRuntimeError:集成错误(PIntegrand,lower =0,上层= Inf,lambda,vbar,eta ,:检测到舍入错误

rpy2.rinterface.RRuntimeError: Error in integrate(PIntegrand, lower = 0, upper = Inf, lambda, vbar, eta, : roundoff error was detected

如何在Python中捕获此RuntimeError?

How do I go about catching this RuntimeError in Python?

问题解答

RRuntimeError 源自 Exception ,因此您应该能够像捕获任何异常一样捕获它其他例外.

The RRuntimeError is derived from Exception so you should be able to catch it as you would with any other exception.

try: # your code except rpy2.rinterface.RRuntimeError: # handle exception 

在rpy2 v3.0及更高版本中, RRuntimeError 似乎已移至其他位置(请参见示例代码


In rpy2 v3.0 and above, RRuntimeError seems to have been moved elsewhere (see example code from documentation) so you may need to use this instead:

try: # your code except rpy2.rinterface_lib.embedded.RRuntimeError: # handle exception 

有关此内容的更多信息: https://docs.python.org/3/tutorial/errors.html#handling-exceptions

这篇关于如何在Python中捕获rpy2.rinterface.RRuntimeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^