Call by reference in R (using function to modify an object)

The reason you’re having trouble is the fact that you are passing the object into the local namespace of the function. This is one of the great / terrible things about R: it allows implicit variable declarations and then implements supercedence as the namespaces become deeper.

This is affecting you because a function creates a new namespace within the current namespace. The object ‘myTable’ was, I assume, originally created in the global namespace, but when it is passed into the function ‘title.asterisk’ a new function-local namespace now has an object with the same properties. This works like so:

title.asterisk <- function(myTable){ do some stuff to 'myTable' }

In this case, the function ‘title.asterisk’ does not make any changes to the global object ‘myTable’. Instead, a local object is created with the same name, so the local object supercedes the global object. If we call the function title.asterisk(myTable) in this way, the function makes changes only to the local variable.

There are two direct ways to modify the global object (and many indirect ways).

Option 1: The first, as you mention, is to have the function return the object and overwrite the global object, like so:

title.asterisk <- function(myTable){
    do some stuff to 'myTable'
    return(myTable)
}
myTable <- title.asterisk(myTable)

This is okay, but you are still making your code a little difficult to understand, since there are really two different ‘myTable’ objects, one global and one local to the function. A lot of coders clear this up by adding a period ‘.’ in front of variable arguments, like so:

title.asterisk <- function(.myTable){
    do some stuff to '.myTable'
    return(.myTable)
}
myTable <- title.asterisk(myTable)

Okay, now we have a visual cue that the two variables are different. This is good, because we don’t want to rely on invisible things like namespace supercedence when we’re trying to debug our code later. It just makes things harder than they have to be.

Option 2: You could just modify the object from within the function. This is the better option when you want to do destructive edits to an object and don’t want memory inflation. If you are doing destructive edits, you don’t need to save an original copy. Also, if your object is suitably large, you don’t want to be copying it when you don’t have to. To make edits to a global namespace object, simply don’t pass it into or declare it from within the function.

title.asterisk <- function(){ do some stuff to 'myTable' }

Now we are making direct edits to the object ‘myTable’ from within the function. The fact that we aren’t passing the object makes our function look to higher levels of namespace to try and resolve the variable name. Lo, and behold, it finds a ‘myTable’ object higher up! The code in the function makes the changes to the object.

A note to consider: I hate debugging. I mean I really hate debugging. This means a few things for me in R:

  • I wrap almost everything in a function. As I write my code, as soon as I get a piece working, I wrap it in a function and set it aside. I make heavy use of the ‘.’ prefix for all my function arguments and use no prefix for anything that is native to the namespace it exists in.
  • I try not to modify global objects from within functions. I don’t like where this leads. If an object needs to be modified, I modify it from within the function that declared it. This often means I have layers of functions calling functions, but it makes my work both modular and easy to understand.
  • I comment all of my code, explaining what each line or block is intended to do. It may seem a bit unrelated, but I find that these three things go together for me. Once you start wrapping coding in functions, you will find yourself wanting to reuse more of your old code. That’s where good commenting comes in. For me, it’s a necessary piece.

Leave a Comment