Create and Save a Script in R
Last Updated :
09 Jan, 2025
Scripting is a powerful way to save, organize, and reuse your code when working with R. Instead of typing commands interactively in the R console, you can write a series of commands in a script file (.R file) and execute them all at once. Saving your work as an .R script ensures reproducibility and better collaboration, and allows you to revisit and modify your code easily.
In this article, we will guide you through the process of creating, saving, and using .R script files effectively.
What is an .R Script?
An .R script is a plain text file that contains R commands. These files typically have the extension .R. By saving your code in script files, you can:
- Execute multiple commands at once.
- Debug and edit your code conveniently.
- Share your code with others for collaboration or educational purposes.
Creating and Saving an .R
Script File
Follow these steps to create and save an .R
script file:
1. Open RStudio or Your Preferred R IDE
Most R users prefer RStudio for scripting due to its robust features. However, you can use any text editor (such as Notepad, VS Code, or Sublime Text) to write .R
scripts.
2. Create a New Script File
In RStudio:
- Go to the top menu and click on File > New File > R Script.
- A blank script window will open where you can write your R code.
If you're using a text editor:
- Open the editor and start typing your R code.
3. Write Your R Code
Type the commands you want to save. For example:
R
x <- 5
y <- 10
sum <- x + y
print(sum)
4. Save the Script
Once you’ve written your code, save it with the .R
extension.
In RStudio:
- Click on File > Save As.
- Choose a location on your computer.
- Provide a descriptive name for your file, e.g.,
my_first_script.R
. - Ensure the file extension is
.R
.
In a text editor:
- Save the file with the
.R
extension (e.g., analysis_script.R
).
Executing Your .R
Script
After saving your script, you can execute it in R or RStudio. Here’s how:
1. Running the Script in RStudio
- Open the saved
.R
file in RStudio. - Click on the Run button in the top-right corner of the script editor to execute individual lines or the entire script.
Alternatively:
- Highlight the code you want to run and press Ctrl + Enter (Windows) or Cmd + Enter (Mac).
2. Running the Script in the R Console
- Use the
source()
function to execute the script. For example:
source("path/to/your_script.R")
Replace path/to/your_script.R
with the actual file path to your .R
script.
3. Running the Script via Terminal or Command Line
If you prefer to execute your script outside of RStudio:
- Open a terminal or command prompt.
- Use the
Rscript
command to run the script. For example:
Rscript your_script.R
Saving and working with .R script files is an essential skill for anyone using R. It allows you to streamline your workflow, maintain a record of your code, and share your work with others.
Similar Reads
Importing Data in R Script We can read external datasets and operate with them in our R environment by importing data into an R script. R offers a number of functions for importing data from various file formats. In this article, we are going to see how to Import data in R Programming Language. Importing Data in R First, let'
3 min read
Power BI - Create a R Script Visual An R script is different from R visuals. It helps transform our data into statistical analysis and machine learning algorithms on our datasets. It creates beautiful charts, with statistical measures. To make R Script Visual, we need to have some prerequisites i.e. Installing R in the System, and Che
4 min read
How to Create Tables in R? In this article, we will discuss how to create tables in R Programming Language. Method 1: Create a table from scratch We can create a table by using as.table() function, first we create a table using matrix and then assign it to this method to get the table format. Syntax: as.table(data) Example: I
2 min read
Write DataFrame to SPSS .sav File in R In this article, we are going to see how to write Dataframe to SPSS .sav File in R Programming language. The SPSS Statistics File Format is a proprietary binary format, developed and maintained as the native format for the SPSS statistical software application. To write a data frame in SPSS format i
1 min read
How to Create a Shell Script in linux Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to create a matrix in R In this article, we will discuss What is a matrix and various methods to create a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data set that collects rows and columns. The matrix stores the data in rows and columns format. It is possible to access the data i
3 min read
Saving a Plot in R (With Examples) In this article, we will be looking at the approach to save plots in data objects in R Programming Language. Using recordPlot() functionThis approach is the easiest way to save any type of plot given in the data object form using the recordPlot() function. In this approach, to save the plot in the d
3 min read
Write Data Into Excel Using R In this article, we will discuss how to write data into excel using R Programming Language. To write data into excel,  we must leverage a package called xlsx in R. The excel package is a java based language that is powerful to read, write and update data from and to excel files. Syntax: write.xlsx(
2 min read
How to Read Command Line Parameters from an R Script? Reading command line parameters in an R script is essential for creating flexible and reusable scripts that can handle different inputs without modifying the code. This is particularly useful for automation and batch-processing tasks. This article will guide you through the process of reading comman
3 min read
Saving Graphs as Files in R Graphs are descriptive R objects which facilitate the visual representation of data in R. The data points become more understandable and readable when expressed in the form of plots. The plots can also be saved within the local directory. They can then be accessed without opening the R editor.1. Sav
3 min read