Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中捕获rpy2.rinterface.rrontimeerror?_Python_R_Exception Handling_Runtime Error - Fatal编程技术网

如何在Python中捕获rpy2.rinterface.rrontimeerror?

如何在Python中捕获rpy2.rinterface.rrontimeerror?,python,r,exception-handling,runtime-error,Python,R,Exception Handling,Runtime Error,设置: Python 3.5.3 | Continuum Analytics,Inc.|(默认,2017年3月6日,12:15:08) Mac OSX 10.13.1 问题: 我已经下载了下面的R脚本 我使用包RPy2通过Python反复调用其中的一个函数。现在,对于输入到R函数中的一些输入,R返回以下错误: rpy2.rinterface.rrontimeerror:集成中出错(PIntegrand,下)= 0,上限=Inf,λ,vbar,eta,:检测到舍入错误 如何在Python中捕获

设置:

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

我已经下载了下面的R脚本 我使用包RPy2通过Python反复调用其中的一个函数。现在,对于输入到R函数中的一些输入,R返回以下错误:

rpy2.rinterface.rrontimeerror:集成中出错(PIntegrand,下)= 0,上限=Inf,λ,vbar,eta,:检测到舍入错误


如何在Python中捕获此运行时错误?

Python使捕获异常相对容易

try:
    # some code
except Exception, e:
    # Log the exception.

rrontimeerror
是从
Exception
派生的,因此您应该能够像捕获任何其他异常一样捕获它

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

在rpy2 v3.0及更高版本中,
rrontimeerror
似乎已被移到其他位置(请参见示例代码),因此您可能需要使用此选项:

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


更多信息:

我无法使
rpy2.rinterface.rrontimeerror
正常工作,但下面的解决方法对我有效

安装
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
def test_R_error_handling():

%R tryCatchLog(expr=result运行时错误在Python中是一个异常。它可能位于路径
rpy2.rinterface.embedded.rrontimeerror
@wassname是的,你可能是对的,我可能只是删除对源代码的引用,因为它最终总是会过时。