[Rd] Getting hold of a package's environment from C code

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Oct 23 09:43:38 CEST 2006


As far as I can tell from the original post, Deepayan asked for a way 
to find a package's namespace environment (rather than the package 
environment, <namespace:foo> not <package:foo>), so the subject line is 
subject to misinterpretation.

On Mon, 23 Oct 2006, Prof Brian Ripley wrote:

> On Sun, 22 Oct 2006, Seth Falcon wrote:
>
>> Seth Falcon <sfalcon at fhcrc.org> writes:
>>> Perhaps:
>>> 
>>> R_FindNamespace(mkString(where))
>> 
>> Sorry, this won't help you for package-level code as this function is
>> part of the internal use only API.  It would be nice to have access to
>> it or a similar function from C in package code.
>
> Hmm, it is declared in Rinternals.h, which is the public header for 
> manipulating R objects at C level.  As such it is available to packages, but 
> as it is not documented in 'Writing R Extensions' it is subject to change.
>
> R_FindNamespace is essentially the same code as Duncan M provided, so it is a 
> pretty simple function.  (It still contains a test that namespaces are 
> supported, and that seems long overdue for removal.)

I think Deepayan's original enquiry was about getting hold of a namespace 
environment from within an already loaded namespace.  That can be done 
entirely at C level, as we don't have the complication of possibly loading 
the namespace.  Just

     val = findVarInFrame(R_NamespaceRegistry, install("pkgname"));

does that job, with val == R_UnboundValue should the namespace not be 
loaded.  AFAICS, everything here is available to a package writer, and 
this is equally undocumented.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-devel mailing list