[R] "Attach" for S4 objects?

McGehee, Robert Robert.McGehee at geodecapital.com
Tue Jan 18 18:19:22 CET 2005


Certainly this _is_ possible. However, there are no built-in functions to do just this (that I know of). Here's a function that attaches like you say (but still makes a local copy)

attachslot <- function(x) {
    xname <- substitute(x)
    sl <- names(getSlots(class(x)))
    slotnames <- paste(sl, " <<- ", xname, "@", sl, sep = "")

    for (i in slotnames) {
        eval(parse(text = substitute(slotnames, list(slotnames = slotnames))))
    }
}

If you wanted to do this without making a local copy, then the strategy might be either to make a promise object out of the "slotname" (which stores only the unevaluated expression until you need the data), see ?delay, or a bit more complicated, store the "name" of the object as a pointer to the underlying slot. Then, you would be referencing the pointer to the object rather than the copy. Don't want to spend the time to figure this out, but I'm pretty sure it _can_ be done.

All this said, there are good reasons not to attach like this as it is certainly a bad coding habit. If your code ever references two objects of the same class, there might be confusion as to which one is attached as slots are not unique. Also, this relies on the class design and not on the underlying data. I would certainly (and do!) want to switch around my slots or classes without substantively rewriting my code. And lastly, if you ever had an object named x and a slot of an attached object named x then either the former would be rewritten, or you'd have to spend a great deal of time figuring out ways to properly scope your attached slots.

In short, don't do it.

Best,
Robert


-----Original Message-----
From: F.Tusell [mailto:etptupaf at bs.ehu.es] 
Sent: Tuesday, January 18, 2005 9:38 AM
To: R-help at stat.math.ethz.ch
Subject: [R] "Attach" for S4 objects?


When passing a list as an argument to a function, I find it convenient
to attach it in the first line of th function code, then refer to the
components as A, B, etc. rather than as list$A, list$B, etc.

If I pass a S4 class object, is there a way to "attach" it, or do I have
to refer to the slots as object at A, objetc at B, etc.? 

I could always make copies,  

     A <- object at A 
     B <- object at B
     ...

but "object" has many slots, some fairly large, so this seems wasteful.

Thank you.
-- 
Fernando TUSELL                                e-mail:
Departamento de Econometría y Estadística           etptupaf at bs.ehu.es 
Facultad de CC.EE. y Empresariales             Tel:   (+34)94.601.3733
Avenida Lendakari Aguirre, 83                  Fax:   (+34)94.601.3754
E-48015 BILBAO  (Spain)                        Secr:  (+34)94.601.3740

______________________________________________
R-help at 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




More information about the R-help mailing list