From fparlamis at mac.com  Tue May  2 04:53:47 2006
From: fparlamis at mac.com (Parlamis Franklin)
Date: Mon, 1 May 2006 16:53:47 -1000
Subject: [Rd] methods for @ operator
Message-ID: <F162114B-C9C8-4B99-870B-7CBF8FEC6995@mac.com>

i often find myself having a list of similarly-classed S4 objects and  
needing a list containing a particular extracted slot from those  
objects.  so i did the following:

 > setMethod("slot", signature(object = "list"),
+ 	function(object, name)
+ 		lapply(object, function(i) slot(i, name)))
Creating a new generic function for 'slot' in '.GlobalEnv'
[1] "slot"

which works as expected.

 > setClass("foo", representation(a = "numeric", b = "character"))
[1] "foo"
 > o1 <- new("foo", a = 1:5, b = "one")
 > o2 <- new("foo", a = 6:9, b = "two")
 > slot(list(o1,o2), "a")
[[1]]
[1] 1 2 3 4 5

[[2]]
[1] 6 7 8 9

 > slot(list(o1, list(o1,o2)), "a")
[[1]]
[1] 1 2 3 4 5

[[2]]
[[2]][[1]]
[1] 1 2 3 4 5

[[2]][[2]]
[1] 6 7 8 9

now, because i am too lazy to type the longer syntax required by the  
'slot' function, i am interested in doing something analogous for the  
'@' operator.
i attempted to do this as I have done it for infix operators like  
'+' , but am not meeting with success.   i am pretty sure this is  
because '+' has a predefined generic with
"dispatchable" formals e1 and e2, whereas no generic is predefined  
for '@'

 > getGeneric("+")
standardGeneric for "+" defined from package "base"
   belonging to group(s): Arith

function (e1, e2)
standardGeneric("+", .Primitive("+"))
<environment: 0x169dea44>
Methods may be defined for arguments: e1, e2

 > getGeneric("@")
NULL

nor can one be set

 > setGeneric("@")
[1] "@"
Warning message:
'@' is a primitive function;  methods can be defined, but the generic  
function is implicit, and cannot be changed. in: setGeneric("@")

however, in this last warning message, R says "methods can be  
defined" so i am left with a ray of hope.  is there some way to do this?

franklin parlamis



[Rd] methods for @ operator
Parlamis Franklin