Skip to content
Prev 248067 / 398503 Next

Truly Global Variables

On Sat, Jan 22, 2011 at 8:17 AM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
Or a bit safer:

   assign("globalvariable", 10, .GlobalEnv)

Note that this type of code is sometimes an attempt to get into object
oriented programming through the back door.  You might just want to
make it explicit.  The proto package will let you do that.  Below we
define a proto object, p, with components function_called and
function_calls and then later, within function_calls, we create a
third component of p called variable.

library(proto)

p <- proto(function_called = function (.) {
       result = .$variable * .$variable
       print(result)
       },
       function_calls = function(.) {
       .$variable <- 10
       .$function_called()
       }
)

p$function_calls()