Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Many times you might have lost the work you did in R, or you might want to share your work with your peers, but you might have faced some difficulties while doing so. There are many ways to store or save the file in R. One of the most common way of doing so is by saving the output as a CSV file.
In this blog, we will learn how to store the output in different files in R, along with an example, and we will also learn about the importance of saving the work in separate files in R.
What is R?
In the world of data science, we use R mostly for data cleaning or visualisation. It creates stunning visuals that help us to analyse the data very easily and quickly.
It is similar to Python programming language. It is an open-source programming language that is user-friendly and which offers different packages and libraries that help any user to analyse complex datasets.
Importance of Storing Output
Storing output in different files in R has many advantages.
It helps us in efficiently organising the data.
We reduce the risk of losing our work by saving and storing the output.
We can share and save the analysis done on a dataset by storing the output in a file.
R also allows us to save the commands so that we can use those commands anytime need.
How to Save the Output in R
We can save the output in different file types such as CSV, Excel, JSON, RDS and Text Files.
Firstly let us create a small dataset in R containing information about the marks of different students in a class.
# Creating a small dataset
# ’c’ is used to create a vector in R.
students <- data.frame(
Name = c("Raman", "Mahesh", "Atul", "Lara"),
Math = c(90, 85, 92, 88),
Science = c(92, 89, 93, 87),
English = c(88, 91, 84, 90))
# Printing the dataset
print(students)
Output
Now let us look at the different methods to store the students dataset in different file types.
Saving Output as a CSV File
CSV is a plain text file in which data is stored in a tabular format separated by commas. It stands for Comma Separated Values file. CSV saves data in each row in a specific structured pattern.
# Saving students dataset as a CSV file
write.csv(students, file = "students_output.csv")
On running this code, it will create a new CSV file named “students_output” in CSV format in our directory.
Saving Output as an Excel File
In Excel, data is present in the form of a worksheet. Every data value is present in a cell. As the data is stored in tabular format, the Excel file looks more clean and more organised. Excel file has various formatting, data cleaning and manipulation options available, which makes it a good choice for storing and analysing data.
# Saving students dataset as an Excel file
library(openxlsx)
write.xlsx(students, file = "students_output.xlsx")
In this method, firstly, we imported the package named openxlsx and saved the dataset as an Excel file named “students_output.xlsx”.
Saving Output as a Text File
A text file contains data in a paragraph format. Text files can be created and edited using different text editors. Data cannot be efficiently stored in text files as it does not have the option to store tables. It stores only the text contained in the tables.
There are two ways to store the output in R.
Using sink() function
# Specifying the file path
file_path <- "students_output.txt"
# Opening the connection to the text file using sink()
sink(file_path)
# Closing the connection to the text file
sink()
In the code above, firstly, we describe the file path where the dataset will be saved. Then we open the connection to the text file using the sink() function and finally, we close the connection so that now any subsequent change to the data will not be directly saved to the text file.
Using cat() function
# Specifying the file path
file_path <- "students_output.txt"
# Opening the text file in write mode
file_conn <- file(file_path, "w")
# Redirecting the output to the text file using cat()
cat("Name\tMath\tScience\tEnglish\n", file = file_conn)
cat(students, file = file_conn, sep = "\t", append = TRUE)
# Closing the text file connection
close(file_conn)
In the code above, firstly, we specify the file path where our dataset will be saved. Then we open the text file in write mode using file_conn<-file(file_path, “w”). Then by using the cat function, the output will be directly saved in the text file we created above.
Finally, we close the connection so that now any subsequent change to the data will not be directly saved to the text file.
Saving Output as a JSON File
JSON is a file format used for transferring data between a server and a web application. It stands for JavaScript Object Notation. In JSON, data is written in a plain text format in javascript notation. JSON is a lightweight file format and stores a huge amount of data in small sizes.
In this method, we import the jsonlite library first and then save our output in a JSON file named “students_output.json”.
Saving Output as an RDS File
Every R file is saved in RDS file format. Its full form is an R Data Serialization (RDS) file. It helps us to store the work done in R so that we can continue our work later on. In R, data or object created in R is converted into a binary format, which decreases the file size and helps in transmitting data.
# Saving students dataset as an RDS file
saveRDS(students, file = "students_output.rds")
In this method, we use the saveRDS function of R to save our dataset as an R data serialization (RDS) file.
Comparison Chart
Let us look at the comparison chart to analyse different file-saving types in R.
File Format
Advantages
Disadvantages
CSV
It is widely supported on multiple platforms and easily read using different tools.
Formatting options are limited in CSV files.
EXCEL
It is supported everywhere, allows multiple sheets in a single file, and allows easy formatting and manipulation in Excel.
We need to lead a new package named openxlsx while saving the data in an Excel file.
JSON
It has a hierarchical design and supports nested data structures. The size of the JSON file is small compared to others.
It is not easily read as compared to others.
RDS
It is the best method for saving files in R as it saves all the attributes of R.
It cannot be opened on software that does not support the R programming language.
TEXT FILE
Saves the data as plain text. Formatting is easy, and any changes to the data change the text file.
It cannot read tables and other data types, so analysing the data is very difficult.
How to Save the Objects in R
We use the save function in R to save the objects in a binary format. Let us see the example below to understand the method of saving objects in R.
# Creating an example object
my_object <- c("apple", "banana", "orange")
# Saving the object
save(my_object, file = "my_object.Rdata")
When we execute the code, A new file named “my_object.Rdata” is created in our directory.
The format of the file saved is RDS. In our code, a file parameter is used to specify the location of the saved file.
We use the load function to load the file containing the object in R.
# Loading the saved object
load("my_object.Rdata")
print(my_object)
Output
Frequently Asked Questions
What is R programming language?
R is one of the most used programming languages for big calculations and analysing data.
What is R Studio?
R Studio is an open-source Integrated Development Environment (IDE) specially made for the R programming language.
How can we store the output in a different file in R?
We can save the output in different file types such as CSV, Excel, JSON, RDS and Text Files.
Can we save commands in R?
Yes, we can save the commands using the savehistory() function in R.
How can we save objects in R?
We use the save function in R to save the objects in a binary format. The code snippet for saving an object in R is save(my_object, file = "my_object.Rdata").
Conclusion
This article discusses the topic of storing outputs and objects in R. We have discussed how to store the output in different files in R, along with their comparison chart.
We hope this blog has helped you enhance your knowledge about storing outputs and objects in R. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!
Happy Learning!
Live masterclass
Top GenAI Skills to crack 30 LPA+ roles at Amazon & Google
by Sumit Shukla
02 Feb, 2026
03:00 PM
Python + AI Skills to ace 30L+ CTC Data roles at Amazon
by Prerita Agarwal
01 Feb, 2026
06:30 AM
Top 5 GenAI Projects to Crack 25 LPA+ Roles in 2026
by Shantanu Shubham
01 Feb, 2026
08:30 AM
Zero to Data Analyst: Amazon Analyst Roadmap for 30L+ CTC
by Abhishek Soni
02 Feb, 2026
01:30 PM
Top GenAI Skills to crack 30 LPA+ roles at Amazon & Google
by Sumit Shukla
02 Feb, 2026
03:00 PM
Python + AI Skills to ace 30L+ CTC Data roles at Amazon