[R] How to pass an object between two independant environment?

Duncan Murdoch murdoch.duncan at gmail.com
Wed Oct 2 16:19:38 CEST 2013

On 02/10/2013 10:12 AM, Frederic Fournier wrote:
> Hello everyone,
> I am trying to pass an object cleanly between two independant
> environments, any ideas on how to do that?
> I have something like this:
> calling.function <- function(my.object) {
> ... ...
> local(source("function_to_which_I_want_to_pass_my_object.Rda", local =
> new.env(parent = .GlobalEnv))) # I can't change this part of the code
> }
> The calling function will be part of a package, so I don't want to use
> the global environment (that's the user's space, not a space that should
> be used by the packages). Writing a temp file is unclean for the same
> reason (and using the user's hard-drive is actually worse than using its
> space in R!). The package namespace is locked when the package is
> loaded, so using the package environment is not a perfect option either.
> I was wondering, is there a way to create a named environment in R? If
> it was possible to do that, then the calling function could 'assign' to
> the named-environment and the scripted function could 'get' from it and
> the problem would be solved.

name <- new.env()

creates an environment named "name".

Duncan Murdoch

More information about the R-help mailing list