python rpy2模块:刷新全局R环境

发布于 2024-12-16 17:37:09 字数 187 浏览 2 评论 0原文

rpy2 的文档指出 robjects.r 对象可以访问 R 全局环境。有没有办法将这个全局环境“刷新”到初始状态?

我希望能够将全局环境恢复到导入 rpy2.robjects 模块但尚未使用时的状态。通过这种方式,我不必担心长时间运行的作业的内存泄漏或其他意外的副作用。是的,刷新环境可能会引入不同类别的错误,但我相信就我而言这将是一场胜利。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-12-23 17:37:09

从字面上理解您的问题,如果您只想清除 .GlobalEnv,您可以用一行来完成:

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

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

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)   

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))

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)   
橪书 2024-12-23 17:37:09

R中只有/一个/“全局环境”;当R启动时它被初始化。正如 Josh 指出的那样,您可以清除其成员,但如果您碰巧需要它,这可能意味着您最好实例化新环境,并在它们之间切换或在不再需要时删除它们。

There is only /one/ "global environment" in R; it is initialized when R starts. You can clear out its members, as Josh points it out, but if you happen to need to it this might mean that you'd better instanciate new environments and either switch between them or delete them when no longer needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文