When we want to export data out of R there are 2 primary ways to save objects: Saving single objects to a file or saving multiple objects to a file that we can open with R.

Saving single objects

Often we need to work with data in multiple different programs. Say you import a spreadsheet into R, do some type of manipulation, and then want to view it in excel. The same goes for wanting to export an object to view in GIS software, a visualization to put in a publication, etc etc

To save objects, we use the write family of functions.

In the same fashion that we read data into R, we will write data out of R. There are a wide variety of write functions that we use for different types of data. Here I will show you how to use the write.csv() function.

write.csv() does exactly what you think. It writes an object out of R into a .csv file that you can very easily open up in your favorite spreadsheet viewing software.

Lets take that same data frame we read into R, and now lets write it into our working directory and name it exported_df.csv

write.csv(df,file = 'exported_df.csv')

The function above simply takes the dataframe df, and writes it out as the file exported_df.csv. Note that you need to add the file extension, in this case, .csv, in order for the file to export correctly.

There are a wide variety of write functions suited for different types of data. I have included a few examples below. Note that many of these will require packages that are noted in the comment.

write.tree() #Writes phylogenetics tree from APE package
write.FASTA() #Writes a FASTA file from the APE package
st_write() #Writes a variety of spatial objects from the SF package
write_delim() #Writes out a table not using a comma delimiter from the reader package

Saving multiple objects

What if you want to save multiple objects at once?

Let’s say after mastering the material in this course, you create an advanced series of r scripts for your research. You might have started with a single data set, but now you have multiple data objects, results from analyses, and various visualizations that you don’t want to rerun the next time you open up R.

This is where we will save and load .Rdata files

.Rdata files can contain many different objects. The chunk below creates many different objects.

num_val <- 2 #A single numeric value
char_val <- "frog" # A single character value
num_vect <- c(1,2,3,4,5) # a vector of numbers
df <- data.frame(col_1 = c(1,2,3,4,5), #A dataframe of 3 columns and 5 rows
                 col_2 = c('a','b','c','d','e'),
                 col_3 = c(22.4,23.7,34.2,12.3,45.3)) 
list <- list(num_val,char_val,num_vect,df) #a list that contains all the previous objects

What we can do, is place all of these different objects into a single .Rdata file which we can load later. We do this by using the save() function. Simply list out every object we want to save and then include a file name with the file argument.

save(df,list,char_val,num_val,num_vect, file = "ExampleRfile.Rdata")

Now if we want to load that same data in the future, we simply use the load() function. Lets clear the data in the environment using the next chunk to demonstrate this.

rm(list = ls()) #this nifty command removes everything in the environment

Now lets load the data back in using the load() function

load('ExampleRFile.Rdata')

Simple right? It is good practice to get in the habit of saving and loading collections of R objects after major steps in your analysis. This will help keep your code organized and your code more reproducible!

Last modified: Tuesday, 2 December 2025, 11:38 AM