From luke at stat.uiowa.edu  Mon Oct 23 15:23:20 2006
From: luke at stat.uiowa.edu (Luke Tierney)
Date: Mon, 23 Oct 2006 08:23:20 -0500 (CDT)
Subject: [Rd] Getting hold of a package's environment from C code
In-Reply-To: <Pine.LNX.4.64.0610230831110.11172@gannet.stats.ox.ac.uk>
References: <eb555e660610221256g3952a408sc25f06afe8307cb5@mail.gmail.com>
	<453BD456.4050702@stats.uwo.ca>
	<eb555e660610221424kac664fanba7b998b12faaf1e@mail.gmail.com>
	<m2ac3n3ce9.fsf@ziti.fhcrc.org> <m28xj71wqa.fsf@ziti.fhcrc.org>
	<Pine.LNX.4.64.0610230810330.10964@gannet.stats.ox.ac.uk>
	<Pine.LNX.4.64.0610230831110.11172@gannet.stats.ox.ac.uk>
Message-ID: <Pine.LNX.4.64.0610230819160.3414@itasca2.wildberry.org>

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

> 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.

This level of detail should remain undocumented as it might be useful
to change the representation at some point. R_FindNamespace is a safer
bet, and we probably should document it as officially available..

Best,

luke

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu




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