It would be recommended to familiarised yourself with R and R studio, before completing this part of the tutorial.
Suggested reading |
---|
The philosophy of R interaction is mainly command-based on the console and writing some scripts. R interprets and processes each line at a time. The results of these processes are shown using mainly a textual approach. It is hoped to create a type of basic conversation between the users and the computer. Sometimes the R programmer may be in this situation.
Because of the nature of R programming language most of the commands consist of some functions that returns a value and some arithmetic operators. The latter can have three outcomes: correct results, errors and warnings.
Outcome | Description |
---|---|
Correct results | The results of the command are shown, using a variety of data types. If a value returned by a function is assigned to a variable, then no value tends to be displayed. The latter is kept for future use in the current R environment until the end of the session. |
Errors | Errors inform the users a function could not complete its execution. A problem has occurred and an error has been thrown. No value is returned by the function. Some of the errors can be something challenging to understand; some imaginations and good problem solving skills may be required. |
Warnings | A warning suggests that something has not worked as expected. However, it was able to be corrected and a value is likely to be returned. |
These two examples demonstrate how R can be used a calculator
> 1 + 1 [1] 2 |
This example show a list of random calculations in a script. Each line is interpreted by chronological order. The output is shown below.
1+1 20*5 (45*3)+3.7 2^2 pi (30/90)*100 |
Output:
> 1+1 [1] 2 > 20*5 [1] 100 > (45*3)+3.7 [1] 138.7 > 2^2 [1] 4 > pi [1] 3.141593 > (30/90)*100 [1] 33.33333 |
Practice - managing errors
Download the file referred as communication_part_1.R. Run its content (see Become familiar with R Studio and basic concepts of R for more information).
Outcome
Warnings do not stop the execution of a function. The warning messages can clutter the results. It is best to correct the cause of the warnings. In this instance, set two vectors a and b of the same length and complete the multiplication again.
The R console can be opened from (1) the command-prompt in Windows or in the terminal in MacOs and Unix. By typing the command R, a R console opens automatically. This command is part of the R installation (see What is R programming language?.) Advanced users who are familiarised with such tools should explore this option.
Some R console have been implemented online, Tutorialspoint - R Tutorial provides many live examples that can be edited. R Studio also offers some R Notebooks. The latter interactively runs some R command and show the results in a more user-friendly manner.
Additional reading list |
---|