From ripley at stats.ox.ac.uk  Mon Oct 23 08:41:00 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 23 Oct 2006 07:41:00 +0100 (BST)
Subject: [Rd] Getting hold of a package's environment from C code
In-Reply-To: <eb555e660610221424kac664fanba7b998b12faaf1e@mail.gmail.com>
References: <eb555e660610221256g3952a408sc25f06afe8307cb5@mail.gmail.com>
	<453BD456.4050702@stats.uwo.ca>
	<eb555e660610221424kac664fanba7b998b12faaf1e@mail.gmail.com>
Message-ID: <Pine.LNX.4.64.0610230738340.10335@gannet.stats.ox.ac.uk>

On Sun, 22 Oct 2006, Deepayan Sarkar wrote:

> On 10/22/06, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> On 10/22/2006 3:56 PM, Deepayan Sarkar wrote:
>>> Hi,
>>>
>>> I have a package where I'm calling an R function (say "foo") from C
>>> code. "foo" is in the same package, but is not exported. I construct
>>> the call using lang1(install("foo")), but to eval it I need the
>>> package's environment.  Is there a way to do this? Passing the correct
>>> environment through .Call() is not an option.
>>>
>>> Right now, I'm getting the environment first using something like
>>>
>>>   rho = PROTECT(eval(lang2(install("environment"), install("bar")),
>>> R_GlobalEnv))
>>>
>>> where bar _is_ exported. However, this doesn't work if the package is
>>> loaded but not attached, and is also risky because someone might
>>> define another "bar" that is found first.
>>
>> In R code, you could use getNamespace("pkg") to get the namespace
>> environment.  I haven't tried this, but I'd assume
>>
>> PROTECT(rho = eval(lang2(install("getNamespace"),
>>                           ScalarString(mkChar("pkg"))));
>>
>> (or something like it) would be the C equivalent.

mkString("pkg") is simpler than ScalarString(mkChar("pkg")).

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




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