在 R 中,我可以使用工作区保存加载的包吗?

发布于 2024-11-30 08:26:19 字数 208 浏览 0 评论 0原文

R 通常只将对象保存在 .GlobalEnv 中:

$ R
> library(rjson)
> fromJSON
function (...) ...
> q(save='yes')
$ R
> fromJSON
Error: object 'fromJSON' not found

有没有办法也保存此信息?

R normally only saves objects in .GlobalEnv:

$ R
> library(rjson)
> fromJSON
function (...) ...
> q(save='yes')
$ R
> fromJSON
Error: object 'fromJSON' not found

Is there a way to have this information saved as well?

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

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

发布评论

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

评论(4

不醒的梦 2024-12-07 08:26:19

现在,您可以将 R 会话信息保存到文件中并将其加载到另一个会话中。

首先安装“session”包:

install.packages('session')

加载您的库和数据,然后将会话状态保存到文件中:

library(session)
library(ggplot2) # plotting

test <- 100

save.session(file='test.Rda')

然后您可以在另一个会话中加载会话状态:

library(session)

restore.session(file='test.Rda')

#ggplot2 (and associated data) should have loaded with the session data
head(diamonds)
ggplot(data = diamonds, aes(x = carat)) +
  geom_histogram()

print(test)

参考:https://www.rdocumentation.org/packages/session/versions/1.0.3/topics/save.session

You are now able to save R session information to a file and load it in another session.

First install the "session" package:

install.packages('session')

Load your libraries and your data, then save the session state to a file:

library(session)
library(ggplot2) # plotting

test <- 100

save.session(file='test.Rda')

Then you can load the session state in another session:

library(session)

restore.session(file='test.Rda')

#ggplot2 (and associated data) should have loaded with the session data
head(diamonds)
ggplot(data = diamonds, aes(x = carat)) +
  geom_histogram()

print(test)

Reference: https://www.rdocumentation.org/packages/session/versions/1.0.3/topics/save.session

日暮斜阳 2024-12-07 08:26:19

据我所知,没有。工作区用于数据和函数等对象。启动 R 并加载特定的包就是 .Rprofile 文件的用途,并且每个目录中可以有不同的文件。

我想,您可以在工作区中保存一个函数来加载所需的包,然后在第一次启动 R 时运行该函数。

To the best of my knowledge, no. The workspace is for objects like data and functions. Starting R with particular packages loaded is what your .Rprofile file is for, and you can have a different one in each directory.

You could, I suppose, save a function in the workspace that loads the packages you want, and then run that function when you first start R.

攀登最高峰 2024-12-07 08:26:19

joran 是对的,但我想提一下一种技术,虽然很麻烦,但可能会有所帮助。

您可以使用检查点程序(例如 DMTCP)来保存整个 R 进程并稍后重新启动。

joran is right, but I want to mention a technique that, while cumbersome, might be helpful.

You can use a checkpointing program such as DMTCP to save the entire R process and restart it later.

七秒鱼° 2024-12-07 08:26:19

我建议不要在 r 会话之间保存任何内容,而是使用代码重新创建它。这更有可能导致可重复的结果。

I'd recommend not saving anything between r sessions and instead recreate it all using code. This is much more likely to lead to reproducible results.

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