R/C2/Introduction-to-R-script/English-timed

From Script | Spoken-Tutorial
Revision as of 12:45, 8 May 2020 by Sakinashaikh (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
00:01 Welcome to this tutorial on Introduction to R script.
00:06 In this tutorial, you will learn
00:09 How to work with an R script in RStudio
00:14 To understand this tutorial, you should know basic commands of R.
00:21 If not, please locate the relevant tutorials on R on this website.
00:28 This tutorial is recorded on
00:32 Ubuntu Linux OS 16.04
00:37 R version 3.2.3
00:42 RStudio version 1.1.456
00:48 Install R version 3.2.0 or higher.
00:54 An R script is a plain text file in which you save your R code
00:01 Script allows you to show your work to others, and also reproduce and modify the results
01:10 Before you begin please create a folder named myProject on Desktop.
01:17 I have already created this folder on my Desktop.
01:23 I shall now save all my scripts and data sets in this folder.
01:30 Later, I shall set this folder as my Working Directory.
01:37 Let us switch to RStudio.
01:40 We will close these two tabs named Untitled1 and R packages available.
01:49 Click on the small cross symbol next to these tabs
01:55 We will now create a new R script.
01:59 In the top left corner of RStudio, click on the green plus sign.
02:05 Now from the dropdown menu, select R Script.
02:11 A new R script appears in the Source window.
02:17 You can also create a new R script by pressing Ctrl+Shift+N together.
02:24 Let us save this R script first.
02:28 In the top left corner of the screen, click on the File option.
02:36 Then click on Save to save the script.
02:40 Select the directory where you want to save your R script.
02:46 I am saving the script in the folder myProject on my Desktop, with the name myfirstscript.
02:56 Finally, click on the Save button.
03:01 Now you can see that the file is named as myfirstscript.R.
03:07 RStudio automatically adds dot R at the end of the filename.
03:15 Now, let us write some lines of code in the script file myfirstscript.R.
03:23 We will create a vector named testSample with one two three four as its elements.
03:33 We have already learned how to create vectors earlier in this series.
03:39 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

03:59 You may also use equal sign in place of less than symbol followed by hyphen.
04:08 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.
04:22 We will now use the print function to display the values in testSample vector.
04:30 In the Source window, type p r i
04:35 RStudio automatically starts suggesting different functions.
04:41 You can navigate this list of functions by using the up (↑) and down (↓) arrow keys on the keyboard.
04:50 To select a particular function, just press Enter.
04:56 Finish typing print testSample in parentheses and press Enter.
05:04 Now let’s save the file and run it with these two lines only.
05:11 Now, we maximize the Source window.
05:15 In the top right corner of the Source window, click on the Source button.
05:22 You can see the line-by-line execution of the script in the Console.
05:29 You can also run the script by pressing Ctrl+Shift+Enter together.
05:36 For this, you need to first click in the script window.
05:42 Now let’s declare a variable firstVar with value of ten in the Source window.
05:50 Type firstVar with capital V space equal sign space 10 and press Enter.
06:00 Please note that R language is case-sensitive.
06:05 We will now use the print function to show the value of firstVar.
06:11 In the Source window, type print.
06:16 I am typing only p r i n but the options appear and I select print from there.
06:28 Now in parentheses, start typing firstVar.
06:32 RStudio shows firstVar; press Enter to select it.
06:39 Press Enter to go to the next line.
06:43 Now let’s declare another variable secondVar with value of fifteen.
06:50 In the Source window, type secondVar space equal sign space 15 and press Enter.
07:01 Now, as shown before, type print secondVar in parentheses to show its value and press Enter
07:13 We can add comments to our code by using # hash symbol followed by our comment.
07:22 Type hash symbol space Creating an R script before the declaration of testSample vector.
07:32 R interprets each line of code.
07:36 Please make sure that your comment and the first line of code appear in separate lines.
07:46 Now, let’s comment the line print(secondVar).
07:50 To do that, first click on this line.
07:56 Then from the top menu bar of RStudio, click on the Code option.
08:04 From the dropdown menu, click on the Comment/Uncomment Lines option.
08:10 This option helps us to comment as well as uncomment a line.
08:17 On doing so, we see a hash symbol with space.
08:24 For now, we will uncomment print(secondVar) and save our script.
08:30 Delete # hash symbol at the beginning of this line.
08:35 Now let’s run the script file.
08:38 In the top right corner of the Source window, click on the Source button.
08:45 Now maximize the Console.
08:50 The line-by-line execution of the script is shown in the Console window.
08:56 The sample vector [1 2 3 4] is printed.
09:02 The values of two variables, 10 and 15 are printed.
09:08 We will clear the Console by clicking on the broom icon.
09:15 Now, we minimize the Console window.
09:19 Now let’s move ahead.
09:23 Suppose, we want to run only a block of code after print(testSample) from the file myfirstscript.R.
09:34 First, select or highlight the block of code, starting from firstVar equals 10 to the end.
09:44 Then in the top right corner of the Source window, click on the Run button with an arrow.
09:53 Now you can see the output of only the highlighted part of the code in the Console window.
10:00 The output shows the values of two variables, 10 and 15 only.
10:07 You can also run a particular block by highlighting it and pressing Ctrl+Enter together.
10:16 We will clear the Console window by clicking on the broom icon.
10:23 Now let us create another R script.
10:28 Use Ctrl+Shift+N to open a new script.
10:33 Save this script in the same folder myProject with the name mysecondscript.
10:43 Now let’s learn how to load myfirstscript.R into mysecondscript.R.
10:53 Go to myfirstscript.R and run it by clicking on the Source button.
11:02 In the Console window, scroll up to locate the source for this script.
11:09 Copy it without the symbol.
11:14 Now let’s get back to our new script mysecondscript.R
11:20 Paste what we have just copied, at the top of the script mysecondscript.R
11:29 We can press Enter after comma for better visibility.
11:35 Next, write one more line to ensure that the execution is from this script only.
11:43 In the Source window, type print and then the following words in parentheses.
11:51 In double quotes, Script space loaded.
11:56 Press Enter.
11:58 Save the script.
12:01 Let us clear the Console again by clicking on the broom icon.
12:09 Now run the script by pressing Ctrl+Shift+Enter together.
12:16 Maximize the Console window.
12:20 You can see the output in the Console window and verify the results.
12:27 Now, we minimize the Console window.
12:30 This brings us to the end of this tutorial. Let us summarize.
12:37 We have learned
12:39 How to create, save and run an R script
12:44 We now suggest an assignment.
12:47 Create a script and save it on Desktop as testscript.R
12:54 Load myfirstscript.R in this script and run it.
13:00 The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

13:10 We conduct workshops using Spoken Tutorials and give certificates.

Please contact us.

13:18 Do you have questions in THIS Spoken Tutorial?
13:18 Choose the minute and second where you have the question. Explain your question briefly.
13:31 Someone from the FOSSEE team will answer them. Please visit this site.
13:37 Do you have any general / technical questions? Please visit the forum given in the link.
13:45 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.
14:00 The Spoken Tutorial project is funded by MHRD, Govt. of India
14:08 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.

Contributors and Content Editors

Sakinashaikh