Understanding the communication with R

Prior reading

It would be recommended to familiarised yourself with R and R studio, before completing this part of the tutorial.


Communicating with R

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. 
ErrorsErrors 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.
WarningsA warning suggests that something has not worked as expected. However, it was able to be corrected and a value is likely to be returned. 


R as a calculator

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

Before completing this part of the tutorial, clear the environment. Click on the little brush in the Environment.

Downloading and running the script

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).

  • The console should list at least four variables: a, b, d and animals.  
  • The second line should only shows variables, which contains the letter 'a'
  • The third line should only display and error. The execution of script is stop.

Outcome

Correcting errors

  1. The error suggests the argument patterns has yet to be defined or it is not recognised; the correct spelling is pattern; correct the error and run the script again.
  2. The following error is now shown: Error in eval(ei, envir) : object 'd' not found.  To correct this error, create a variable d before it is used. Assign any values and run the script again.

Practice - warnings

  1. In the console, create a vector primes with the values 1, 2, 3; type primes <-  c(2,3,5). 
  2. In the console, create another vector non.primes with the values 3 and 4. Type b <- c(1,4)
  3. Multiply both vectors together; primes* non.primes

Correcting warnings

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 and of the same length and complete the multiplication again.

R console  ("Advanced" users)

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. 


What can I learn next?

R functions and packages

DataSHIELD Wiki by DataSHIELD is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://www.datashield.ac.uk/wiki