本文介绍了python rpy2模块:刷新全局R环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rpy2的文档指出,robjects.r对象可以访问R全局环境.有没有办法将这种全球环境刷新"到其初始状态?

The documentation for rpy2 states that the robjects.r object gives access to an R global environment. Is there a way to "refresh" this global environment to its initial state?

我希望能够将全局环境还原到导入但尚未使用rpy2.robjects模块时的状态.通过这种方式,我不必担心长时间运行的作业中的内存泄漏或其他意外的副作用.是的,刷新环境可能会引入不同类别的错误,但是我认为这将是一次成功.

I would like to be able to restore the global environment to the state it was in when the rpy2.robjects module was imported but not yet used. In this manner, I don't have to worry about memory leaks on long running jobs or other unexpected side effects. Yes, refreshing the environment could introduce a different category of bug, but I believe in my case it will be a win.

推荐答案

让您的问题从字面上讲是什么意思,如果您只想清除.GlobalEnv,则只需一行即可:

Taking your question to mean literally what it says, if you just want to clear out .GlobalEnv, you can do that with a single line:

rm(list = ls(all.names=TRUE))

all.names=TRUE位是必需的,因为香草ls()不会返回某些对象名称.例如:

The all.names=TRUE bit is necessary because some object names are not returned by vanilla ls(). For example:

x <- rnorm(5)
ls()
# [1] "x"

# Doesn't remove objects with names starting with "."
rm(list=ls())
ls(all.names = TRUE)
# [1] ".Random.seed"

# Removes all objects
rm(list = ls(all.names=TRUE))
ls(all.names = TRUE)
# character(0)

这篇关于python rpy2模块:刷新全局R环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:10