【发布时间】:2013-02-12 00:03:10
【问题描述】:
这将是一个很长的问题。所以,请原谅我
我有以下场景,我想最好给个伪代码来更好地解释事情
一个 python 文件说 test.py
def test(i):
from rpy2.robjects import r
r.source('r_file.R')
r.call_function(with some arguments)
#Some Operations
del r
文件:r_file.R
rm(list=ls(all=TRUE))
#some global variables
#some reference class
#creating an object of reference class
call_function = function(some arguments)
{
Do some processing
call few methods on a reference class
call some more methods and do some operations
rm(list=ls(all=TRUE))
gc()
return(0)
}
python 中函数测试的调用发生在 'i' 的某些值上,即函数被调用的 i 的某些值总是大于 1,即函数从 main 中多次调用强>。因此,我们不止一次地获取 R 文件。每次调用 python 函数时,我都想要一个新的 R 解释器。因此,每次调用该函数时我都会导入 r 并删除 rpy2 对象。
在 r 函数“call_function”中,我调用了一些方法,这些方法又创建了引用类对象。
在 R 代码中,我在代码开头以及函数 some_function 退出时使用 rm。
鉴于这种背景,我现在面临的问题是 rm 没有删除代码中的任何引用类,我不断收到这样的警告
In .removePreviousCoerce(class1, class2, where, prevIs) :
methods currently exist for coercing from "Rev_R5" to "envRefClass"; they will be replaced
这里,Rev_R5 是一个参考类。我不希望这种情况发生,有没有办法使用 rm 删除与引用类相关的所有 methods、objects ?
【问题讨论】:
-
这听起来不像您为每次调用都创建一个新的 R 会话 - 这样做将是最简单的解决方法。
-
这正是我想要的。如何使用 rpy2 为每次调用创建一个新的 R 会话?