1. Global Variables in R
This is the first post about R, so I would like to write a small introduction before start to speak about the main argument.
R is a very powerful scripting language, widely used for statistical data manipulation and analysis.
Its main qualities are:
- it is open source (it is sometimes called GNU S to reflect its open source nature). This is the reason of its popularity compared to S or S-plus languages.
- it is easy to start programming with R because it is easy to write small scripts thanks to its high level language.
- it is an interpreted language (indeed it is very very very slow) and it is used through a command-line interpreter where you can test the code piecemeal.
- there are thousands of open source packages available and the it is very simple to find the package with the features you need.
In addition to providing statistical operations, R is a general-purpose programming language thus it is possible to develop small softwares with many rows of code. In this case, it is very important to follow some rules tipical for lower level programming languages, in order to write robust and maintanable codes.
The correct use of global variables is maybe one of these rules. As the definition says: “a global variable is a variable defined outside functions so it is available within functions as well”.
Yes, I know, the use of global variables is a subject of controversy in the programming community, in fact its use may create a code difficult to debug and maintain. However, I think that, if you have to write a small script around a key variable and you need to hurry, you can create globals without worry.
Indeed in R, one of the reason to use globals is that no passing by reference is allowed in functions: to evaluate a function call, R copies each actual argument to the corresponding local parameter variable, and changes to that variable are not visible outside the function . Thus, if you want to return mor
e than one parameter, there are at least three ways:
- return a list;
- use the
- use global variables.
and the latter is, maybe, the most expeditious method.
How to use global variables is very simple. You have to remember that when you want to modify their values inside functions, you have to use superassignment operator
<<-, as shown in the following simple example.
This code doesn’t do anything useful. But it shows that if you have to return to several values as
odd you can use a global variable and a local variable, whithout creating a nonsense list.
Why do not use
We will discuss in the next post!
 Matloff, N. (2011). The Art of R programming. no starch press.