[R] getting lapply() to work for a new class

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 16 07:53:42 CEST 2007


On Wed, 15 Aug 2007, Pijus Virketis wrote:

> Thank you.
>
> When I tried to set as.list() in baseenv(), I learned that its bindings
> are locked.

Of course.  Did you not see my comment about 'to protect code against 
redefining functions'?

> Does this mean that the thing to do is just to write my own
> "lapply", which does the coercion using my "private" as.list(), and then
> invokes the base lapply()?

I believe 'the thing to do' is to call your as.list explicitly.  After 
all, the first 'l' in lapply means 'list', so is it is 'natural' to call 
it on a list.

And please do NOT edit other people's messages without indication: the R 
posting guide covers that and it is a copyright violation.

>
> -P
>
> -----Original Message-----

Not so: an EDITED version of my message.

> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Sent: Wednesday, August 15, 2007 5:18 PM
>
>> As far as I can tell, lapply() needs the class to be coercible to a
>> list. Even after I define as.list() and as.vector(x, mode="list")
>> methods, though, I still get an "Error in as.vector(x, "list") :
>> cannot coerce to vector". What am I doing wrong?
>
> Not considering namespaces.  Setting an S4 method for as.list() creates
> an object called as.list in your workspace, but the lapply function uses
> the as.list in the base namespace.  That's the whole point of
> namespaces: to protect code against redefining functions.
>
>

-- 
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-help mailing list