Joschi
Joschi

Reputation: 3039

Load and save single objects to workspaces in R/R-Studio

I like the idea of working with workspaces. So far I always saved entire workspaces and loaded them entirely to an existing project. But a lot of times I only need single objects from a specified workspace. Is there a possibility to load them seperatly from another workspace.

Also sometimes it would be nice to add an object to an already existing workspace. Imagine for example you have five huge scripts with seperatly huge workspaces and you don't want to mix them up to have them all in one workspace. So now you want to store only the clean results from each of the five worspaces to another clean workspace...

So theses are the basic tasks:

# save entire workspace
save.image("mypath/myworkspace")

# load entire workspace
load ("mypath/myworkspace")

# save a single object (or several)
save (myobject,file="mypath/myworkspace")

# load a single object from an existing workspace
?

# add a single object to an existing workspace
?

Upvotes: 10

Views: 16293

Answers (2)

user974514
user974514

Reputation: 552

# load a single object from an existing workspace

You can't restore one object from the workspace. Since save.image(".Rdata") is just a simplified command for save(list = ls(all=TRUE), file= ".RData"). When you use load command you restore all objects from that list.

However you can save couple objects merged in a list and then load them. For example:

library(ggplot2)
c <- ggplot(mtcars, aes(factor(cyl)))
d<-c + geom_bar(width=.5)  # create two graphs 
c<- c + geom_bar()
save(list=c("c","d"),file="myobjects") # save them (notice that objects are accessed as strings)
rm(list = ls()) # remove from the memory
load(file="myobjects") # load again

You now have your objects (plots) c and d back. That answers your last question.

Now, suppose you already have objects c and d in file myobjects and you want to add more objects to this file. Without loading it it's impossible, since save and saveRDS stores data compressed (in case of save you get tar package and in saveRDS you get to choose compressing method). As you know you cant add data to already archived files without unpacking it. The only solution I see is this. Suppose we want to add a and b to myobjects.

library(ggplot2)
a<-qplot(rnorm(100))
b<-qplot(rnorm(200))
list=ls()
list<-list[-which(list%in%c("a","b"))] # list all variables except the one you want to save
rm(list=list) # we're deleting all except a and b
load(file="myobjects") # loading or unpacking objects c and d
save(list=ls(),file="myobjects") # saving objects a,b,c,d in myobjects file

This is a rough workaround, however if you think about it, in R we got either data or plot objects (i used ggplot2 examples for reason). Data can be saved as save.table, plots can be stored to list of grobs (package gridExtra) and then saved with save.

Upvotes: 3

GSee
GSee

Reputation: 49820

RStudio calls your globalenv() your "workspace"

You can load .RData files into environments other than your globalenv()

x <- 1; y <- 2 #First, create some objects
save.image()  # save workspace to disk
rm(list=ls()) # remove everything from workspace
tmp.env <- new.env() # create a temporary environment
load(".RData", envir=tmp.env) # load workspace into temporary environment
x <- get("x", pos=tmp.env) # get the objects you need into your globalenv()
#x <- tmp.env$x # equivalent to previous line
rm(tmp.env) # remove the temporary environment to free up memory

Once an object is in your globalenv(), it will show up in RStudio's "Workspace" tab.

Similarly, you can assign objects into the environment.

tmp.env <- new.env()
load(".RData", envir=tmp.env) # load workspace into temporary environment
assign("z", 10, pos=tmp.env)
#tmp.env$z <- 10 # equivalent to previous line

Now, you can save all the objects in tmp.env if you tell save where they are located.

save(list=ls(all.names=TRUE, pos=tmp.env), envir=tmp.env, file="test.RData")
rm(tmp.env)

You have effectively added an object, z, to the workspace stored in test.RData.

rm(list=ls(all.names=TRUE))
load("test.RData")

> ls()
[1] "x"       "y"       "z"  

Upvotes: 17

Related Questions