In previous section, we studied about R Functions, now we will be studying about R History and Scripts.
R has an option called History. We can go to R history, take all of that and can do two things. Either we can send it to console and compile it or we can send it to the source which creates a source code and can be saved in a file that can be used in future.
Source Files and Scripts
There are multiple ways of coding in R. We can write our code in Notepad and then copy paste the snippets in R compiler and run it.
There is one more way that we can open particular source script file and we can type it here and then submit it using Ctrl+enter. But, generally, we will prefer notepad++ as everybody has their own preferences.
Saving the R script
Just click on save as and save the file.
Saving the workspace and Loading the workspace
This is important, as there are so many objects we have created in this session, next time when we open R there is no necessity that these objects will be there. But, what if we want to Login tomorrow and want to see exactly where we left off. In a project, we did half of it, we created few datasets and if tomorrow we have to start from where we left, but we don’t want to run previous codes, then we can just save this workspace. The whole thing will be saved as it is. Even it will save all the commands, if we have any datasets or objects, everything will be saved. Now, whenever on some other day when we login, and load the workspace, we can start from where we left off. It really helps when we have codes that take a lot of time to run and finally, we create that dataset, but if we close it without saving then we will be in trouble. Thus, it is better to save the workspace and then load the workspace the next time we want.
Comments
We can use # for comments. Anything after # is a comment.
If we write “# This code” it will not work. Let’s say x+#10, it will print only x because anything after # is a comment.
#CO2 data is in datasets()
CO2
## Plant Type Treatment conc uptake
## 1 Qn1 Quebec nonchilled 95 16.0
## 2 Qn1 Quebec nonchilled 175 30.4
## 3 Qn1 Quebec nonchilled 250 34.8
## 4 Qn1 Quebec nonchilled 350 37.2
## 5 Qn1 Quebec nonchilled 500 35.3
## 6 Qn1 Quebec nonchilled 675 39.2
## 7 Qn1 Quebec nonchilled 1000 39.7
## 8 Qn2 Quebec nonchilled 95 13.6
## 9 Qn2 Quebec nonchilled 175 27.3
## 10 Qn2 Quebec nonchilled 250 37.1
## 11 Qn2 Quebec nonchilled 350 41.8
## 12 Qn2 Quebec nonchilled 500 40.6
## 13 Qn2 Quebec nonchilled 675 41.4
## 14 Qn2 Quebec nonchilled 1000 44.3
## 15 Qn3 Quebec nonchilled 95 16.2
## 16 Qn3 Quebec nonchilled 175 32.4
## 17 Qn3 Quebec nonchilled 250 40.3
## 18 Qn3 Quebec nonchilled 350 42.1
## 19 Qn3 Quebec nonchilled 500 42.9
## 20 Qn3 Quebec nonchilled 675 43.9
## 21 Qn3 Quebec nonchilled 1000 45.5
## 22 Qc1 Quebec chilled 95 14.2
## 23 Qc1 Quebec chilled 175 24.1
## 24 Qc1 Quebec chilled 250 30.3
## 25 Qc1 Quebec chilled 350 34.6
## 26 Qc1 Quebec chilled 500 32.5
## 27 Qc1 Quebec chilled 675 35.4
## 28 Qc1 Quebec chilled 1000 38.7
## 29 Qc2 Quebec chilled 95 9.3
## 30 Qc2 Quebec chilled 175 27.3
## 31 Qc2 Quebec chilled 250 35.0
## 32 Qc2 Quebec chilled 350 38.8
## 33 Qc2 Quebec chilled 500 38.6
## 34 Qc2 Quebec chilled 675 37.5
## 35 Qc2 Quebec chilled 1000 42.4
## 36 Qc3 Quebec chilled 95 15.1
## 37 Qc3 Quebec chilled 175 21.0
## 38 Qc3 Quebec chilled 250 38.1
## 39 Qc3 Quebec chilled 350 34.0
## 40 Qc3 Quebec chilled 500 38.9
## 41 Qc3 Quebec chilled 675 39.6
## 42 Qc3 Quebec chilled 1000 41.4
## 43 Mn1 Mississippi nonchilled 95 10.6
## 44 Mn1 Mississippi nonchilled 175 19.2
## 45 Mn1 Mississippi nonchilled 250 26.2
## 46 Mn1 Mississippi nonchilled 350 30.0
## 47 Mn1 Mississippi nonchilled 500 30.9
## 48 Mn1 Mississippi nonchilled 675 32.4
## 49 Mn1 Mississippi nonchilled 1000 35.5
## 50 Mn2 Mississippi nonchilled 95 12.0
## 51 Mn2 Mississippi nonchilled 175 22.0
## 52 Mn2 Mississippi nonchilled 250 30.6
## 53 Mn2 Mississippi nonchilled 350 31.8
## 54 Mn2 Mississippi nonchilled 500 32.4
## 55 Mn2 Mississippi nonchilled 675 31.1
## 56 Mn2 Mississippi nonchilled 1000 31.5
## 57 Mn3 Mississippi nonchilled 95 11.3
## 58 Mn3 Mississippi nonchilled 175 19.4
## 59 Mn3 Mississippi nonchilled 250 25.8
## 60 Mn3 Mississippi nonchilled 350 27.9
## 61 Mn3 Mississippi nonchilled 500 28.5
## 62 Mn3 Mississippi nonchilled 675 28.1
## 63 Mn3 Mississippi nonchilled 1000 27.8
## 64 Mc1 Mississippi chilled 95 10.5
## 65 Mc1 Mississippi chilled 175 14.9
## 66 Mc1 Mississippi chilled 250 18.1
## 67 Mc1 Mississippi chilled 350 18.9
## 68 Mc1 Mississippi chilled 500 19.5
## 69 Mc1 Mississippi chilled 675 22.2
## 70 Mc1 Mississippi chilled 1000 21.9
## 71 Mc2 Mississippi chilled 95 7.7
## 72 Mc2 Mississippi chilled 175 11.4
## 73 Mc2 Mississippi chilled 250 12.3
## 74 Mc2 Mississippi chilled 350 13.0
## 75 Mc2 Mississippi chilled 500 12.5
## 76 Mc2 Mississippi chilled 675 13.7
## 77 Mc2 Mississippi chilled 1000 14.4
## 78 Mc3 Mississippi chilled 95 10.6
## 79 Mc3 Mississippi chilled 175 18.0
## 80 Mc3 Mississippi chilled 250 17.9
## 81 Mc3 Mississippi chilled 350 17.9
## 82 Mc3 Mississippi chilled 500 17.9
## 83 Mc3 Mississippi chilled 675 18.9
## 84 Mc3 Mississippi chilled 1000 19.9
CO2$Type
## [1] Quebec Quebec Quebec Quebec Quebec ## [6] Quebec Quebec Quebec Quebec Quebec ## [11] Quebec Quebec Quebec Quebec Quebec ## [16] Quebec Quebec Quebec Quebec Quebec ## [21] Quebec Quebec Quebec Quebec Quebec ## [26] Quebec Quebec Quebec Quebec Quebec ## [31] Quebec Quebec Quebec Quebec Quebec ## [36] Quebec Quebec Quebec Quebec Quebec ## [41] Quebec Quebec Mississippi Mississippi Mississippi ## [46] Mississippi Mississippi Mississippi Mississippi Mississippi ## [51] Mississippi Mississippi Mississippi Mississippi Mississippi ## [56] Mississippi Mississippi Mississippi Mississippi Mississippi ## [61] Mississippi Mississippi Mississippi Mississippi Mississippi ## [66] Mississippi Mississippi Mississippi Mississippi Mississippi ## [71] Mississippi Mississippi Mississippi Mississippi Mississippi ## [76] Mississippi Mississippi Mississippi Mississippi Mississippi ## [81] Mississippi Mississippi Mississippi Mississippi ## Levels: Quebec Mississippi
CO2$Type #Printing a column
## [1] Quebec Quebec Quebec Quebec Quebec
## [6] Quebec Quebec Quebec Quebec Quebec
## [11] Quebec Quebec Quebec Quebec Quebec
## [16] Quebec Quebec Quebec Quebec Quebec
## [21] Quebec Quebec Quebec Quebec Quebec
## [26] Quebec Quebec Quebec Quebec Quebec
## [31] Quebec Quebec Quebec Quebec Quebec
## [36] Quebec Quebec Quebec Quebec Quebec
## [41] Quebec Quebec Mississippi Mississippi Mississippi
## [46] Mississippi Mississippi Mississippi Mississippi Mississippi
## [51] Mississippi Mississippi Mississippi Mississippi Mississippi
## [56] Mississippi Mississippi Mississippi Mississippi Mississippi
## [61] Mississippi Mississippi Mississippi Mississippi Mississippi
## [66] Mississippi Mississippi Mississippi Mississippi Mississippi
## [71] Mississippi Mississippi Mississippi Mississippi Mississippi
## [76] Mississippi Mississippi Mississippi Mississippi Mississippi
## [81] Mississippi Mississippi Mississippi Mississippi
## Levels: Quebec Mississippi
This is all about comments. Practice more in your PC all the different things mentioned above.
The next post will be most errors in R.
In next section, we will be studying about Most Common Errors in R.