In R can I save loaded packages with the workspace?

一个人想着一个人 提交于 2019-12-09 18:06:25

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/7115219/in-r-can-i-save-loaded-packages-with-the-workspace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!