Global variables in R

1. Global Variables in R

Rlogo

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 [1]. Thus, if you want to return mor
e than one parameter, there are at least three ways:

  1. return a list;
  2. use the assign() or eval.parent(substitute()) commands;
  3. 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.

 

#! /usr/bin/env Rscript

# get the global variable as argument
# after the call to the script
args <- commandArgs(trailingOnly = TRUE)

#-----------------------------------------#
# GLOBAL VARIABLES                        #
#-----------------------------------------#

glVAR <- as.numeric(args[1])

#-----------------------------------------#
# FUNCTIONS                               #
#-----------------------------------------#

sum_glVAR <- function(start, end, add) {

    # local variable, valid within the
    # sum_glVAR scope odd <- 0

    for (index in start:end) {

        operation <- add * index

        reminder <- operation %% 2
        if (reminder == 0) odd <- odd + 1

        glVAR <<- glVAR + operation

    }

    # return the number of odd values added
    # to the global variable
    return(odd)

}

#-----------------------------------------#
# MAIN                                    #
#-----------------------------------------#

odd_numb <- sum_glVAR(1, 3, 2.5)

 

This code doesn’t do anything useful. But it shows that if you have to return to several values as glVAR and odd you can use a global variable and a local variable, whithout creating a nonsense list.

Why do not use eval.parent(...)?

We will discuss in the next post!

Happy hacking!

2. Bibliography

[1] Matloff, N. (2011). The Art of R programming. no starch press.

 

About Francesco Serafin

I am a PhD student at the Department of Civil, Environmental and Mechanical Engineering at the University of Trento. My two greatest loves: Computer Science and Water (take three with my Lenovo!:D). As a result, the aim of my life is to make both passions coexist. My gpg public key available at https://pgp.mit.edu

Leave a Reply

Your email address will not be published.

*