Search code examples
rglobal-variables

Global variables in packages in R


I'm developing a package in R. I have a bunch of functions, some of them need some global variables. How do I manage global variables in packages?

I've read something about environment, but I do not understand how it will work, of if this even is the way to go about the things.


Solution

  • In general global variables are evil. The underlying principle why they are evil is that you want to minimize the interconnections in your package. These interconnections often cause functions to have side-effects, i.e. it depends not only on the input arguments what the outcome is, but also on the value of some global variable. Especially when the number of functions grows, this can be hard to get right and hell to debug.

    For global variables in R see this SO post.

    Edit in response to your comment: An alternative could be to just pass around the needed information to the functions that need it. You could create a new object which contains this info:

    token_information = list(token1 = "087091287129387",
                             token2 = "UA2329723")
    

    and require all functions that need this information to have it as an argument:

    do_stuff = function(arg1, arg2, token)
    do_stuff(arg1, arg2, token = token_information)
    

    In this way it is clear from the code that token information is needed in the function, and you can debug the function on its own. Furthermore, the function has no side effects, as its behavior is fully determined by its input arguments. A typical user script would look something like:

    token_info = create_token(token1, token2)
    do_stuff(arg1, arg2, token_info)
    

    I hope this makes things more clear.