Welcome to this tutorial on Introduction to R script.
In this tutorial, you will learn
How to work with an R script in RStudio
To understand this tutorial, you should know basic commands of R.
If not, please locate the relevant tutorials on R on this website.
This tutorial is recorded on
Ubuntu Linux OS 16.04
R version 3.2.3
RStudio version 1.1.456
Install R version 3.2.0 or higher.
An R script is a plain text file in which you save your R code
Script allows you to show your work to others, and also reproduce and modify the results
Before you begin please create a folder named myProject on Desktop.
I have already created this folder on my Desktop.
I shall now save all my scripts and data sets in this folder.
Later, I shall set this folder as my Working Directory.
Let us switch to RStudio.
We will close these two tabs named Untitled1 and R packages available.
Click on the small cross symbol next to these tabs
We will now create a new R script.
In the top left corner of RStudio, click on the green plus sign.
Now from the dropdown menu, select R Script.
A new R script appears in the Source window.
You can also create a new R script by pressing Ctrl+Shift+N together.
Let us save this R script first.
In the top left corner of the screen, click on the File option.
Then click on Save to save the script.
Select the directory where you want to save your R script.
I am saving the script in the folder myProject on my Desktop, with the name myfirstscript.
Finally, click on the Save button.
Now you can see that the file is named as myfirstscript.R.
RStudio automatically adds dot R at the end of the filename.
Now, let us write some lines of code in the script file myfirstscript.R.
We will create a vector named testSample with one two three four as its elements.
We have already learned how to create vectors earlier in this series.
In the Source window, type testSample space less than symbol. Now type hyphen space c 1 comma 2 comma 3 comma 4 in parentheses. Press Enter
You may also use equal sign in place of less than symbol followed by hyphen.
In the Source window, to the left of the checkbox Source on Save, click on the Save icon Or, press Ctrl+S together to save the file.
We will now use the print function to display the values in testSample vector.
In the Source window, type p r i
RStudio automatically starts suggesting different functions.
You can navigate this list of functions by using the up (↑) and down (↓) arrow keys on the keyboard.
To select a particular function, just press Enter.
Finish typing print testSample in parentheses and press Enter.
Now let’s save the file and run it with these two lines only.
Now, we maximize the Source window.
In the top right corner of the Source window, click on the Source button.
You can see the line-by-line execution of the script in the Console.
You can also run the script by pressing Ctrl+Shift+Enter together.
For this, you need to first click in the script window.
Now let’s declare a variable firstVar with value of ten in the Source window.
Type firstVar with capital V space equal sign space 10 and press Enter.
Please note that R language is case-sensitive.
We will now use the print function to show the value of firstVar.
In the Source window, type print.
I am typing only p r i n but the options appear and I select print from there.
Now in parentheses, start typing firstVar.
RStudio shows firstVar; press Enter to select it.
Press Enter to go to the next line.
Now let’s declare another variable secondVar with value of fifteen.
In the Source window, type secondVar space equal sign space 15 and press Enter.
Now, as shown before, type print secondVar in parentheses to show its value and press Enter
We can add comments to our code by using # hash symbol followed by our comment.
Type hash symbol space Creating an R script before the declaration of testSample vector.
R interprets each line of code.
Please make sure that your comment and the first line of code appear in separate lines.
Now, let’s comment the line print(secondVar).
To do that, first click on this line.
Then from the top menu bar of RStudio, click on the Code option.
From the dropdown menu, click on the Comment/Uncomment Lines option.
This option helps us to comment as well as uncomment a line.
On doing so, we see a hash symbol with space.
For now, we will uncomment print(secondVar) and save our script.
Delete # hash symbol at the beginning of this line.
Now let’s run the script file.
In the top right corner of the Source window, click on the Source button.
Now maximize the Console.
The line-by-line execution of the script is shown in the Console window.
The sample vector [1 2 3 4] is printed.
The values of two variables, 10 and 15 are printed.
We will clear the Console by clicking on the broom icon.
Now, we minimize the Console window.
Now let’s move ahead.
Suppose, we want to run only a block of code after print(testSample) from the file myfirstscript.R.
First, select or highlight the block of code, starting from firstVar equals 10 to the end.
Then in the top right corner of the Source window, click on the Run button with an arrow.
Now you can see the output of only the highlighted part of the code in the Console window.
The output shows the values of two variables, 10 and 15 only.
You can also run a particular block by highlighting it and pressing Ctrl+Enter together.
We will clear the Console window by clicking on the broom icon.
Now let us create another R script.
Use Ctrl+Shift+N to open a new script.
Save this script in the same folder myProject with the name mysecondscript.
Now let’s learn how to load myfirstscript.R into mysecondscript.R.
Go to myfirstscript.R and run it by clicking on the Source button.
In the Console window, scroll up to locate the source for this script.
Copy it without the symbol.
Now let’s get back to our new script mysecondscript.R
Paste what we have just copied, at the top of the script mysecondscript.R
We can press Enter after comma for better visibility.
Next, write one more line to ensure that the execution is from this script only.
In the Source window, type print and then the following words in parentheses.
In double quotes, Script space loaded.
Press Enter.
Save the script.
Let us clear the Console again by clicking on the broom icon.
Now run the script by pressing Ctrl+Shift+Enter together.
Maximize the Console window.
You can see the output in the Console window and verify the results.
Now, we minimize the Console window.
This brings us to the end of this tutorial. Let us summarize.
We have learned
How to create, save and run an R script
We now suggest an assignment.
Create a script and save it on Desktop as testscript.R
Load myfirstscript.R in this script and run it.
The video at the following link summarises the Spoken Tutorial project. Please download and watch it.
We conduct workshops using Spoken Tutorials and give certificates. Please contact us.
Do you have questions in THIS Spoken Tutorial?
Choose the minute and second where you have the question. Explain your question briefly.
Someone from the FOSSEE team will answer them. Please visit this site.
Do you have any general / technical questions? Please visit the forum given in the link.
The FOSSEE team coordinates coding of solved examples of popular books. We give honorarium and certificates to those who do this. For more details, please visit these sites.
The Spoken Tutorial project is funded by MHRD, Govt. of India
The script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018). This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.