Re: [R] lattice: calling functions

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Tue 21 Feb 2006 - 09:43:30 EST


On Tue, 14 Feb 2006, Deepayan Sarkar wrote:

> On 2/14/06, Duncan Murdoch <murdoch@stats.uwo.ca> wrote:

>> On 2/14/2006 9:38 AM, Gabor Grothendieck wrote:
>>> On 2/14/06, Duncan Murdoch <murdoch@stats.uwo.ca> wrote:
>>>> On 2/14/2006 8:56 AM, Wolfram Fischer wrote:
>>>> > I defined three functions:
>>>> >
>>>> >> fun0 <- function( x=1:5, y=1:5, ... ) xyplot( y ~ x, ... )
>>>> >
>>>> >> fun1 <- function( x=1:5, y=1:5, ... ) fun2( y ~ x, ... )
>>>> >> fun2 <- function( ... ) xyplot( ... )
>>>> >
>>>> > The call of fun0() works as expected.
>>>> >
>>>> > The call of fun1() causes the following error:
>>>> >     'Error in eval(expr, envir, enclos) : object "y" not found'
>>>> >
>>>> > How should I define fun2 to avoid the error?
>>>>
>>>> fun2 is fine, it's fun1 that has problems.  It is passing a formula
>>>> through fun2 to xyplot without telling xyplot where to evaluate the
>>>> arguments.  If you change it to
>>>>
>>>> fun1 <- function( x=1:5, y=1:5, ... ) fun2( y ~ x, data=enviroment(), ...
>> )
>

> data=NULL works too, which is apparently what lm has. The point being
> that the environment of the formula is looked at, but the default
> data=parent.frame() subverts that because of the way eval works (using
> enclos only when envir is a list or data frame. What's wrong with
> environments?).

The reason for not using enclos= when envir= is an environment is that an environment already consists of a frame plus enclosing environment. The point of enclos= is to supply the enclosing environment when envir= doesn't have one built in.

        -thomas

> Even the following works:

>

> fun1 <- function( x=1:5, y=1:5, ... )
> fun2( y ~ x, data = data.frame(x = x), ... )
>

> I don't understand non-standard evaluation all that well, so I'll
> happily consider any suggestions. I'll try changing the defaults to
> NULL and see if there are any obvious problems.
>

> Deepayan
>
>>>> it will tell xyplot to look in the current environment at the time of
>>>> the call, i.e. the fun1 evaluation environment where x and y live.
>>>>
>>>
>>> Although this does seem to be how xyplot works, I think it indicates
>>> there is a problem with it.
>>>
>>> The help file for xyplot indicates that for the xyplot formula method
>>> the default
>>> environment is the caller environment whereas it ought to be the
>> environment
>>> of the formula:
>>>
>>>     data: For the 'formula' method, a data frame containing values for
>>>           any variables in the formula, as well as 'groups' and
>>>           'subset' if applicable.  By default the environment where the
>>>           function was called from is used.
>>>
>>> For example, if we replace xyplot with lm it does work as expected:
>>>
>>>    fun1 <- function( x=1:5, y=1:5, ... ) fun2( y ~ x, ... )
>>>    fun2 <- function( ... ) lm( ... )
>>>    fun1()
>>
>> You're right, I forgot formulas have associated environments.  I've
>> added the lattice maintainer to the cc list.
>>
>> Duncan Murdoch
>

> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Feb 22 04:56:07 2006

This archive was generated by hypermail 2.1.8 : Wed 22 Feb 2006 - 09:27:47 EST