[R] Creating truly global variable within function within namespace

Murray Stokely murray at stokely.org
Thu Oct 28 08:35:37 CEST 2010


I am trying to create a function with a package with a NAMESPACE that
will save() some variables, distribute an Rdata file to another
computer, where it will be load()ed.  The problem is that this load()
tries to load the namespace of the package on the original computer
that created the .Rdata file, but this package need not be loaded on
the new computer.

This example function is in a package in a namespace :

create.global.function <- function(x, FUN, ...) {
  environment(FUN) <- .GlobalEnv
  assign(".GLOBAL.FUN", function(x) { FUN(x, ...) }, env=.GlobalEnv)
  environment(.GLOBAL.FUN) <- .GlobalEnv
  save(list = ls(envir = .GlobalEnv, all.names = TRUE),
       file = "/tmp/.Rdata",
       envir = .GlobalEnv)
}

And then if I quit my session and then try to load("/tmp/.Rdata")
without loading the package, it will try to loadNamespace from the
.Rdata file and then fail.

Is it possible to fully strip the namespace out of the .GLOBAL.FUN
before I save() it such that it can be loaded into other R instances
without trying to load the namespace?

Thanks for any pointers ..

             - Murray



More information about the R-help mailing list