From sfalcon at fhcrc.org  Fri Apr 27 02:51:53 2007
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Thu, 26 Apr 2007 17:51:53 -0700
Subject: [Rd] evaluation in unattached namespace
In-Reply-To: <46314403.2070701@stats.uwo.ca> (Duncan Murdoch's message of "Thu,
	26 Apr 2007 20:29:55 -0400")
References: <eb555e660704261639h531c7c0fk968283b961b0f504@mail.gmail.com>
	<46314403.2070701@stats.uwo.ca>
Message-ID: <m2vefiod06.fsf@ziti.fhcrc.org>

Duncan Murdoch <murdoch at stats.uwo.ca> writes:

> On 4/26/2007 7:39 PM, Deepayan Sarkar wrote:
>> Hi,
>> 
>> I recently discovered this buglet in lattice: If lattice is _not_
>> attached, I get
>> 
>>> lattice::dotplot(~1:10)
>> Error in eval(expr, envir, enclos) : could not find function "bwplot"
>> 
>> This happens because of this:
>> 
>>> lattice:::dotplot.formula
>> function (x, data = NULL, panel = "panel.dotplot", ...)
>> {
>>     ocall <- ccall <- match.call()
>>     ccall$data <- data
>>     ccall$panel <- panel
>>     ccall[[1]] <- as.name("bwplot")
>>     ans <- eval.parent(ccall)
>>     ans$call <- ocall
>>     ans
>> }
>> <environment: namespace:lattice>
>> 
>> That is, ccall is eval()-ed in the global environment where bwplot is
>> not visible. While this example is somewhat silly, similar things
>> happen when I try to import lattice from another package.
>> 
>> Are there any simple alternatives? I tried
>> 
>>     ccall[[1]] <- as.name("lattice::bwplot")
>> 
>> but that didn't work either.
>
> One way would be to create a new environment with the caller's 
> environment as the parent, and put a copy of bwplot in it, i.e. 
> something like the following untested code:
>
>   function (x, data = NULL, panel = "panel.dotplot", ...)
>   {
>       ocall <- ccall <- match.call()
>       ccall$data <- data
>       ccall$panel <- panel
>       ccall[[1]] <- as.name("bwplot")
>       evalenv <- new.env(parent = parent.frame())
>       assign("bwplot", bwplot, envir=evalenv)
>       ans <- eval(ccall, envir=evalenv)
>       ans$call <- ocall
>       ans
>   }
>

quote(lattice::bwplot) also seems to work.

+ seth

-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
http://bioconductor.org



[Rd] evaluation in unattached namespace
Seth Falcon