[R] find all objects of a particular class

Zhang Honglian zhonglian at hotmail.com
Sun Sep 23 18:15:52 CEST 2007


   Hello,

   I would like to find all objects of a particular class. Is that possible to
   do so in R? I knew that in SPLUS, the function objects(class="classname")
   can do this. But in R, I cannot find the similar function to do so. Is there
   any way that I can distinguish where an object comes from? E.g. I defined a
   class "person". Then I generated three objects from it (e.g, Joe, Lee, Dan).
   I also have some other objects in the workspace (not from class "person").
   How do I suppose to know in the current workspace I have exactly three
   objects  (Joe,  Lee, Dan) from the class "person"? For example, in the
   following program, at the final step when I run objects(), it will give me
   not only "Joe" ""Lee" and "Dan", but also "x". If I run some other programs
   before this, I might have other objects too. How can I let R only give me
   "Joe" ""Lee" and "Dan"?

   Any help will be highly appreciated,

   Honglian

   setClass("person",

   representation(Name="character",

   PhoneNo="character", Address="character"))

   setMethod("show", "person",

   function(object)

   {

   x<- data.frame (Name=object at Name, PhoneNo=object at PhoneNo,

   Address=object at Address)

   print(x)

   }

   )

   Joe =  new("person", Name='Joe', PhoneNo='(210)481-5720', Address='San
   Antonio')

   Dan = new("person", Name='Dan', PhoneNo='(413)583-5202', Address='Boston')

   Lee = new("person", Name='Lee', PhoneNo='(519)837-1291', Address='Toronto')

   x <- objects()

   objects()
     _________________________________________________________________

   Express yourself instantly with MSN Messenger! [1]MSN Messenger Download
   today it's FREE!

References

   1. http://g.msn.com/8HMAEN/2737??PS=47575


More information about the R-help mailing list