# 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:

- Save & Load the Whole Workspace (save.image Function)
- Save & Load Multiple Data Objects (save Function)
- Save & Load a Single Data Object (saveRDS Function)

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 |

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") |

# 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:

**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") |

# 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:

**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") |

# 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") |

# 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 saveRDS(data_1, file = "C:/ ... Your Path ... /single_data_object.RData") |

# Save single data object to working directory saveRDS(data_1, 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") data_1_reloaded # 4 1 8 10 15 |

# Load workspace back to RStudio data_1_reloaded <- readRDS("C:/ ... Your Path ... /single_data_object.RData") data_1_reloaded # 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:

- Write XLSX Files from R
- Read Excel File in R
- R Functions List (+ Examples)
- The R Programming Language

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.

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R