Table of contents
1.
Introduction
2.
How to Create and Execute an R file in R Studio
2.1.
Creating an R file
2.1.1.
By using the File Tab
2.1.2.
By using the Plus Button
2.2.
Example of Writing Scripts
2.2.1.
Code
2.2.2.
Output
2.2.3.
Explanation
2.3.
Saving an R file
2.4.
Execution of an R file
2.4.1.
By using the Run Command
2.4.2.
By using the Source Command
2.4.3.
By using the Source with Echo Command
2.5.
Run Command vs Source Command
3.
Frequently Asked Questions
3.1.
How do you create a new R script file in R Studio?
3.2.
How can you run an R script in R studio?
3.3.
How do you save your R file in R Studio?
3.4.
How do you install and load R packages in an R script?
3.5.
Can we pass command-line arguments to an R script in R Studio?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Creating and executing an R file in R studio

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Do you know how to create and execute an R file in R studio? If not, then you are at the right place. In this blog, we will learn about the creation and execution of an R file in R studio.

Creating and executing an R file in R studio

Below is the process of creation and execution of an R file in R studio.

How to Create and Execute an R file in R Studio

Creating an R file

R is a programming language, and R Studio is an integrated development environment (IDE) specially designed for working with R. IDE is a platform where you can write your quotes and see the variables and outputs that are generated during the programming.

Let's discuss how to create an file in R Studio. So you can create an R file in R studio in two ways. 

By using the File Tab

Below are the steps to create an R file.

  • The first way to create an R file is that you first open your R Studio and click on the File tab.
     
  • After clicking, it will give you a  drop-down menu. There you will see that a new file option is available; click on that and then R script.
     
  • Now, you will get a new file open, and your R file will be created.
By using the File Tab

By using the Plus Button

Below are the steps to create an R file.

  • The second way to create an R file is that you go to the R Studio and click on the plus button, which is below the file tab.
     
  • Then choose the R Script to open a new R script file.
     
By using the plus button


Once you open an R file, the R Studio environment will look like below.

R studio environment


In the above image, there are four panels, Scripts, Environment/History, Files/Plots, and Console. Now you are all set up and ready to write your script file.

Example of Writing Scripts

Now we are going to learn how we can write a script or code in an R file. Below is an example script.

example image


Code

a = 20
b = a*5
print(c(a,b))


Output

Output

Explanation

In the above image example, we assigned a variable “a” with a value of 20 and a variable “b” which is five times of “a” value. Here, the code finds the third statement's value, print(c(a,b)). This statement will concatenate the a and b and print the result, which is 100. So, in this way, a script file is written in R.

Saving an R file

Let's see how we can save an R file in R Studio. First, you go to the File tab, where you can choose either Save or Save as button. If you choose the Save button, it will save the file automatically.

Saving an R file

 

If you use the Save as a button, so, this will pop out a window where you can rename your script file. Then by clicking on the save button, you can save your script file.

Pop out window

Execution of an R file

There are many ways by which you can execute your commands that are available in the R file.

By using the Run Command

A "Run" command is available at the top right corner. You can execute your R file by clicking o the "Run" button. It will execute the line in which the cursor is there. Instead of all this, you can simply use the shortcut key "Ctrl+Enter".

By using the run command

By using the Source Command

Click on the "Source” command, which is available just beside the “Run” command. This will print the output along with the command. Use the shortcut key “Ctrl+Shift+S” to execute it.

By using the Source command

By using the Source with Echo Command

Click on the “Source with Echo” button, which is available on the “Source” command. This will print the output along with the command. Use the shortcut key “Ctrl+Shift+S” to execute it.

By using the Source with Echo command

Run Command vs Source Command

You can use any command “Source” or “Run”. Both methods can execute R files effectively.

  • The run command is specific to R studio. It allows you to execute the selected lines of R code.
     
  • Source and source with echo allow you to run the whole file.
     
  • By using the Run command, you can debug or troubleshoot the program.
     
  • The source reads the R code from the file and evaluates it in the current R session.

Frequently Asked Questions

How do you create a new R script file in R Studio?

To create a new file, go to “file” -> “new file” -> “R Script” or use the shortcut Ctrl+Shift+N.

How can you run an R script in R studio?

By clicking on the “Run” button, you can run an R script in R studio or use the shortcut Ctrl+Shift+S.

How do you save your R file in R Studio?

To save your R script file, go to “File” -> “Save” or use the shortcut Ctrl+S. In this way, you can save your R script file.

How do you install and load R packages in an R script?

You can use the ‘install.packages()’ function to install an R package.

Can we pass command-line arguments to an R script in R Studio?

Yes, we can pass the command-line arguments to an R script in R Studio.

Conclusion

In this article, we discussed the creation and execution of an R script in R Studio. We hope this blog helped you to enhance your knowledge of R Studio. 

Check the below articles to know more.

And many more on our platform Coding Ninjas Studio.
 

If you are a beginner and want to test your coding ability, check out the mock test series.

If you have just started your learning process and are looking for questions from tech giants companies like Amazon, Microsoft, Uber, etc. Take a look at the problemsinterview experiences, and interview bundles for placement preparations.

However, consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass