A Additional Tips
Commenting
Imagine writing a 500 line R code which analyses financial and claim severity data for your company. Within this code, you have assigned a variety of variables, produced countless plots and created numerous different data frames containing the necessary information. Now imagine either of the following scenarios:
- You need to go back through the code to find a particular plot and/or function that computed a certain value of interest that lies somewhere in the middle of your code;
- You have been re-assigned to a different task and have to send your source code to another colleague to take over.
In either case, you will encounter a major problem when it comes to going through lines upon lines of code to find what your looking for, or even remember what you have done. Therefore, to avoid this problem, we insist on the use of commenting when writing R script. To add comments into your script you can simply use the hash tag symbol #
. R understands that anything on a line following the hash tag is only a comment and will not be executed when run into the console. For example, look at the following code:
As you can see from Figure:@ref{fig:comment1}, when we ran the line of code from the script into the console, R throws up an error. This is due to the fact that R is trying to understand the text following the curve()
function as a command to be executed but cannot match it to any known functions and/or variables. On the other hand, if we add the commenting symbol #
, look what happens:
In this case, R executed the initial command/function curve()
but then did not consider anything after the hash tag as it knows it is simply a comment. This is a very helpful feature that we strongly recommend you use at all times and get into the habit of using early. It will save you a lot of time and hard work further down the line.
Help
The final feature we want to mention in this introductory chapter is the help functionality. Towards the start of this chapter, we discussed the ‘Help’ tab on the bottom right of the screen and briefly explained how it works. In summary, you can click the ‘Help’ tab and search for a particular function, e.g. plot()
. Doing so will bring up an information page detailing the plot()
function, its possible arguments and some worked examples:
An alternative way to access this help page is to use the ?
symbol within the script/console. For example, if you know the name of the function you want some more information about, e.g. plot()
, instead of going into the ‘Help’ tab, you can simply ?plot()
in your script then run it into the console or, equivalently, type it directly into the console itself. In either case you will produce the same screen as seen in Figure:A.3 above. Finally, if you do not know the function names itself you can conduct a broader search of a specific word using the double question mark symbol, i.e. ??
. For example, assume you wanted to compute the variance but did not already know the associated function was var()
. Then, you could type ??variance
, which would bring up a list of information pages containing any relevance to variance and you can browse through these as you wish until you find an appropriate function:
Although these ‘Help’ tools will indeed prove very helpful throughout your programming journey, you cannot emphasise enough the power and ease of simply using a search engine to find answers. There are so many different packages, containing different functions, each of which has several arguments with a list of possible value, it is impossible to learn them. Therefore, browsing the internet to search for these functions and how bets to use them is another vital tool at your disposal.