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

时间:2023-01-24 13:33:21

R normally only saves objects in .GlobalEnv:

R通常只保存.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?

有没有办法保存这些信息?

4 个解决方案

#1


3  

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

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

First install the "session" package:

首先安装“session”包:

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

#2


5  

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.

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

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.

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

#3


3  

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.

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

#4


2  

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

joran是对的,但我想提一种虽然繁琐但可能有用的技巧。

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

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

#1


3  

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

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

First install the "session" package:

首先安装“session”包:

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

#2


5  

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.

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

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.

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

#3


3  

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.

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

#4


2  

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

joran是对的,但我想提一种虽然繁琐但可能有用的技巧。

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

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