How to use saveRDS and readRDS method In R Language

The saveRDS and readRDS methods available in base R are basically used to provide a means to save a single R object to a connection, mostly a type of file object, and then to restore the object. The restored object may belong to a different name. This approach is different from the save and load approach, which saves and restores one or more named objects into an environment. It is used to save a single object into the workspace. 

Syntax:

saveRDS(object, file = “”)

Arguments : 

  • object – R object to serialize.
  • file – name of the file where the R object is saved to or read from.

Syntax: 

readRDS(file)

Example: Saving and loading R data workspace  

R




# creating data objects
obj1 <- c(1:5)         
obj2 <- FALSE                      
obj3 <- "w3wiki!!"  
 
# saving all data to the path
saveRDS(obj1, file = "saveworkspaceobj1.RData")
print("Data object1")
 
# loading the workspace
readRDS("saveworkspaceobj1.RData")


 
Output: 

[1] "Data object1" 
[1] 1 2 3 4 5

Save and Load RData Workspace Files in R

In this article, we will discuss how to save and load R data workspace files in R programming language.

Similar Reads

Method 1: Using save.image and load method

The save.image method in R is used to save the current workspace files. It is an extended version of the save method in R which is used to create a list of all the declared data objects and save them into the workspace. These files can then later be read into the corresponding saved data objects using the load() method....

Method 2: Using saveRDS and readRDS method

...

Method 3: Using the save and load method

...