The ‘’read parse evaluate’’ loop

The ‘’read parse evaluate’’ loop is at the core of the R system. Understanding this mechanism and being able to manage and control it is a key point for writing efficient R codes.

When we type R commands, two things happen:

  • those characters are parsed into an R expression
  • the expression is then evaluated by the internal evaluator

This process, known as the ‘’read parse evaluate’’ loop, is internally performed by the R system but, the same process is available to the end user by mean of two functions: parse() and eval() .

Effectively, when typing any a <- 1, what happens is

The inner parse section returns an object of class expression and afterward the expression is evaluated by the evaluator.

When we call functions eval() and parse() directly, we generally pass character strings as arguments to the parse() function either from quoted text strings or external files

Expressions objects are special language objects of class expression which contain parsed but unevaluated R statements. Parsed expressions are stored in an R object that can be explored as standard list objects.

and even manipulated as standard list objects

The evaluation part of the R program consists of passing the object resulting from parsing the current expression to the R evaluator.

The parsed expression is then evaluated by the function eval().

and the result is returned.

Because of the way R works, evaluating expressions is, except few exceptions, about evaluating functions calls. This is clearly true when we call any a standard function in R as:

but, this is also true when we write any assignment statement. In fact R translates:

into a function call:

and even a conditional construct such as:

translates into a call to a function

The parse-eval mechanism has at least three exceptions: constants, names and promises:

Constants in R are evaluated into themselves. Any expression as:

is the evaluation of a constant while

turns into a call to a function:

More on how R evaluates function in the chapter dedicated to functions

A symbol is a variable name with a value associated to it: x is a symbol, or a symbol name:

Symbols in R may be made of lower or capital letters, numbers and the special characters "." and "_". Almost any rule is a valid one when defining a symbol

Standard symbols cannot start with a number or a "_". Any name staring with a "." is a hidden name meaning that it is not returned by a call to ls() unless argument all.names is set to TRUE.

When we ask R to evaluate a symbol, R looks for the value associated to that symbol, first in the current environment and, in case the symbol is not found within current environment, R looks progressively in all the parents environments until the object value is returned or an error occurs as the symbol is not found. This key idea will be fully discussed in the chapter dedicated to environments.

Assignment

When typing a = 1 at the command prompt, the value 1 in assigned to the symbol a. The = operator is used to perform the assignment. In fact, R provides three operators for assignments: =, <- and <<- the last two being bi-directional.

Operators = and <- assign into the environment in which they are evaluated. Therefore, at the command prompt a = 1 is equivalent to a <- 1.

When an assignment is done on formal parameter lists within functions calls, assignment is performed in the environment where the function is evaluated if the = operator is used while the same assignment occurs in the local environment in case the of the <- operator. As a simple example, we can consider a simple call to any function i.e. median(). First we clean up our workspace:

Then we call median() using both = and <- operators for parameters assignement:

In practice, the way the evaluator understands assignment is:

R supports multiple assignments with both operators.

Attention should be paid as:

works correctly but:

returns an error. This happens as

k = p <- 0 translates to '='(k,'<-'(p, 0))

while

k <- p = 0 is interpreted as '='('<-'(k, p), 0)

as the <- operator takes precedence on the = operator.

Finally, operator <<- is used to assign into the parent environment. As an example consider:

In this case the assignment x = 8 is performed within the parent frame of env, that is R_GlobalEnv. Thus:

does not show any x symbol while x is still available in env:

as, since the evaluator does not find x in the local frame, it looks for x in the parent frame. In fact:

Removing objects

To remove objects, the function rm() can be used. The function remove() may be considered as an alias for the rm() function.

As seen above, ls() returns a vector containing all objects in the current environment. To remove all objects in the current environment, all you need is

Of course, the list argument can contain any character vector with object names.

When argument are not already in a vector, they can be passed directly:

When arguments are passed directly, and not in the character vector list, it is not mandatory to quote them.

Garbage collection

When objects are no longer used, and this clearly happens when objects are deleted. R releases immediately the memory they filled in the system. This is done automatically by the garbage collector gc().

We can call gc() to see how much memory R is using for allocating objects

and as a proof, we can create 100x10^7 elements matrix

and check how much memory R is using:

The increase of memory usage is related to the newly created matrix that takes:

When this matrix is removed, the memory is immediately released to the operating system.