[Rd] methods for @ operator

Parlamis Franklin fparlamis at mac.com
Tue May 2 04:53:47 CEST 2006


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



More information about the R-devel mailing list