Skip to content
Prev 46917 / 63424 Next

search for variable in package in .GlobalEnv first

On 07/10/2013 10:29 AM, Rainer M Krug wrote:
The rgl package does this when looking for defaults for graphics. Here's 
the code:


getr3dDefaults <- function()
     tryCatch(get("r3dDefaults", envir=.GlobalEnv),
          error = function(e) r3dDefaults)

This will find the variable r3dDefaults if it is in the global 
environment or in a package on the search path; if that fails, it 
returns the local one.  Since that function is defined in the package, 
it can see the local r3dDefaults variable.  You might not want to accept 
anything except what is in .GlobalEnv; in that case, use inherits = 
FALSE in the call to get().

Duncan Murdoch