Search code examples
rscopeenvironment-variablesscoping

store variables from a function in the global environment


coming back to this post years later to add yet another warning: DONT DO THIS

I am trying to store a variable from a function in the global environment, but none of the solutions I found SO post or different SO post worked for me. This is an extremely simplified example to illustrate my problem. I cannot just use the <<- to make v global because my actual function creates several dozen variables.

Code (main_file.R)

source("function_me.R")
#environment(doGlobalThing) <-parent.frame(2) #didnt work
doGlobalThing()
v #cant get to this var from the global environment

Code (function_me.R)

doGlobalThing<-function(){
  #eval(v<-'hi',env=parent.frame(2)) #tried this, didnt work
   v<-'hi'
}

Solution

  • You could use assign:

    assign("v","hi",envir = globalenv())
    

    This requires that you have the name of the target global variable as a string, but it can be easy to do this even with a vector of dozens of such things.

    This question discusses the differences between assign and <<-. The chief difference is that assign lets you specify the environment -- so it is easy to use it to store data in a non-global but persistent environment so that you could e.g. emulate static variables in R. While it is possible to use assign to modify the global environment, you should be aware that it is seldom a good thing to do so. There is too much of a danger of accidentally overwriting data that you don't want to have overwritten. Code which makes heavy use of global variables can almost always be refactored into cleaner code which doesn't. If you need to get a lot of heterogeneous data from a function to the calling environment, the cleanest solution would be to return the needed data in a list.

    The accepted answer ends its discussion of <<- and assign with a good quote:

    The Evil and Wrong use is to modify variables in the global environment.