Save & Load RData Workspace Files in R (3 Examples)


This article shows how to save and load data from and to R. In the article, I’ll show in three examples how to:

Let’s dive in!


Example 1: Save & Load Whole Workspace (save.image Function)

Example 1 shows how to save and load all data files that are stored in the R environment. Before we can start with the example, let’s create some simple data objects:

data_1 <- c(4, 1, 8, 10, 15)           # Create simple example data
data_2 <- 5                            # Create another data object
data_3 <- "Hello R User"               # Create a third data object

Our example data objects are called data_1, data_2, and data_3. The execution of the previous syntax stores all of them in our R (or RStudio) environment.

We can now use the save.image function to save all these data to a working directory on our computer:

# Save whole workspace to working directory
save.image("C:/ ... Your Path ... /all_data.RData")

If you open your working directory after executing the previous code, you should find an RData file which looks as follows:


Working Directory with Saved RData File

Figure 1: Working Directory with Example RData File.


This file can be loaded back to R with the load function at a later point:

# Load workspace back to RStudio
load("C:/ ... Your Path ... /all_data.RData")

The data should appear at the top right of your RStudio (i.e. in the Global Environment panel) once you’ve execute the previous R code:


global environment in RStudio

Figure 2: Global Environment with Reloaded Example Data.


Example 2: Save & Load Multiple Data Objects (save Function)

Sometimes we might want to save only several data objects of our R workspace (e.g. due to memory limitations). In such a case we can use the save function to store only the data files that we want to keep:

# Save multiple data objects to working directory
save(data_1, data_2,
     file = "C:/ ... Your Path ... /data_1_and_2.RData")

With the previous R syntax we saved data_1 and data_2 to our working directory and with the following code we can load the two data objects as follows:

# Load workspace back to RStudio
load("C:/ ... Your Path ... /data_1_and_2.RData")


Example 3: Save & Load a Single Data Object (saveRDS Function)

We can even save only a single data object to our PC. Either, we can use the code of Example 1 and specify only a single argument within the save command; Or we can use the saveRDS function, which provides further flexibility when saving single data objects:

# Save single data object to working directory
        file = "C:/ ... Your Path ... /single_data_object.RData")

saveRDS also creates an RData file in the currently used folder on your computer. However, in order to reload RData filed created by saveRDS it is advantageous to use the corresponding readRDS function in order to read the data back to R:

# Load workspace back to RStudio
data_1_reloaded <- readRDS("C:/ ... Your Path ... /single_data_object.RData")
# 4  1  8 10 15

As you can see based on the previous R code, the readRDS package allows to rename a data object during the data import (in our case we used the new name data_1_reloaded).

Note that we can apply the methodology of this tutorial to any R data type we want. We used vectors in the previous examples. However, the same code could be applied to object types such as matrix, list, data.frame, array and so on…


Further Resources for Exporting & Importing Data from and to R

There are many different ways to export and read data from and to R. Reading / writing excel and csv files is probably the most common way. If you want to learn more about that, I can recommend the following video of thenewboston YouTube channel:



In addition, you may want to have a look at some of the other R programming tutorials on this website. I have published several R tutorials on reading and exporting data already:

In this article, you have learned how to import and export RData R files. Let me know below, in case you have further questions or comments.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.