Creating and serializing / saving global variable from within a NAMESPACE in R

Tag: r Author: remmyzhjhz Date: 2010-10-12

I would like to create a function within a package with a NAMESPACE that will save some variables. The problem is that when load is called on the .Rdata file it tries to load the namespace of the package that contained the function that created the .Rdata file, but this package need not be loaded.

This example function is in a package in a namespace : <- 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)

The environment(.GLOBAL.FUN) <- .GlobalEnv calls are not sufficient and attaching gdb to the R process confirms it is serializing a NAMESPACESXP here with the name of the package namespace and the load fails because it is unable to load this.

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?

I think it can only happen with S4 objects, but I'm not sure... Is it the case?
May be I am missing something, but you are saving from and that one is in a namespace. So what would you expect? You are "striping" the .GLOBAL.FUN which does not have any bearing on the saving and so on your problem.
what are you trying to achieve actually? This seems quite an odd way to do something that maybe can be done in a more appropriate way. Could you give a bit more background on the meaning, this could lead to other suggestions.
Good points, Joris. When I think "how do I create a function inside a NAMESPACE", what comes to mind is "write a local package" rather that hustle with load/save.

Other Answer1

@JorisMeys snowfall and the others do not offer exactly this functionality.

snowfall uses sfExport ( from clusterFunctions.R in snowfall) to export local and global objects to the slave nodes, and this in turn uses sfClusterCall which is a wrapper around the clusterCall function from snow.

res <- sfClusterCall( assign, name, val, env = globalenv(),
                      stopOnError = FALSE )

And the snow library is loaded on the clients getting around any namespace issues as I mentioned in the last sentence of my question I would like to not load the namespace there.

Furthermore, it seems to make simplified assumptions such as that the nodes will share an NFS mount point for shared data (e.g. sfSource function in clusterFunctions.R).

I am more interested in something like a case where a node saves an .Rdata file then scp's it to another node that need not have the package namespace loaded.

It seems I can for now solve my original problem by using eval.parent and substitute:

         eval.parent(substitute(function(y) { FUN(y, ...) })),

I apologize for the posting snafu, but I do not have an edit link although I posted this question, nor is there any place for me to leave a "comment" in the same way that I have this big text field for an answer. I've flagged this for moderation so I can get some help with that and have referenced the FAQ which talks about buttons that do not appear for me for leaving comments. there is some problem with this new account.