如何在 Python 中捕获 rpy2.rinterface.RRuntimeError?
How do I catch an rpy2.rinterface.RRuntimeError in Python?
设置:
- 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 returns 出现以下错误:
rpy2.rinterface.RRuntimeError: Error in integrate(PIntegrand, lower =
0, upper = Inf, lambda, vbar, eta, : roundoff error was detected
如何在 Python 中捕获此 RuntimeError?
Python 使捕捉异常变得相对容易。
try:
# some code
except Exception, e:
# Log the exception.
RRuntimeError
派生自 Exception
,因此您应该能够像处理任何其他异常一样捕获它。
try:
# your code
except rpy2.rinterface.RRuntimeError:
# handle exception
在 rpy2 v3.0 及更高版本中,RRuntimeError
似乎已被移动到其他地方(参见示例代码 from documentation)因此您可能需要使用改为:
try:
# your code
except rpy2.rinterface_lib.embedded.RRuntimeError:
# handle exception
更多相关信息:https://docs.python.org/3/tutorial/errors.html#handling-exceptions
我无法让 rpy2.rinterface.RRuntimeError
工作,但下面的解决方法对我有用。
正在安装 tryCatchLog
:
%%R
install.packages('tryCatchLog')
library('tryCatchLog')
如果在函数中使用它:
def test_R_error_handling():
%R tryCatchLog( expr = result <- 'a'+2, finally = result <- -1 )
result = ro.globalenv['result'][0]
if result == -1:
print(result)
return
else:
print('\nFunction continued')
return True
这是关于 my post rpy2 错误处理。我希望这可以帮助遇到同样问题的任何人。
设置:
- 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 returns 出现以下错误:
rpy2.rinterface.RRuntimeError: Error in integrate(PIntegrand, lower = 0, upper = Inf, lambda, vbar, eta, : roundoff error was detected
如何在 Python 中捕获此 RuntimeError?
Python 使捕捉异常变得相对容易。
try:
# some code
except Exception, e:
# Log the exception.
RRuntimeError
派生自 Exception
,因此您应该能够像处理任何其他异常一样捕获它。
try:
# your code
except rpy2.rinterface.RRuntimeError:
# handle exception
在 rpy2 v3.0 及更高版本中,RRuntimeError
似乎已被移动到其他地方(参见示例代码 from documentation)因此您可能需要使用改为:
try:
# your code
except rpy2.rinterface_lib.embedded.RRuntimeError:
# handle exception
更多相关信息:https://docs.python.org/3/tutorial/errors.html#handling-exceptions
我无法让 rpy2.rinterface.RRuntimeError
工作,但下面的解决方法对我有用。
正在安装 tryCatchLog
:
%%R
install.packages('tryCatchLog')
library('tryCatchLog')
如果在函数中使用它:
def test_R_error_handling():
%R tryCatchLog( expr = result <- 'a'+2, finally = result <- -1 )
result = ro.globalenv['result'][0]
if result == -1:
print(result)
return
else:
print('\nFunction continued')
return True
这是关于 my post rpy2 错误处理。我希望这可以帮助遇到同样问题的任何人。